Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 [个人原创]Why some unwanted result comes out in floating-point operation?
ranchgirl



版主


发贴: 801
积分: 132
于 2005-07-31 10:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
See a question in Java基础, copy my own article here written quite while ago.

Q. When I do floating-point operation, why some unwanted result comes out?


// Example, why do the digits 000000000003 extra after 20.255 come out?
double x = 27.475;
double y = 7.22;
System.out.println(x - y); // 20.255000000000003

A: That is a famous floating-point phenomenon. Your number is represented in decimal, and your computer represents them in binary, some discrepancy will happen with that conversion back-and-forth. It is normal since Machine Language / Assembly / Fortran time. If you go to an interview, someone ask you this question, you don't know the answer, and inexperienced programmer situation will be exposed to the interviewer immediately.

When you output, format it to what you desired.

How the precision changed? Explanation is no easy job.
You need understand what is radix.

Radix for decimal is 10
Radix for binary is 2

29.3 in decimal is actually
2 * 101 + 9 * 100 + 3 * 10-1
In binary
11101.010011001.............
Do the same thing, but replace 10 to 2.

However, the number will never end. You will find out something is short in decimal may become infinite long in binary. And the opposite will not be infinite long, but possible longer then the computer system allowed. Computer cannot allow infinite long number, it must be truncated to fit the precision of the floating-point number in the system.

Therefore, the value of some floating-point number changes in the conversion from one radix to another radix back-and-forth.

If 29.125 in decimal is converted to binary, there are no problems. It will be 11101.001 in binary precisely, since 0.125 is 2-3 or 1/8. Smile


gongshi edited on 2005-07-31 15:07

作者 Re:[个人原创]Why some unwanted result comes out in floating-point operation? [Re:gongshi]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-07-31 15:14 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
My original article was in xhtml, when I converted it to jute format, made a few mistakes. Now, I think they are fixed.

This is mostly math in computing problem. Real numbers in math are continuous. However, real numbers in computer must be discrete.

Theoretically, this kind unwanted results are inavoidable, no matter how accurate your computer is or will be in the future.

You'd better understand it. Thanks!


gongshi edited on 2005-07-31 15:26


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923