Search code examples
c++precisioninteger-overflowdouble-precision

what is the precision difference between a double and a very long integer?


Take

 36103009879073133562313702394913733
 36103009879073133562313702394913733.0

as an example,

What is the difference in precision when representing a very long integer into double in C++?


Solution

  • Firstly, there is no very long type in C++. I assume you are talking about a 64-bit integer type and that double corresponds to the 64-bit IEE 754 floating point representation.

    A 64 bit integer has (up to) 64 bits of precision; i.e. roughly 19 decimal digits (assuming an unsigned integer type). By contrast, a 64 bit IEE floating point has 52 bits of precision; i.e. roughly 15 decimal digits.

    The example you give has 35 decimal digits, which means that it can't be represented as a 64 bit integer at all. The double representation of that number would lose roughly the last 20 decimal digits of precision.

    Reference: