C++ – Losing Precision with Floating Point Numbers (Double)

c++doublefloating-pointprecision

I'm trying to assign a big double value to a variable and print it on the console. The number I supply in is different than what is displayed as output. Is it possible to get the double value correctly assigned and output without the loss of precision? Here is the C++ code:

#include <iostream>
#include <limits>

int main( int argc, char *argv[] ) {

    // turn off scientific notation on floating point numbers
    std::cout << std::fixed << std::setprecision( 3 );

    // maximum double value on my machine
    std::cout << std::numeric_limits<double>::max() << std::endl;

    // string representation of the double value I want to get
    std::cout << "123456789123456789123456789123456789.01" << std::endl;

    // value I supplied        
    double d = 123456789123456789123456789123456789.01;

    // it's printing 123456789123456784102659645885120512.000 instead of 123456789123456789123456789123456789.01
    std::cout << d << std::endl;

    return EXIT_SUCCESS;
}

Could you, please, help me to understand the problem.

Best Answer

C++ built-in floating point types are finite in precision. double is usually implemented as IEEE-754 double precision, meaning it has 53 bits of mantissa (the "value") precision, 11 bits of exponent precision, and 1 sign bit.

The number 123456789123456789123456789123456789 requires way more than 53 bits to represent, meaning a typical double cannot possibly represent it accurately. If you want such large numbers with perfect precision, you need to use some sort of a "big number" library.

For more information on floating point formats and their inaccuracies, you can read What Every Programmer Should Know About Floating-Point Arithmetic.

Related Question