ruby - How come 0.7 - 0.5 - 0.2 => -5.551115123125783e-17? -


i have no idea how deal problems this:

0.7. - 0.5. - 0.2 # => -5.551115123125783e-17 

an article links what every computer scientist should know floating-point arithmetic , the floating-point guide. describe why approximation, , therefore inaccuracy, unavoidable. still have no idea. explanation of why result remote expected why calculator problem helpful understanding of problem.

as have worked out resources link floating point numbers approximation. right floating point numbers going have inaccuracies representation.

in cases these small inaccuracies end cancelling out when performing calculations. e.g. larger 5 + smaller 3 8.

the accuracy floating point can represent values varies depending on values themselves. numbers between -1 , 0 , 0 , 1 more difficult represent accurately closer 0. numbers outside range less accurate further away 0 are.

the case have shown 1 inaccuracies in representing .7, .5 , .2 have added larger inaccuracy in result.

as calculators, calculators don't use binary floating point numbers. use either arbitrary precision fixed point ones, or decimal floating point instead. fixed point numbers don't have kind of inaccuracies of precision floating point numbers do. comes @ expense of range of numbers capable of representing though. decimal floating point doesn't have same kind of error of representation, doesn't have convert numbers decimal binary before storing them. can still suffer rounding errors though.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -