关闭

精度 强制转换 浮点数

296人阅读 评论(0) 收藏 举报
分类:
int main(){
char *a0="0.89";
  char *a1="1.89";
  char *a2="2.89";
  char *a3="3.89";
  char *a4="4.89";
  char *a5="5.89";
  char *a6="6.89";
  char *a7="7.89";
  char *a8="8.89";
  char *a9="9.89";
  
  long A0=atof(a0)*100;
  long A1=atof(a1)*100;
  long A2=atof(a2)*100;
  long A3=atof(a3)*100;
  long A4=atof(a4)*100;
  long A5=atof(a5)*100;
  long A6=atof(a6)*100;
  long A7=atof(a7)*100;
  long A8=atof(a8)*100;
  long A9=atof(a9)*100;
  
  


  printf("%.16lf\n",atof(a0));
  printf("%.16lf\n",atof(a1));
  printf("%.16lf\n",atof(a2));
  printf("%.16lf\n",atof(a3));
  printf("%.16lf\n",atof(a4));
  printf("%.16lf\n",atof(a5));
  printf("%.16lf\n",atof(a6));
  printf("%.16lf\n",atof(a7));
  printf("%.16lf\n",atof(a8));
  printf("%.16lf\n",atof(a9));
  
  printf("*******\n");
 
  printf("%.16lf\n",atof(a0)*100);
  printf("%.16lf\n",atof(a1)*100);
  printf("%.16lf\n",atof(a2)*100);
  printf("%.16lf\n",atof(a3)*100);
  printf("%.16lf\n",atof(a4)*100);
  printf("%.16lf\n",atof(a5)*100);
  printf("%.16lf\n",atof(a6)*100);
  printf("%.16lf\n",atof(a7)*100);
  printf("%.16lf\n",atof(a8)*100);
  printf("%.16lf\n",atof(a9)*100);


  printf("*******\n");


  printf("%ld\n",A0);
  printf("%ld\n",A1);
  printf("%ld\n",A2);
  printf("%ld\n",A3);
  printf("%ld\n",A4);
  printf("%ld\n",A5);
  printf("%ld\n",A6);
  printf("%ld\n",A7);
  printf("%ld\n",A8);
  printf("%ld\n",A9);

  }


0.8900000000000000
1.8899999999999999
2.8900000000000001
3.8900000000000001
4.8899999999999997
5.8899999999999997
6.8899999999999997
7.8899999999999997
8.8900000000000006
9.8900000000000006
*******
89.0000000000000000
189.0000000000000000
289.0000000000000000
389.0000000000000000
488.9999999999999432
589.0000000000000000
689.0000000000000000
789.0000000000000000
889.0000000000000000
989.0000000000000000
*******
89
189
289
389
488
589
689
789
889
989


3.89
4.89
5.89

为例说明


转换为double类型,在内存中
1 11 52


计算出0.89的60位的表示
111000111101011100001010001111010111000010100011110101110000

100位的表示

1110001111010111000010100011110101110000101000111101011100001010001111010111000010100011110101110000



3.89
11.111000111101011100001010001111010111000010100011110101110000
1.1111000111101011100001010001111010111000010100011110101110000*2^1
因为首位肯定是1,所以第一个1不写。
正数
0
指数是1
00000000001
尾数
1111000111101011100001010001111010111000010100011110
在内存中
0000000000011111000111101011100001010001111010111000010100011110


4.89
1.00111000111101011100001010001111010111000010100011110101110000*2^2
0 00000000010 0011100011110101110000101000111101011100001010001111


5.89
1.01111000111101011100001010001111010111000010100011110101110000*2^2
0 00000000010 0111100011110101110000101000111101011100001010001111



3.8900000000000001
4.8899999999999997
5.8899999999999997

可以看出在内存中使用二进制表示后,无法准确表示。

*100后,转为二进制,代表的值是:

389.0000000000000000
488.9999999999999432
589.0000000000000000

很不幸,强制类型转换是向下转换的,

488.9999999999999432就是488


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:484075次
    • 积分:6683
    • 等级:
    • 排名:第3509名
    • 原创:32篇
    • 转载:1006篇
    • 译文:1篇
    • 评论:12条
    最新评论