精度 强制转换 浮点数

原创 2015年11月18日 11:09:39
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


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

双精度浮点数转换

单精度双精度浮点数转换

  • 2013-05-02 12:36
  • 1.69MB
  • 下载

C++ 字符串转换为浮点数时的精度问题

#include /*库文件包含*/ #include /*用于字符串操作*/ #include /*用于exit函数*/ /************************************...

关于浮点数的精度问题

  • 2013-11-03 14:30
  • 38KB
  • 下载

javascript面试题之浮点数精度问题

首先,在javascript中是不区分整数和浮点数的,所以console.log(1/3)//0.3333333333333333由于,javascript采用了IEEE-754浮点数表示法,这是一种...

高精度浮点数计算器

  • 2015-06-12 12:26
  • 703KB
  • 下载

java中floatdouble浮点数的计算失精度问题

java中浮点数的计算今天在数值计算时碰到一个问题.程序如下:  double a = (3.3-2.4)/0.1;  System.out.println(a);你可能认为结果很简单,不就是9嘛,是...

高精度浮点数幂指运算

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)