小编ato*_*tom的帖子

double和long double:我的电脑上是否相同?

这是ANSI C代码:

   float        x = 3.14159264351134890172;
   double       y = 3.14159264351134890172;
   long double  z = 3.14159264351134890172;

   printf("%f\n",x);
   printf("%f\n",y);
   printf("%f\n",z);
   printf("%.20f\n",x);
   printf("%.20f\n",y);
   printf("%.20f\n",z);
Run Code Online (Sandbox Code Playgroud)

此代码的输出是:

3.141593
3.141593
3.141593
3.14159274101257324219
3.14159264351134881466
3.14159264351134881466
Run Code Online (Sandbox Code Playgroud)

见最后一行输出.我读过,long double给出了小数点后19位的准确度.但是在这里我得到小数点后的15位精度.所以在我的电脑doublelong double电脑之间没有区别.我在2.6.32-358.el6.x86_64CentOS上使用linux内核.我的C编译器是gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

为什么会这样?如果它取决于计算机硬件/架构,我如何购买我的linux机器,其中long double比double更精确?它还依赖于我的C编译器吗?如果是,我如何选择我的C编译器?

对于float价值范围是1.2E-38 to 3.4E+38.我不明白.从上面的例子我们看到我们无法在float变量中正确存储分数十进制数,例如3.1415926 .我的机器只能在该float变量中正确存储3.141592 .所以我丢失了最后7位数,这是6.那为什么我们说float范围是1.2*10 ^ -38?

是不是我们不能存储到10 ^ -38但是我们可以float在C中存储最多10 ^ -6的变量?

c floating-point gcc numerical-methods

1
推荐指数
1
解决办法
139
查看次数

标签 统计

c ×1

floating-point ×1

gcc ×1

numerical-methods ×1