linux浮点数四舍五入还是五舍六入…

上午娃娃问我一个问题:
awk里面   printf("%.2f",test);一个浮点数,比如test=0.725,相同的代码,在不同的集群上跑出来结果不一样,有的打印0.72,有的打印0.73,这是什么原因造成的
在网上搜了一下,一时半会儿没找到答案。下午的时候,用“awk printf   五舍六入”作为关键字,谷沟了一下,《bash 保留小数问题》http://bbs.chinaunix.net/thread-1188475-1-1.html,这篇帖子,基本解答了这个问题,重点是9楼的回答“ 各种软件从字串转化到浮点数的结果不同”。看完帖子,找了8台服务器,执行 /usr/bin/printf "%.20f\n" .895,结果如下:
内核版本为2.6.9、printf版本为5.2.1的3台:   0.89500000000000001776
内核版本为2.6.18、printf版本为5.97的5台:   0.89499999999999999998
用uname -a查看系统信息,用“/usr/bin/printf --version”命令查看printf的版本,结果如下:
操作系统内核版本     硬件平台(hardware platform )  printf版本
2.6.9-67.ELsmp       i386                         printf (GNU coreutils) 5.2.1
2.6.9-42.ELsmp       i386                         printf (GNU coreutils) 5.2.1
2.6.9-42.ELsmp       i386                         printf (GNU coreutils) 5.2.1
2.6.18-194.el5PAE    i386                         printf (GNU coreutils) 5.97
2.6.18-92.el5PAE     i386                         printf (GNU coreutils) 5.97
2.6.18-164.el5       x86_64                       printf (GNU coreutils) 5.97
2.6.18-164.el5       x86_64                       printf (GNU coreutils) 5.97
2.6.18-194.el5       x86_64                       printf (GNU coreutils) 5.97

后来跟娃娃交流,他说跟浮点数的表示形式有关,以下是一段说明:
It is precisely related to the fact that an "exact" decimal fraction
can almost never be expressed exactly as a binary floating-point number.
So when you write 3.5135 the computer will express it as either 117893496
* 2^-25 or 117893497 * 2^-25. One of these values is slightly less than
3.5135, the other is slightly greater. Similarly, the round-off value of
0.0005 to be added to this will have some binary approximation which is
not exactly equal to 0.0005. And the sum will be a binary number that
you will then have to find the nearest decimal approximation to.

Kernighan and Plauger:
"Floating point numbers are like piles of sand; every time you move
one you lose a little sand and pick up a little dirt."

看来四舍五入还是五舍六入,要看该浮点数在具体的环境下是如何表示的,应该跟系统或者软件版本有关系吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值