2

Why Float.valueOf("2.7") > 2.7 gives me true?

    public static void main(String[] args) {
    System.out.println(Float.valueOf("2.7") > 2.7);
    System.out.println(Float.valueOf("2.7") > 2.8);
    System.out.println(Float.valueOf("2.7") > 2.6);
    }

prints

true
false
true.

If I do Float.valueOf("2.7") > Float.valueOf("2.7"), it returns me false.

Can anyone helps me to understand this behavior?

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
ashishgupta_mca
  • 578
  • 6
  • 27
  • Interesting. Did you try with Float.valueOf("2.7f") ? :) – A1ternat1ve Feb 23 '15 at 09:18
  • @A1ternat1ve - It won't make any difference. The *result* will still be different i.e, greater than or lesser than but not equal. Thats the problem with floating point numbers in java – TheLostMind Feb 23 '15 at 09:27

4 Answers4

7

The literal 2.7 is a double - i.e. the closest double value to 2.7.

Float.valueOf("2.7") - or 2.7f, equivalently, is the closest float value to 2.7. Neither of them will be exactly 2.7 - and in this case, they're both slightly greater than 2.7. The actual values are:

float:  2.7000000476837158203125
double: 2.70000000000000017763568394002504646778106689453125

As you can see, the float value really is greater than the double value.

In cases where the closest value is lower than the "ideal" one, you'll see the reverse effect, where the float value will be smaller than the double value, because the double value will be closer to the "ideal" one. You'll see that with 2.8, for example, where the values are:

float:  2.7999999523162841796875
double: 2.79999999999999982236431605997495353221893310546875

If you use Double.parseDouble instead of Float.parseFloat - or alternatively, if you make the comparisons work against the float literals, you should get the expected result:

System.out.println(Float.valueOf("2.7") > 2.7f);
System.out.println(Double.valueOf("2.7") > 2.7);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    May want to mention that `System.out.println(Double.valueOf("2.7") > 2.7);` works as expected, since the data types are the same. As does `System.out.println(Float.valueOf("2.7") > 2.7f);`. – paxdiablo Feb 23 '15 at 09:19
  • I can't help but say this. You must become a teacher. :) @JonSkeet – A1ternat1ve Feb 23 '15 at 09:29
1

2.7 is a double literal. Float.valueOf("2.7") is float, and therefore has less precision, which means it might be approximated by a float value that is higher than the double approximation of 2.7.

System.out.println(Float.valueOf("2.7") > 2.7f);

would give you false, since you would be comparing two floats.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

This is because 2.7f > 2.7d i.e. the float representation of 2.7 is slightly higher than the double representation as float is less accurate.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

This is due to the precision of doubles and floats that makes floats slightly higher that doubles.

You can experiment with the following code.

    System.out.println("double: " + new BigDecimal(2.7));
    System.out.println("float: " + new BigDecimal(Float.valueOf("2.7")));
    System.out.println("double: " + new BigDecimal(2.7d));
    System.out.println("float: " + new BigDecimal(2.7f));
  • 2.7 is a double
  • Float.valueOf("2.7") is a float
  • 2.7d is a double
  • 2.7f is a float
antonio
  • 18,044
  • 4
  • 45
  • 61