浮点数和双变量中的数字

Digits in float and double variable

本文关键字:数字 变量 浮点数      更新时间:2023-10-16

为什么浮点数和双变量存储的位数相同,双精度不应该存储比浮点数更多的数字

#include <iostream>
using namespace std;
int main(){
double a = 3.141528579238;
float b = 3.141528579238; 
cout << a << " " << b;
return 0;
}

我得到这个作为我的输出

3.14153 3.14153

您面临的问题是,首先您应该知道 float 有 4 个字节,double 有 8 个字节,3.141528579238 也许可以存储在两者中,如果您想显示更多数字,我建议首先包括: #include <iomanip>,您可以使用以下命令在"."之后打印任意数量的数字:cout << std::setprecision(7) << myFloat;这将显示 3.1415285。

这不是关于数字,而是关于位!

float s 是

32bitdouble s 是 64bit 。浮点数可以这样表示:a*2^x a < 1a => 0 的地方。 ax将存储在32- or 64-bit内存中。

您的

程序打印相同内容的原因是因为它不打印您的整数。