Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Entering a value such as 27.8675309 into the "Decimal representation" field of the IEEE 754 Converter changes the value I entered to 27.86753 . Likewise, Java drops the last two digits when a parse a string with the same value.

Float.parseFloat("27.8675309") // Results in a float value of 27.86753

I am not sure what the "Decimal representation" of the IEEE converter actually is (is it a float?) but I would expect it to give me the biggest number possible that:

  • Is a float value
  • Does not exceed the original value I entered
  • I would expect Java to behave in a similar fashion, that is, I would expect the line of code above to return a float value equal to 27.8675308 or an even larger float value that is closer to my original input instead of just dropping decimal places. What am I missing here?

    This is as expected.

    If you look at the binary representation of 27.8675309 (27.867530822753906 as a double):

    01000001110111101111000010110100
    

    the next highest value is:

    01000001110111101111000010110101
    

    which yields 27.867533 (27.86753273010254 as a double), and the next lowest value is:

    01000001110111101111000010110011
    

    which yields 27.867529 (27.867528915405273 as a double)

    There simply aren't enough bits in the mantissa of a Float to represent any value in between, so your value is rounded down in decimal to 27.867530

    Also, just so I am clear, this isn't a matter of running out of decimal places, it is a matter of no more bits for the exponent and/or mantissa of the float, correct? Some other float value could have more than 5 decimal places right? – ubiquibacon Jan 31, 2012 at 17:33 @typoknig it's not more bits for the mantissa - this number is small enough that the exponent doesn't matter. And yes, smaller numbers could have more than five decimal places, but no more than 7 or 8 significant digits. – Alnitak Jan 31, 2012 at 17:36 @Alnitak 27.8675309 rounded to 24 bits = 11011.11011110000101101 = 27.86753082275390625. Rounded to 8 decimal digits, that's 27.867531, not 27.867530. To 7 digits, it's 27.86753, but then so are both the prev float (27.8675289154052734375) and the next float (27.8675327301025390625). Why is h-schmidt.net/FloatApplet/IEEE754.html rounding 27.8675309 to 7 digits, but prev and next to 8? – Rick Regan Feb 21, 2012 at 15:17 @RickRegan in IEE754 the 27 won't be part of the mantissa - you'll actually have 2^4 * 1.741720... – Alnitak Feb 21, 2012 at 15:53

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.