Search code examples
c++double-precisionlong-double

Precision long double worse than double


When I use long double I get worse precision than using double. 3.14159265358979323846264L is this good to write long double const in source code or I should add something other than L?

EDIT I solved the problem. I change algorithm to be more precisely.


Solution

  • Your are not getting a worse precision.

    What is happening is that when you print the number out the stream library is truncating the displayed value. Use std::setprecision to get a specific precision.

    double        x = 1.0/3;
    long double   y = 1.0/6;
    
    // Prints out the precision
    std::cout << "Limit: " << std::numeric_limits<double>::digits10 << "\n";
    std::cout << "Limit: " << std::numeric_limits<long double>::digits10 << "\n";
    
    // prints the numbers with max precision.
    std::cout << std::setprecision(std::numeric_limits<double>::digits10) << x << "\n";
    std::cout << std::setprecision(std::numeric_limits<long double>::digits10) << y << "\n";