float多少位小数(float有效数字七位详解)

为了表示浮点数,我们使用floatdoublelong double有什么不同 ?

double的精度是float的2

float是32位IEEE 754单精度浮点数1位符号,(8位为指数,23 *为值),即float具有7位十进制数字精度。

double是64位的IEEE 754双精度浮点数(符号1位,指数11位,值52 *位),即double具有15位十进制数字的精度。

让我们举个例子:

对于二次方程x2 – 4.0000000 x + 3.9999999 = 0,精确到10个有效数字的根是r1 = 2.000316228和r2 = 1.999683772

// C program to demonstrate

// double and float precision values

#include

#include

// utility function which calculate roots of

// quadratic equation using double values

voiddouble_solve(doublea, doubleb, doublec){

doubled = b*b - 4.0*a*c;

doublesd = sqrt(d);

doubler1 = (-b + sd) / (2.0*a);

doubler2 = (-b - sd) / (2.0*a);

printf("%.5ft%.5fn", r1, r2);

}

// utility function which calculate roots of

// quadratic equation using float values

voidfloat_solve(floata, floatb, floatc){

floatd = b*b - 4.0f*a*c;

floatsd = sqrtf(d);

floatr1 = (-b + sd) / (2.0f*a);

floatr2 = (-b - sd) / (2.0f*a);

printf("%.5ft%.5fn", r1, r2);

}

// driver program

intmain(){

floatfa = 1.0f;

floatfb = -4.0000000f;

floatfc = 3.9999999f;

doubleda = 1.0;

doubledb = -4.0000000;

doubledc = 3.9999999;

printf("roots of equation x2 - 4.0000000 x + 3.9999999 = 0 are : n");

printf("for float values: n");

float_solve(fa, fb, fc);

printf("for double values: n");

double_solve(da, db, dc);

return0;

}

输出:

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

通过以上实例,你掌握了这两个数据类型的不同之处了吗?希望对你有帮助哦~

© 版权声明
评论 抢沙发
加载中~
每日一言
不怕万人阻挡,只怕自己投降
Not afraid of people blocking, I'm afraid their surrender