C++ 带有浮点数的 std::cout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33854825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:19:30  来源:igfitidea点击:

std::cout with floating number

c++

提问by Daniel Laügt

I'm using visual studio 2015 to print two floating numbers:

我正在使用 Visual Studio 2015 打印两个浮点数:

double d1 = 1.5;
double d2 = 123456.789;

std::cout << "value1: " << d1 << std::endl;
std::cout << "value2: " << d2 << std::endl;

std::cout << "maximum number of significant decimal digits (value1): " << -std::log10(std::nextafter(d1, std::numeric_limits<double>::max()) - d1) << std::endl;
std::cout << "maximum number of significant decimal digits (value2): " << -std::log10(std::nextafter(d2, std::numeric_limits<double>::max()) - d2) << std::endl;

This prints the following:

这将打印以下内容:

value1: 1.5
value2: 123457
maximum number of significant decimal digits (value1): 15.6536
maximum number of significant decimal digits (value2): 10.8371

Why 123457 is print out for value 123456.789? Does ANSI C++ specification allow to display anything for floating numbers when std::cout is used without std::setprecision()?

为什么 123457 的值是 123456.789?在没有 std::setprecision() 的情况下使用 std::cout 时,ANSI C++ 规范是否允许显示浮点数的任何内容?

采纳答案by incomplet_

The rounding off happens because of the C++ standard which can be seen by writing std::cout<<std::cout.precision();

四舍五入是因为 C++ 标准,可以通过编写看到 std::cout<<std::cout.precision();

The output screen will show 6 which tells that the default number of significant digits which will be printed by cout statement is 6. That is why it automatically rounds off the floating number to 6 digits.

输出屏幕将显示 6,这表明 cout 语句将打印的默认有效位数为 6。这就是为什么它会自动将浮点数四舍五入为 6 位。

回答by Ankit Acharya

What you have have pointed out is actually one of those many things that the standardization committee should consider regarding the standard iostreamin C++. Such things work well when you write :-

你所指出的实际上是标准化委员会应该考虑的关于standard iostreamC++ 的众多事情之一。当你写的时候,这样的事情工作得很好:-

printf ("%f\n", d2);

But not with std::coutwhere you need to use std::setprecisionbecause it's formatting is similar to the use of %ginstead of %fin printf. So you need to write :-

但不适用于std::cout您需要使用的地方,std::setprecision因为它的格式类似于使用 of%g而不是%fin printf。所以你需要写:-

std::cout << std::setprecision(10) << "value2: " << d2 << std::endl;

But if you dont like this method & are using C++11 (& onwards) then you can also write :-

但是,如果您不喜欢这种方法并使用 C++11(及以后),那么您也可以编写:-

std::cout << "value2: " << std::to_string(d2) << std::endl;

This will give you the same result as printf ("%f\n", d2);.

这将为您提供与printf ("%f\n", d2);.

A much better method is to cancel the rounding that occurs in std::coutby using std::fixed:-

更好的方法是std::cout使用std::fixed以下方法取消舍入:-

#include <iostream>
#include <iomanip>
int main()
{
    std::cout << std::fixed;
    double d = 123456.789;
    std::cout << d;
    return 0;
}

Output :-

输出 :-

123456.789000

So I guess your problem is solved !!

所以我想你的问题已经解决了!!

回答by Mats Petersson

I think the problem here is that the C++ standard is not written to be easy to read, it is written to be precise and not repeat itself. So if you look up the operator<<(double), it doesn't say anything other than "it uses num_put- because that is how the cout << some_float_valueis implemented.

我认为这里的问题是 C++ 标准不是为了易于阅读而编写的,而是为了精确而不是重复而编写的。因此,如果您查找operator<<(double),除了“它使用num_put- 因为这cout << some_float_value就是实现的方式”之外,它不会说任何其他内容。

The default behaviour is what print("%g", value);does [table 88 in n3337 version of the C++ standard explains what the equivalence of printf and c++ formatting]. So if you want to do %.16gyou need to change the precision by calling setprecision(16).

默认行为是print("%g", value);[C++标准的n3337版本中的表88解释了printf和c++格式的等价性]。因此,如果您想这样做%.16g,则需要通过调用setprecision(16).