Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Java rounding this float

Given what we know about floating point precision, why does this code:

float a = 0.1f;
System.out.println(a);

Print 0.1 and not 0.100000001?

like image 593
imrichardcole Avatar asked Sep 12 '26 13:09

imrichardcole


2 Answers

The println(a) call to ends up calling Float.toString(a), which says:

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type float. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument f. Then f must be the float value nearest to x; or, if two float values are equally close to x, then f must be one of them and the least significant bit of the significand of f must be 0.

Or said another way, since a IEEE 754 single-precision floating-point value only has from 6 to 9 significant decimal digits precision, it won't include that final 1, since it exceeds the precision of the stored value.

Here is code to show the numbers:

System.out.println(Math.nextDown(0.1f)); // prints: 0.099999994
System.out.println(0.1f);                // prints: 0.1
System.out.printf("%.15f%n", 0.1f);      // prints: 0.100000001490116
System.out.println(Math.nextUp(0.1f));   // prints: 0.10000001
like image 172
Andreas Avatar answered Sep 15 '26 02:09

Andreas


0.100000001 wouldn't be exact either. If Java were to print this float completely unrounded, it would print

0.100000001490116119384765625

which is unuseful for most floating-point use cases.

Either it doesn't round at all and you get 0.100000001490116119384765625, or it rounds, and you need a rounding strategy. The rounding strategy they picked is to print exactly enough digits after the decimal point to distinguish the float from any other float value, with a minimum of one digit. It is not necessarily enough to distinguish the float from any other double value.

like image 34
user2357112 supports Monica Avatar answered Sep 15 '26 02:09

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!