3

When I run the following code:

public static void main(String[] args) {
    float number = 10.0f/6.0f;
    System.out.println(number);
}

My output is:

1.6666666

This sort of makes sense to me - float has seven digits of accuracy. Not sure why the integer portion doesn't count as one of those digits, or why the last digit isn't a 7, but okay. Similarly, when i switch to double:

public static void main(String[] args) {
    double number = 10.0/6.0;
    System.out.println(number);
}

My output is:

1.6666666666666667

This also sort of makes sense. Double has greater accuracy. 15 digits according to my textbook. I've got a 16 digit mantissa and this time it rounds up to 7 at the end. Not sure why the deviation in the number of digits, and maybe the 7 at the end where we had a 6 at the end of the float is due to the fact that the original calculation is happening in binary and being converted to decimal.

What I don't understand though is what happens when I do this:

public static void main(String[] args) {
    double number = 10.0f/6.0f;
    System.out.println(number);
}

My output is:

1.6666666269302368

My understanding is that the division results in a float that is implicitly converted to a double when it is assigned to the variable, but where do all the extra digits come from? I expected zeros. I thought that it might be that the java compiler treats the situation specially because it sees that the mathematical expression is part of the declaration and initialization of a double type variable, but the following code has the same output:

public static void main(String[] args) {
    float number1 = 10.0f/6.0f;
    double number2 = number1;
    System.out.println(number2);
}

Again:

1.6666666269302368
Denis
  • 33
  • 1
  • 5
  • You should learn about "binary numbers". – Scott Hunter Mar 09 '19 at 00:17
  • @ScottHunter - I recognize that because 10 is not a power of 2, discrepancies occur converting between the two systems. I do not understand how that applies to this situation. I mention binary as a possible source of the problem. I do not think you actually read my entire question. – Denis Mar 09 '19 at 00:22
  • @Denis: I think the key insight is that the internal storage of a floating-point number does not have *any* decimal digits. You can say that a float has seven digits and a double has 15, but both of those numbers are, strictly speaking, wrong. The decimal digits are an artifact of how the floating-point number got turned into a string (which happens implicitly when you print it); they aren't "there" except for when you convert back to decimal. – Daniel Pryden Mar 09 '19 at 00:37
  • 1
    @DanielPryden - That sort of makes sense. Is this a concept that would be better to come back to after I've had more than one semester of undergraduate computer science? – Denis Mar 09 '19 at 00:39
  • @Denis: The day you can read [Goldberg's paper](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html) and understand all of it, you will be a better computer scientist than 99% of the people working in the field today. I would start with a simpler resource and work your way up to it. *Numerical computing* is a typical undergraduate class you can take which will teach you about a lot of this stuff. – Daniel Pryden Mar 09 '19 at 00:44

0 Answers0