浮点精度

来源:互联网 发布:wpf高级编程 pdf 网盘 编辑:程序博客网 时间:2024/04/28 07:44
#include <iostream>#include<cstdio>using namespace std;int main(){    int a=0;    double k=1;    while(k>0){        k=k/10;        a++;    }    printf("%d\n",a);    return 0;}

输出a=324,但这并不是精度

#include <iostream>#include<cstdio>using namespace std;int main(){    int a=0;    float k=1;    while(k>0){        k=k/10;        a++;    }    printf("%d\n",a);    return 0;}

输出46,也不是精度

类型    符号位   阶码   尾数   长度  2的尾数次方                                       精确位数                                  指数范围
float           1      8     23       32        8388608                                                 6                                       -128~127   
double       1     11    52       64       4503599627370496                               15                                      -1024~1023
临时数       1     15    64       80      18446744073709551616                         19                                     -32768~32767

#include <iostream>#include<cstdio>using namespace std;int main(){    freopen("out","w",stdout);    int a=0;    double k=1;    while(k>0){        k=k/10;        a++;        printf("%d\n%.324lf\n",a,k);    }    return 0;}
从下面的运行结果看,精度为15

#include <iostream>#include<cstdio>using namespace std;int main(){    freopen("out","w",stdout);    int a=0;    float k=1;    while(k>0){        k=k/10;        a++;        printf("%d\n%.324lf\n",a,k);    }    return 0;}
考虑到计算的浮点误差,在合理的范围内,9的个数稳定在6

参考:http://blog.csdn.net/lai123wei/article/details/7220684

0 0
原创粉丝点击