-2

If we run the following code:

float f = 1.2345678990922222f;
double d = 1.22222222222222222222d;
System.out.println("f = " + f + "\t" + "d = " + d);

it prints:

f = 1.2345679   d = 1.2222222222222223

The long tail in the literal 1.2345678990922222 is ignored but the long tail in 1.22222222222222222222 is not (the last decimal digit in the variable d becomes 3 instead of 2). Why?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
andrew
  • 885
  • 2
  • 8
  • 16
  • double has twice more bytes as float to represent data. It can be much more accurate... but it can't be perfect. – spi Feb 25 '19 at 15:48
  • [How many significant digits have floats and doubles in java?](https://stackoverflow.com/questions/13542944/how-many-significant-digits-have-floats-and-doubles-in-java) – BackSlash Feb 25 '19 at 15:48
  • The amount of data doesn't change the fact that types as float and double have their limits. – Tom Feb 25 '19 at 15:49
  • https://stackoverflow.com/questions/322749/retain-precision-with-double-in-java – mahieus Feb 25 '19 at 15:49
  • @Lino: Please do not promiscuously mark floating-point question as duplicates of [that one](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). This question asks why more or fewer digits are displayed, and that is a consequence of Java’s rules for converting `float` and `double` to decimal, which is not explain in that question or [this one](https://stackoverflow.com/questions/13542944/how-many-significant-digits-have-floats-and-doubles-in-java). – Eric Postpischil Feb 25 '19 at 17:13
  • @Michael: Please do not promiscuously mark floating-point question as duplicates of [that one](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). This question asks why more or fewer digits are displayed, and that is a consequence of Java’s rules for converting `float` and `double` to decimal, which is not explain in that question or [this one](https://stackoverflow.com/questions/13542944/how-many-significant-digits-have-floats-and-doubles-in-java). – Eric Postpischil Feb 25 '19 at 17:13

1 Answers1

3

The number of digits you see when a float or a double is printed is a consequence of Java’s rules for default conversion of float and double to decimal.

Java’s default formatting for floating-point numbers uses the fewest significant decimal digits needed to distinguish the number from nearby representable numbers.1

In your example, 1.2345678990922222f in source text is converted to the float value 1.2345678806304931640625, because, of all the values representable in the float type, that one is closest to 1.2345678990922222. The next lower and next higher values are 1.23456776142120361328125 and 1.23456799983978271484375.

When printing this value, Java only needs to print “1.2345679”, because that is enough that we can pick out the float value 1.2345678806304931640625 from its neighbors 1.23456776142120361328125 and 1.23456799983978271484375.

For your double example, 1.22222222222222222222d is converted to 1.22222222222222232090871330001391470432281494140625. The next lower and next higher values representable in double are 1.2222222222222220988641083749826066195964813232421875 and 1.2222222222222225429533182250452227890491485595703125. As you can see, to distinguish 1.22222222222222232090871330001391470432281494140625 from its neighbors, Java needs to print “1.2222222222222223”.

Footnote

1 The rule for Java SE 10 can be found in the documentation for java.lang.float, in the toString(float d) section. The double documentation is similar. The passage, with the most relevant part in bold, is:

Returns a string representation of the float argument. All characters mentioned below are ASCII characters.

  • If the argument is NaN, the result is the string "NaN".

  • Otherwise, the result is a string that represents the sign and magnitude (absolute value) of the argument. If the sign is negative, the first character of the result is '-' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:

    • If m is infinity, it is represented by the characters "Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-Infinity".

    • If m is zero, it is represented by the characters "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0".

    • If m is greater than or equal to 10-3 but less than 107, then it is represented as the integer part of m, in decimal form with no leading zeroes, followed by '.' ('\u002E'), followed by one or more decimal digits representing the fractional part of m.

    • If m is less than 10-3 or greater than or equal to 107, then it is represented in so-called "computerized scientific notation." Let n be the unique integer such that 10nm < 10n+1; then let a be the mathematically exact quotient of m and 10n so that 1 ≤ a < 10. The magnitude is then represented as the integer part of a, as a single decimal digit, followed by '.' ('\u002E'), followed by decimal digits representing the fractional part of a, followed by the letter 'E' ('\u0045'), followed by a representation of n as a decimal integer, as produced by the method Integer.toString(int).

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.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312