Lo que todo programador debería saber sobre aritmética de punto flotante
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Improve intro comment on rounding error cancels

authored by

Greg Smith and committed by
Michael Borgwardt
5aacd89a a7e18d84

+5 -2
+5 -2
content/basic.html
··· 34 34 ### Why do other calculations like 0.1 + 0.4 work correctly? 35 35 36 36 In that case, the result (0.5) *can* be represented exactly as a floating-point number, 37 - and the errors in the input numbers cancel each other out. 37 + and it's possible for rounding errors in the input numbers to cancel each other out. 38 + Even that can't necessarily be relied upon though. If for example those two numbers 39 + were stored in differently sized floating point representations first, and then added 40 + together, the rounding errors might not have offset each other perfectly. 38 41 39 42 In other cases like 0.1 + 0.3, the result actually isn't *really* 0.4, but close enough that 0.4 40 43 is the shortest number that is closer to the result than to any other floating-point number. Many languages then display that number instead of converting the actual result back to the closest 41 - decimal fraction. 44 + decimal fraction.