2

I have the following code:

public static float FLOAT_VALUE = 0.899999761581421f;

But whenever I call this value using the debugger, I see that it has now become 0.9f. Why is this happening and how do I get around it?

p192
  • 518
  • 1
  • 6
  • 19
  • 2
    https://stackoverflow.com/questions/588004/is-floating-point-math-broken – epascarello Oct 04 '17 at 00:12
  • 2
    `float` has [6 to 9 significant decimal digits precision](https://en.wikipedia.org/wiki/Single-precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format:_binary32). You're specifying 15 digits of precision. `float` can't store that, but `double` can, since it has [15 to 17 significant decimal digits precision](https://en.wikipedia.org/wiki/Double-precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64). – Andreas Oct 04 '17 at 00:22
  • The debugger is rounding to fewer digits than it should. The closest float to 0.899999761581421 is 0.89999973773956298828125. The closest float to 0.9 is 0.89999997615814208984375. They are different. The toString conversion of your float is 0.89999974, not 0.9 – Patricia Shanahan Oct 04 '17 at 00:47
  • 1
    I have voted to reopen. This question concerns a debugger rounding to fewer digits than are needed to distinguish unequal floats, a problem that does not seem to be covered by the canonical question. – Patricia Shanahan Oct 04 '17 at 00:49
  • Vote to reopen. This is not duplicate thing since Patrick said he is using the debugger. – Thomas Oct 04 '17 at 01:18

1 Answers1

3

Either the value really is changing, or, more likely, the debugger is failing to display enough digits to distinguish distinct floats.

The closest float to 0.899999761581421 is 0.89999973773956298828125. The closest float to 0.9 is 0.89999997615814208984375. They are different.

The toString conversion of 0.89999973773956298828125 is 0.89999974, not 0.9

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75