为什么float和double变量存储的位数相同,double不应该比float存储更多的位数吗
#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。
关于c++ - float 和 double 变量中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38042442/