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 <stdio.h>#include <math.h>// utility function which calculate roots of// quadratic equation using double valuesvoiddouble_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 valuesvoidfloat_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 programintmain(){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.00000for double values: : 2.00032 1.99968通过以上实例,你掌握了这两个数据类型的不同之处了吗?希望对你有帮助哦~
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)