当流到 std 输出时,我可以指定 set precision 对双精度值进行舍入吗?
ofile << std::setprecision(12) << total_run_time/TIME << "\n";
Output:0.756247615801
ofile << std::setprecision(6)<< total_run_time/TIME << "\n";
Output:0.756248
但我需要输出为0.756247
Thanks
还有std::fesetround
from <cfenv>
,设置舍入方向:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cfenv>
int main () {
double runtime = 0.756247615801;
// Set rounding direction and output with some precision:
const auto prev_round = std::fegetround();
std::fesetround(FE_DOWNWARD);
std::cout << "desired: " << std::setprecision(6) << runtime << "\n";
// Restore previous rounding direction and output for testing:
std::fesetround(prev_round);
std::cout << "default: " << std::setprecision(6) << runtime << "\n";
}
(请注意,这些不是我推荐的评论,它们仅用于辅导目的)
Output:
desired: 0.756247
default: 0.756248
但重要提示:我在标准中没有发现任何提及operator<<
浮点类型的重载have以遵守舍入方向。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)