Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Peter Lawrey for int and double divided by double java

There seems to be some IEEE 754 floating point precision specifications I'm missing

When you use (int) it truncates the fractional part no matter how close it is to the next whole value. It is not reversible as information is lost, so it should be no surprise that you don't get the same result.

What you could do instead is the following

double o = 2.3;long l = Math.round(o * 100000.0);double d = l / 100000.0;System.out.println(d);

jvm optimizations that inlines ... making this a no-op

The JVM might optimise it, but it won't change the result. If an optimisation changes the outcome it is a bug in the optimiser.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>