gpt4 book ai didi

c++ - C/C++ : double constants in class are inaccurate

转载 作者:搜寻专家 更新时间:2023-10-31 00:16:15 25 4
gpt4 key购买 nike

我正在用 C++ 编写一个类,它需要一些在整个类中都可以访问的常量。通常我会使用#define、const- 或静态声明,但发生了一些奇怪的事情:所以例如我写

#define Rm 8.3144621

但是std::cout << Rm << std::endl;

输出 8.31446。

我也试过

#define Rm 831.44621e-2

const double Rm = 8.3144621

static double Rm = 8.3144621

以及初始化或转换为 double 常量的所有可能方法。

是否有解决方案,还是我必须使用字段?

最佳答案

如果你需要输出所有数字的 double ,你需要设置正确的精度:

std::cout << std::fixed;
std::cout << std::setprecision(7) << Rm << std::endl;

在此处查看现场演示:Double print Demo

关于c++ - C/C++ : double constants in class are inaccurate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16825654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com