Hexfloat manipulator and precision

六眼飞鱼酱① 提交于 2019-12-10 10:24:30

问题


How come output using the hexfloat manipulator ignores any precision on ostream?

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main(){

    cout << setw(17) << left << "default format: " << setw(20) << right << 100 * sqrt(2.0) << " " <<  cout.precision() << '\n'
         << setw(17) << left << "scientific: " << setw(20) << right << scientific << 100 * sqrt(2.0) << " " <<  cout.precision() <<  '\n'
         << setw(17) << left << "fixed decimal: " << fixed << setw(20) << right << 100 * sqrt(2.0) << " " <<  cout.precision() <<  '\n'
         << setw(17) << left << "hexadecimal: " << hexfloat << setw(20) << right << uppercase << 100 * sqrt(2.0) << nouppercase << " " <<  cout.precision() <<  '\n'
         << setw(17) << left << "use defaults: " << defaultfloat << setw(20) << right << 100 * sqrt(2.0) << " " <<  cout.precision() <<  "\n\n";

}

Despite a default precision of 6 this seems to get ignored when outputting the double in hexadecimal format (coliru) (gcc 5.2.0):

default format:               141.421 6
scientific:              1.414214e+02 6
fixed decimal:             141.421356 6
hexadecimal:     0X1.1AD7BC01366B8P+7 6
use defaults:                 141.421 6

Is it possible to ensure a decimal precision of 6 using hexadecimal format?


回答1:


The std::hexfloat format is intended to dump out the exact repesentation of a floating point value. The string represention isn't for human consumption but primarily to restore the exact same representation again. Thus, it does not make sense to truncate the format in any way based on any stream setting.



来源:https://stackoverflow.com/questions/34323580/hexfloat-manipulator-and-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!