ostream 中的负精度值
这更多是一个好奇的问题,但有谁知道 C++ 中如何处理负精度值?例如:
double pi = 3.14159265;
cout.precision(-10);
cout.setf(ios::fixed, ios::floatfield);
cout << pi << endl;
我已经尝试过并使用 GCC,似乎精度值被忽略,但我很好奇是否有一些关于这种情况下发生的情况的官方路线。
This is more of a question of curiosity but does anyone know how negative precision values are handled in C++? For example:
double pi = 3.14159265;
cout.precision(-10);
cout.setf(ios::fixed, ios::floatfield);
cout << pi << endl;
I've tried this out and using GCC and it seems that the precision value is ignored but I was curious if there is some official line on what happens in this situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
奇怪的是(恕我直言,这是错误的)C++ 标准指定了一个有符号类型(streamsize)作为精度参数,因此它不会被转换为一个大数。然而,该标准并没有说明负数可能意味着什么(如果有的话)。
Strangely (and wrongly, IMHO) the C++ Standard specifies a signed type (streamsize) as the parameter for precision, so it won't be converted to a large number. However, the standard is silent on what a negative number might mean, if anything.
我在 C++03 也不在 C++0x (N3092) 草案中。但是,C89 标准表示
我希望 C++ std::ostream 行为与 CI/O printf 一致。 fprintf 和相关函数。
使用 Visual C++ 10.0 和 GCC 4.4.1 进行的快速测试表明是这种方式,负精度意味着省略精度。并且,省略精度意味着默认值,即 表 125 中的 27.5.4.1 basic_ios 构造函数 中指定的 6 位
I can't find any specification of this behaviour neither in C++03 nor in draft of C++0x (N3092). However, the C89 standard says
I would expect that C++ std::ostream behaviour is consistent with the C I/O printf. fprintf and related functions.
A quick test with Visual C++ 10.0 and GCC 4.4.1 suggests it to be this way and negative precision means precision omitted. And, precision omitted means a default value which is 6 places as specified in 27.5.4.1 basic_ios constructors in Table 125