Python rounding error with float numbers -
this question has answer here:
- is floating point math broken? 20 answers
i don't know if obvious bug, while running python script varying parameters of simulation, realized results delta = 0.29 , delta = 0.58 missing. on investigation, noticed following python code:
for i_delta in range(0, 101, 1): delta = float(i_delta) / 100 (...) filename = 'foo' + str(int(delta * 100)) + '.dat'
generated identical files delta = 0.28 , 0.29, same .57 , .58, reason being python returns float(29)/100 0.28999999999999998. isn't systematic error, not in sense happens every integer. created following python script:
import sys n = int(sys.argv[1]) in range(0, n + 1): = int(100 * (float(i) / 100)) if != a: print i,
and can't see pattern in numbers rounding error happens. why happen particular numbers?
any number can't built exact powers of 2 can't represented floating point number; needs approximated. closest approximation less actual number.
read what every computer scientist should know floating-point arithmetic.
Comments
Post a Comment