我想要下面的程序做的就是打印所有小于 30 的正数的乘积(以指数和非指数形式)。当变量product
声明为 afloat
或 a时它工作正常double
,但当类型为 时会产生完全荒谬(否定)的结果long double
。那么请回答由此引发的这两个问题:
为什么
long double
产生完全荒谬(甚至负面)的结果,而float
变量double
的类型product
产生正确的结果?我有这个概念,
long double
它只不过是 的“高容量”版本double
,它本身就是float
类型的“高容量”版本!现在对于产生正确结果的类型
product
,即float
和double
,为什么它们以指数形式(%e
)产生相同的输出,但对于非指数形式(%f
)产生明显不同的输出?
#include<stdio.h>
int main(void)
{
int j;
float product=1; //Works fine
//double product=1; //Works fine
//long double product=1; //Produces absurd output.
for(j=2;j<=30;j=j+2)
product=product*j;
printf("The product of even numbers <30 is %e \n",product);
printf("The product in non-exponential form is %f",product);
}
product
作为浮点数的输出
The product of even numbers <30 is 4.284987e+16
The product in non-exponential form is 42849875099910144.000000
product
双倍输出
The product of even numbers <30 is 4.284987e+16
The product in non-exponential form is 42849873690624000.000000
输出product
只要双倍
The product of even numbers <30 is -6.078565e-192
The product in non-exponential form is -0.000000