【发布时间】:2016-03-23 06:31:06
【问题描述】:
为什么使用hexfloat 操纵器的输出会忽略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";
}
尽管默认精度为 6,但在以十六进制格式 (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
是否可以使用十六进制格式确保十进制精度为 6?
【问题讨论】:
-
见LWG 671。