2

I declared three real number values and tried to compare each value as below codes.

public class Main {
    public static void main(String[] args) {
        float f = 0.1f;
        double d= 0.1;
        double d2 = (double)f;

        System.out.println(d);
        System.out.println(d2);
        System.out.println();

        System.out.println(d==f);
        System.out.println(d==d2);
        System.out.println(d2==f);
    }
}

In first comparison (d==f), since the d is a double type whereas f is a float type, so the compiler changed the variable f into double type. It means that the value stored in f will be reassigned into double type values. Therefore this comparison returns false. And also, d==d2 comparison is executed and returns false as a same way in first comparison.

However, the last comparison d2==f, it returns true. I expected it returned the false because the variable f were reassigned into the double type value and the values stored in d2 and f will be slightly different. However, the results is true.

Can you explain the third comparison why it gave the true?

sclee1
  • 1,095
  • 1
  • 15
  • 36
  • If you change the first two like `System.out.println((float) d == f);` and `System.out.println((float) d == d2);` then they'll print `true` too. – Elliott Frisch Sep 16 '16 at 21:33

2 Answers2

0

As you said, when comparing a double and a float, the float is cast to a double.

So let's think about what is happening to your variables in that context.

d2 is a double which has been cast from a float, f is a float which is being cast into a double.

Since they are both being cast from the same type to the same type in the same way, it doesn't really matter how the cast works, they will both evaluate to the same thing.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
0

0.1 is a repeating number when represented in binary.

Since a double has higher precision than a float, you lose information when converting from a double to a float, but you do not lose information when converting from a float to a `double.

A float has 24 bits of precision, while a double has 53 bits (minus one for the sign)

Since d2 came from f, both d2 and f have 23 bits of precision, but d still has 52 bits of precision.

  • This is extremely questionable. While you are technically right that a float to double conversion has no precision loss, you ignore the fact that the internal representation of `.001f` might actually be `.001102`. So `(double).01f` might not yield `.01` as a double. This makes your answer a bit misleading in my opinion. See this question for proof of what I'm saying: http://stackoverflow.com/questions/916081/convert-float-to-double-without-losing-precision – nhouser9 Sep 16 '16 at 21:47
  • @nhouser9 the internal representation of `.001f` can't be `.001102` because there is no `2` – Sam I am says Reinstate Monica Sep 16 '16 at 21:49
  • that doesn't matter. The point is that your answer is misleading because it ignored the way floats are stored internally. – nhouser9 Sep 16 '16 at 21:52
  • @nhouser9 huh? no It didn't. It is entirely base on how floats and doubles are stored internally. The entire basis of the answer is that a double can hold more `0.01` internally than a float can. – Sam I am says Reinstate Monica Sep 16 '16 at 21:53