#include <iostream>
int main()
{
double a = 2 , b = 3;
double answer = a/b;
std::cout << answer;
std::cout << std::to_string(answer);
}浮点型或双精度型: 0.666667;
我被期望从双精度中获得更高的精度;我如何获得更多的精度,比如: 0.666666666666667
编辑:不能因为使用to_string而丢失精度;
发布于 2020-07-14 10:06:20
i被期望从double中获得更高的精度
在文本表示中,浮点类型的精度与十进制数的精度是分开的概念。
您没有指定所需的精度,因此使用默认值。碰巧的是,缺省值并不是您随意期望的。您可以像这样指定精度:
std::cout.precision(15);发布于 2020-07-14 10:10:15
setprecision位于头文件#include<iomanip>中,您可以使用该文件设置小数精度。
例如:
#include <iostream>
#include <iomanip> // The precision manipulator is in this header file
int main()
{
double a = 2 , b = 3;
double answer = a/b;
std::cout << std::setprecision(15) << answer;
return 0;
}https://stackoverflow.com/questions/62886948
复制相似问题