浮点数和双变量中的数字
Digits in float and double variable
为什么浮点数和双变量存储的位数相同,双精度不应该存储比浮点数更多的数字
#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 是32bit
,double
s 是 64bit
。浮点数可以这样表示:a*2^x
a < 1
和 a => 0
的地方。 a
和x
将存储在32- or 64-bit
内存中。
程序打印相同内容的原因是因为它不打印您的整数。
相关文章:
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 使用指针选择长无符号变量中的数字
- 如果静态变量只为程序的整个部分存储了一个副本,为什么我不能使用静态变量交换 2 个数字?
- 如何使用C++中不是文字的变量在数字中显示单引号和两个引号?假设 6'2" 英尺
- 常量整数变量和数字的不同类型推导
- 运行时检查失败 #2 - 变量"数字选择"周围的堆栈已损坏
- 在 c++ 中向数组变量添加一个数字
- 在Visual Studio中生成的匿名命名空间变量的损坏名称的数字部分是什么意思
- 为什么 al_draw_textf() 打印数字而不是存储在字符串变量中的字母
- 使用模量来找出数字是否可以除去不起作用的变量
- 如何判断变量是否是一个复杂的数字C
- 在 c++ 中不能将超过 10 位数字放入一个变量中
- 使用单个数字表示多个变量
- 如何在变量中仅允许数字
- 如何使用同一变量输入字母和数字?(C )
- 打印长双变量的所有十进制数字
- 尝试将数字放入 ifstream 文件中的变量中
- 如何调用一个变量(包含rand()函数)并得到不同的数字
- 为循环嵌套的变量快速生成数字组合
- 如何在C++中显示数字变量