0

I'm testing the significant digit lengths for float and double in Visual Studio's C++ compiler and it's only showing 6 significant digits for float, double, and long double even though C++ Primer describes float as showing 6 significant digits and double/long double as 10. Why is this the case?

Here's the code using random numbers:

#include <iostream>

int main()
{
    float dec=12.789346;
    long double dec2=12.98746359;
    

    
    std::cout << dec << std::endl
              << dec2 << std::endl;

    std::cin.get();
    std::cin.get();
    return 0;
}

My output is

12.7893

12.9875

Spellbinder2050
  • 644
  • 3
  • 13
  • Required reading: [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Thomas Matthews Mar 31 '21 at 18:05
  • @ThomasMatthews that's not it. The problem is that `<<` has a default precision and it's not being overridden. – Mark Ransom Mar 31 '21 at 18:05
  • I take it that the issue has to do with the default printing of floating point numbers. – Thomas Matthews Mar 31 '21 at 18:07
  • 1
    Side note: `float` is generally considered to be "precise enough" out to 7 digits and double out to 15 digits. I can understand 6 for `float`, but 10 for `double` strikes me as selling `double` short. – user4581301 Mar 31 '21 at 18:12
  • @user4581301 certainly that's true for IEEE doubles, but the book is old enough that it might be taking into account obsolete double representations that are long forgotten. – Mark Ransom Mar 31 '21 at 20:33

0 Answers0