When I do floating point operation, why some unwanted result comes
来源:互联网 发布:怎么和淘宝达人合作 编辑:程序博客网 时间:2024/05/21 15:42
Q. When I do floating point operation, why some unwanted result comes
out?
// Example, why the red extra comes 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 is represented 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, unexperienced programmer situation will be exposed to the interviewer immediately.
When you output, format it to what you desired. How the precision changed? This 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 converts to binary, then no problems. It will be 11101.001 in binary precisely, since 0.125 is 2-3 or 1/8
out?
// Example, why the red extra comes 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 is represented 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, unexperienced programmer situation will be exposed to the interviewer immediately.
When you output, format it to what you desired. How the precision changed? This 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 converts to binary, then no problems. It will be 11101.001 in binary precisely, since 0.125 is 2-3 or 1/8
- When I do floating point operation, why some unwanted result comes
- invaild floating point operation
- Why Floating-Point Numbers May Lose Precision
- a weird result when I try to print the value of point by %p format
- Why do I have hundreds of child cursors when cursor_sharing set to similar in 10g
- Why do I get "Value computed is not used" when working with pointers?
- 关于invalid floating point operation的一些参考资料
- delphi “Invalid floating point operation.”错误的解决方法
- TWebbrowser报invalid floating point operation错误解决办法
- delphi2007:"Invalid floating point operation" 错误的解决方法
- TWebbrowser报invalid floating point operation错误解决办法
- TWebbrowser报invalid floating point operation错误解决办法
- C++ invalid floating point operation(无效的浮点操作)
- 解决提示:Invalid floating point operation.无效的浮点运算
- Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C st
- Why do I get java.lang.IllegalStateException?
- Why do I need a passphrase?
- Some tips I get when learning bash
- 什么是COM组件
- gnuplot绘制的图像的输出
- 由Java实现简单的文本编辑器
- 互联网的变革 – WEB2.0
- 经典面试问题回答思路
- When I do floating point operation, why some unwanted result comes
- 超线程技术与双通道技术
- 简历制作诀窍谈
- java第六课
- 面试全攻略
- 调试程序DEBUG命令说明
- 误入ADF
- 试用SortedList 类
- 湖大软件学院电子院刊3