brs*_*gic 5 c++ floating-point
当流到std输出时,我可以指定setprecision来舍入double值吗?
ofile << std::setprecision(12) << total_run_time/TIME << "\n";
输出: 0.756247615801
ofile << std::setprecision(6)<< total_run_time/TIME << "\n";
输出: 0.756248
但我需要输出为 0.756247
谢谢
Seb*_*ach 14
还有std::fesetroundfrom <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";
}
(请注意,这些不是我推荐的评论,它们仅用于辅导目的)
输出:
desired: 0.756247
default: 0.756248
但重要提示:我没有在标准中找到任何提及,operator<<浮动类型的重载必须遵循舍入方向.
| 归档时间: | 
 | 
| 查看次数: | 7853 次 | 
| 最近记录: |