精度损失
A loss of 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 << b << std::endl;
return EXIT_SUCCESS;
}
std::cout
默认打印精度为 6 的浮点值。要提高精度,请使用 <iomanip>
中的 std::setprecision
,例如:
std::cout << std::setprecision(9) << b << std::endl;
如果我有一个等于 "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;
}
std::cout
默认打印精度为 6 的浮点值。要提高精度,请使用 <iomanip>
中的 std::setprecision
,例如:
std::cout << std::setprecision(9) << b << std::endl;