简体   繁体   English

为什么cout截断了double?

[英]Why does cout truncate a double?

The following is my console input/output. 以下是我的控制台输入/输出。

Please enter a real number: -23486.33 Characters checked: 9 请输入真实数字:-23486.33检查的字符:9

Thank you. 谢谢。 The real number you entered is -23486.3 您输入的真实数字是-23486.3

The value I entered is -23486.33, but yet cout prints it as -23486.3. 我输入的值为-23486.33,但是cout将其打印为-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; 调试时,我在方法调用atof(value.c_str())l;之后立即检查number的值atof(value.c_str())l; . Number is shown to have a value of -23486.33. 数字显示值为-23486.33。 So what happens between that and the print out by cout? 那么,这与cout打印出来之间会发生什么? In no part of my code do I set the precision of cout or make it fixed. 在我的代码中,我都没有设置cout的精度或使其固定。

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/ 查看: 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. 当您将DEC数字的字符串表示形式转换为双精度(浮点数表示形式)时,内存中的数据在数学上可能不等于字符串表示形式。 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. 显示-23486.3是因为std::cout默认只输出6位数字。

To print back a number entered from standard input (convertion text → floating number → text), you can use set_precision with digits10 as precision: 要打印回从标准输入中输入的数字(转换文本→浮点数→文本),可以将set_precisiondigits10用作精度:

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

This displays: 显示:

-23486.33 -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: 要打印具有全精度(通常为皈依浮点数→文本→浮数)一个数字,你可以使用set_precisionmax_digits10精密:

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

This displays: 显示:

-23486.330000000002 -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). 此处的打印编号不同,因为-23486.33在IEEE编码中没有确切的表示形式(以2为基数而不是10为基数)。


For more details with digits10 and max_digits10 , you can read: 有关digits10max_digits10更多详细信息,您可以阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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