我需要获得更精确的数学函数log()输出。 我想获得数字的第2和第10位数。
我的测试代码如下:
#include "string"
#include <sstream>
double temp_number = log(10.0);
std::ostringstream strs;
strs << temp_number;
std::string str = strs.str();
std::cout << str << "\n";
我正在将double转换为字符串,因为我不知道如何通过double的索引获取数字。无论如何,当转换为字符串时,即使我在使用cout
之前检查输出,计算得到的舍入为6位,这显然不够精确。
有什么建议吗?
答案 0 :(得分:0)
正确的方法是:
#include "string"
#include <sstream>
#include <iomanip> // std::setprecision
double temp_number = log(10.0);
std::ostringstream strs;
strs << std::setprecision(15) << temp_number;
std::string str = strs.str();
std::cout << str << "\n";
Usind std::setprecision(15)
有助于获得正确的值