C/C中浮动和双精度的区别++

为了表示浮点数,我们使用 浮动 , 双重的 长双人 .

null

有什么区别?

双重的 它的精确度是原来的2倍 浮动 .

浮动 是一个32位的IEEE 754单精度浮点数字,1位表示符号(8位表示指数,23*表示值),即浮点有7位十进制精度。

双重的 是一个64位IEEE 754双精度浮点数(符号为1位,指数为11位,值为52*位),即双精度有15位小数。

让我们举个例子 在这里 ) : 对于一个二次方程 x2–4.0000000 x+3.999999=0 ,10位有效数字的精确根为,r1=2.000316228和r2=1.999683772

// C program to demonstrate
// double and float precision values
#include <stdio.h>
#include <math.h>
// utility function which calculate roots of
// quadratic equation using double values
void double_solve( double a, double b, double c){
double d = b*b - 4.0*a*c;
double sd = sqrt (d);
double r1 = (-b + sd) / (2.0*a);
double r2 = (-b - sd) / (2.0*a);
printf ( "%.5f %.5f" , r1, r2);
}
// utility function which calculate roots of
// quadratic equation using float values
void float_solve( float a, float b, float c){
float d = b*b - 4.0f*a*c;
float sd = sqrtf(d);
float r1 = (-b + sd) / (2.0f*a);
float r2 = (-b - sd) / (2.0f*a);
printf ( "%.5f %.5f" , r1, r2);
}
// driver program
int main(){
float fa = 1.0f;
float fb = -4.0000000f;
float fc = 3.9999999f;
double da = 1.0;
double db = -4.0000000;
double dc = 3.9999999;
printf ( "roots of equation x2 - 4.0000000 x + 3.9999999 = 0 are : " );
printf ( "for float values: " );
float_solve(fa, fb, fc);
printf ( "for double values: " );
double_solve(da, db, dc);
return 0;
}


输出:

roots of equation x2 - 4.0000000 x + 3.9999999 = 0 are : 
for float values: 
2.00000    2.00000
for double values: 
2.00032    1.99968

本文由 曼迪星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享