Search code examples
floating-pointdoubleieee-754

Can 42 be precisely represented as a floating-point?


I have learned that 0.1 cannot be exactly represented as a floating-point number. The textbook argument was that 0.1 cannot be written as a kind of binary-based scientific notation, although I have never been certain how people prove that argument. Related to that, my question now is

Can 42 be precisely represented as a floating-point ?

Say we are discussing the IEEE-754 binary64 format.


Solution

  • Well, an integer value small enough to fit floating point's mantissa (42 in your case) can be represented precisely

     42 (decimal) == 101010 (binary)
    

    on the contary, 1/10 can't be represented precisely:

     0.1 (decimal) == 0.0001100110011001100... (binary)
    

    since 0.1 decimal is a binary periodical fraction