Python adds extra to result -


this question has answer here:

i using python add simple numbers together, , got strange result

>>> 16+4.8+6.4+0.48+1.92+.48 30.080000000000002 

that not right answer, why python add bit? how reliable python more complicated computations?

the culprit floating point math.

floating point math:

your language isn't broken, it's doing floating point math. computers can natively store integers, need way of representing decimal numbers. representation comes degree of inaccuracy. that's why, more not, .1 + .2 != .3. why happen?

it's pretty simple. when have base 10 system (like ours), can express fractions use prime factor of base. prime factors of 10 2 , 5. 1/2, 1/4, 1/5, 1/8, , 1/10 can expressed cleanly because denominators use prime factors of 10. in contrast, 1/3, 1/6, , 1/7 repeating decimals because denominators use prime factor of 3 or 7. in binary (or base 2), prime factor 2. can express fractions cleanly contain 2 prime factor. in binary, 1/2, 1/4, 1/8 expressed cleanly decimals. while, 1/5 or 1/10 repeating decimals. 0.1 , 0.2 (1/10 , 1/5) while clean decimals in base 10 system, repeating decimals in base 2 system computer operating in. when math on these repeating decimals, end leftovers carry on when convert computer's base 2 (binary) number more human readable base 10 number.

source: http://0.30000000000000004.com/


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -