Hexfloat机械手和精度

时间:2015-12-16 22:44:12

标签: c++ c++11 hex precision

如何使用help sessionoptions操纵器输出忽略hexfloat上的任何精度?

ostream

尽管默认精度为6,但在以十六进制格式(coliru)(gcc 5.2.0)输出双精度时似乎会被忽略:

#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?

1 个答案:

答案 0 :(得分:7)

std::hexfloat格式旨在转储浮点值的确切重复。字符串表示不供人类消费,但主要是为了再次恢复完全相同的表示。因此,基于任何流设置以任何方式截断格式都没有意义。