【问题标题】:Hexfloat manipulator and precisionHexfloat 机械手和精度
【发布时间】: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?

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    相关资源
    最近更新 更多