-3

When I tried the following codes in Java:

System.out.println("0.1d/0.3d is " + 0.1d/0.3d)
System.out.println("0.1f/0.3d is " + 0.1f/0.3d)

I get the following output:

0.1d/0.3d is 0.33333333333333337
0.1f/0.3d is 0.3333333383003871

If float/double should get a double then float/double should be same with double/double in this case.

shifu.zheng
  • 691
  • 7
  • 16
  • 4
    No, it's a double, but `0.1d != 0.1f`. – user2357112 Nov 30 '17 at 08:40
  • To get a precise result, just use BigDecimal. – davidxxx Nov 30 '17 at 08:41
  • 1
    One actually sees nicely that a float (4 bytes) has about half the precision of double (8 bytes). And 0.1 is in binary an infinite sequence. – Joop Eggen Nov 30 '17 at 08:48
  • 3
    Wait, you're actually demanding same precision for half the cost? – kumesana Nov 30 '17 at 09:03
  • Double carry 16 precision `0.1111111111111111d/0.3d` no matter how much you added to the precision above 16 digit it will be the same, and float carry 8 precision `0.11111111f/0.3d` no matter how much you added to the precision place above 8 digit it will be the same value, Therefore you can't get the same value, as double use 64bits and float use 32bits. –  Nov 30 '17 at 09:11

5 Answers5

2

Just execute the below code to see binary string of number you want to check for:

System.out.println(Long.toBinaryString(Double.doubleToLongBits(0.1d)));
System.out.println(Integer.toBinaryString(Float.floatToIntBits(0.1f)));

Output:

11111110111001100110011001100110011001100110011001100110011010

111101110011001100110011001101

And you see the difference in float bits and double bits. so we can't get the same result for float/double and double/double for a longer precision values .

Community
  • 1
  • 1
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
1

yes "milbrandt" said right when we are converting float quantity into double by implicit typecasting some information at decimal point will differ. That's why it is showing the different result while dividing float/ double.

1

As you probably know, double uses 64 bits to store a value but float 32 bits.

To represent significand, double uses 52 bits, and float uses 23 bits. Thus double can give 15 digit precision at most, and float can give 7.

If you do float / double, you actually try to divide a floating point with 7 digit precision at most (far to exact) by a floating point with 15 digit precision at most (closer to exact), and then implicitly typecast the result to a floating point with 15 digit precision. The typecast cannot restore your result from 7 digit precision to 15 digit. Therefore, the results are different though both of them are double.

Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42
0

initialize a double with a float and divide then. You will see the same result.

    double f = 0.1f;
    double d = 0.1d;
    double n = 0.3d;

    System.out.println("float /double = " + f/n);
    System.out.println("double/double = " + d/n);

result is

float /double = 0.3333333383003871
double/double = 0.33333333333333337
milbrandt
  • 1,438
  • 2
  • 15
  • 20
0

The reason why you see this difference is because you don't have these exact values.

Even if we suppose 0.1d and 0.3d would be exact (which they aren't, what you can see on the result 0.33333333333333337), 0.1f is 0.10000000149011612, which makes the difference.

glglgl
  • 89,107
  • 13
  • 149
  • 217