Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 精度的损失_C++_C++11_C++14_Precision - Fatal编程技术网

C++ 精度的损失

C++ 精度的损失,c++,c++11,c++14,precision,C++,C++11,C++14,Precision,如果我有一个等于“123.546123”的双精度字符串,并使用atof将其转换为双精度字符串,我只得到123.546。我能做些什么来解决这个问题 这是我的密码: #include <iostream> int main(){ std::string a = "123.546123"; double b = atof(a.c_str()); std::cout << a << std::endl; std::cout <

如果我有一个等于
“123.546123”
的双精度字符串,并使用
atof
将其转换为双精度字符串,我只得到
123.546
。我能做些什么来解决这个问题

这是我的密码:

#include <iostream>

int main(){

    std::string a = "123.546123";
    double b = atof(a.c_str());

    std::cout << a << std::endl;
    std::cout << b << std::endl;

    return EXIT_SUCCESS;
}
#包括
int main(){
std::string a=“123.546123”;
双b=atof(a.c_str());

std::cout
std::cout
默认打印精度为6的浮点值。要提高精度,请使用from
,例如:

std::cout << std::setprecision(9) << b << std::endl;

std::cout
std::cout
打印默认精度为6的浮点值。要提高精度,请使用from
,例如:

std::cout << std::setprecision(9) << b << std::endl;

std::cout您不会丢失精度,只是std::cout在默认情况下以低于您预期的特定精度打印双精度值。您不会丢失精度,只是std::cout在默认情况下以低于您预期的特定精度打印双精度值。