简体   繁体   中英

Why does cout truncate a double?

The following is my console input/output.

Please enter a real number: -23486.33 Characters checked: 9

Thank you. The real number you entered is -23486.3

The value I entered is -23486.33, but yet cout prints it as -23486.3. The relevant code is below:

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

//  Function prototype (declaration)
string readDouble();
bool isValidDouble(string);

int main()
{
    string value;
    double number;

    value = readDouble();
    while (!isValidDouble(value)) {
        cout << "The number you entered is not a valid integer." << endl;
        value = readDouble();
    }

    number = atof(value.c_str());
    cout << "Thank you." << endl
         << "The real number you entered is " << number << endl;
}

When debugging, I check the value of number right after the method call atof(value.c_str())l; . Number is shown to have a value of -23486.33. So what happens between that and the print out by cout? In no part of my code do I set the precision of cout or make it fixed.

If you have any questions, please let me know.

Have you tried

std::cout << std::setprecision(2) << number;

look at: http://www.cplusplus.com/reference/iomanip/setprecision/

You can set the precision to the maximum limit for double.

The code snippet is here:

#include <iostream>
#include <limits>
#include <iomanip>

using namespace std;
double number = ... // your double value.
cout << setprecision(numeric_limits<double>::digits10) << number << endl; 

Set a precision when you output a double and keep precision explicitly when you compare them.

When you convert a string presentation of a DEC number to a double(float point number presentation), the data in the memory might not be mathematically equal to the string presentation. It's the best approximation by a float point number presentation, and vise versa.

-23486.3 is displayed because std::cout prints only 6 digits by default.

To print back a number entered from standard input (convertion text → floating number → text), you can use set_precision with digits10 as precision:

double d = -23486.33;
int precision = std::numeric_limits<double>::digits10;
std::cout << std::setprecision(precision) << d << std::endl;

This displays:

-23486.33


To print a number with full precision (usually for convertion floating number → text → floating number), you can use set_precision with max_digits10 as precision:

double d = -23486.33;
int precision = std::numeric_limits<double>::max_digits10;
std::cout << std::setprecision(precision) << d << std::endl;

This displays:

-23486.330000000002

Here the printed number is not the same because -23486.33 doesn't have an exact representation in IEEE encoding (expressed in base 2 instead of base 10).


For more details with digits10 and max_digits10 , you can read:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM