在我之前的问题中,我打印了一个double using cout,当我没有预料到它时,它变得圆润了。我如何才能使cout打印一个双重使用全精度?


当前回答

下面是如何完全精确地显示double值:

double d = 100.0000000000005;
int precision = std::numeric_limits<double>::max_digits10;
std::cout << std::setprecision(precision) << d << std::endl;

这将显示:

100.0000000000005

Max_digits10是唯一表示所有不同双精度值所需的位数。Max_digits10表示小数点前后的位数。

不要在std::fixed中使用set_precision(max_digits10)。 在固定表示法中,set_precision()只设置小数点后的位数。这是不正确的,因为max_digits10表示小数点前后的位数。

double d = 100.0000000000005;
int precision = std::numeric_limits<double>::max_digits10;
std::cout << std::fixed << std::setprecision(precision) << d << std::endl;

显示错误的结果:

100.00000000000049738

注意:需要头文件

#include <iomanip>
#include <limits>

其他回答

最好的选择是使用std::setprecision,解决方案是这样的:

# include <iostream>
# include <iomanip>

int main()
{
    double a = 34.34322;
    std::cout<<std::fixed<<a<<std::setprecision(0)<<std::endl;
    return 0;
}

注意:您不需要使用cout。我在std::setprecision处填充0,因为它必须有一个参数。

printf("%.12f", M_PI);

%。12f表示浮点数,精度为12位。

利用std::设置精度:

#include <iomanip>
std::cout << std::setprecision (15) << 3.14159265358979 << std::endl;

在c++ 20中,你可以使用std::format来做到这一点:

std::cout << std::format("{}", M_PI);

输出(假设双IEEE754):

3.141592653589793

默认浮点格式是具有往返保证的最短十进制表示形式。与setprecision I/O操纵符相比,这种方法的优点是它不会打印不必要的数字。

在此期间,您可以使用{fmt}库,std::format是基于。{fmt}还提供了print函数,使这更容易和更有效(godbolt):

fmt::print("{}", M_PI);

免责声明:我是{fmt}和c++ 20 std::format的作者。

这将显示点后小数点后两位的值。

#include <iostream>
#include <iomanip>

double d = 2.0;
int n = 2;
cout << fixed << setprecision(n) << d;

看这里:定点符号

std::固定

使用固定浮点符号设置的浮点字段格式标志 STR流的固定。 当floatfield设置为fixed时,将写入浮点值 使用定点表示法:值完全用as表示 由精度字段指定的小数部分中的多个数字 (精度)和无指数部分。

标准::设置精度

Set decimal precision设置格式化时使用的十进制精度 输出操作上的浮点值。

如果您熟悉表示浮点数的IEEE标准,就会知道在标准范围之外完全精确地显示浮点数是不可能的,也就是说,它总是导致实际值的四舍五入。

您需要首先检查该值是否在作用域内,如果是,则使用:

cout << defaultfloat << d ;

std:: defaultfloat

使用默认浮点符号设置floatfield格式标志 为STR流设置defaultfloat。 当floatfield设置为defaultfloat时,浮点值为 使用默认表示法编写:表示使用尽可能多的表示法 需要的有意义的数字,直到流的十进制精度 (精度),同时计算小数点前后的数字 点(如果有的话)。

这也是cout的默认行为,这意味着您没有显式地使用它。