Inconsistent results from int(floatNumber)

Ian ian.g.kelly at gmail.com
Mon Oct 25 18:51:59 EDT 2010


On Oct 25, 4:25 pm, Terry Reedy <tjre... at udel.edu> wrote:
> The binary float resulting from the conversion of .1 is slightly greater
> than .1, so this increases i by slightly more than .1
>
> >    z = i * 10.0
>
> so z is increased be lightly more that 1

It should also be pointed out that Java in fact does the same thing:

$ cat Test.java
class Test
{
    public static void main(String[] argv)
    {
        double i = -50.0;
        for (int x = 0; x < 5; x++)
        {
            i += 0.1;
            double z = i * 10.0;
            System.out.println("");
            System.out.println(z);
            System.out.println((int) z);
        }
    }
}
$ javac Test.java
$ java Test

-499.0
-499

-498.0
-498

-496.99999999999994
-496

-495.99999999999994
-495

-494.99999999999994
-494

Cheers,
Ian



More information about the Python-list mailing list