ostream 中的负精度值

发布于 2024-08-16 04:33:44 字数 247 浏览 1 评论 0原文

这更多是一个好奇的问题,但有谁知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撕心裂肺的伤痛 2024-08-23 04:33:44

奇怪的是(恕我直言,这是错误的)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.

烟花肆意 2024-08-23 04:33:44

我在 C++03 也不在 C++0x (N3092) 草案中。但是,C89 标准表示

7.19.6.1 fprintf 函数

采用负精度参数,就像省略精度一样。

我希望 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

7.19.6.1 The fprintf function

A negative precision argument is taken as if the precision were omitted.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文