Hexfloat机械手和精度 [英] Hexfloat manipulator and precision

查看:192
本文介绍了Hexfloat机械手和精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用hexfloat操纵器的输出为何会忽略ostream的任何精度?

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";

}

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

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

是否可以使用十六进制格式确保十进制精度为6?

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

推荐答案

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

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.

这篇关于Hexfloat机械手和精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆