Quantcast
Viewing all articles
Browse latest Browse all 4

int and double divided by double java

How do I explain the below:

    double o = 2.3;    int i = (int) (o * 100000.0);    double d = i / 100000.0;    System.out.println(d);

prints 2.29999

    double o = 2.3;    double i = o * 100000.0;    double d = i / 100000.0;    System.out.println(d);

prints 2.3

When dividing an int by double doesn't java first cast the int as a double then do the division - if so the two code blocks should effectively print the same value?

There seems to be some IEEE 754 floating point precision specifications I'm missing and/or jvm optimizations that inlines

 double i = 2.3 * 100000.0; double d = i / 100000.0;

as double d = 2.3 * 100000.0 / 100000.0;effectively cancelling out the division and making this a no-op.

Thoughts?


Viewing all articles
Browse latest Browse all 4

Trending Articles