Why not FP for Money?
Michael Hoffman
m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Fri Sep 24 16:20:13 EDT 2004
Paul Rubin wrote:
> danb_83 at yahoo.com (Dan Bishop) writes:
>
>>str(x), however, is meant to return a "nice" string representation,
>>and so it only uses '%.12g' % x (ignoring 5 "noise" digits at the end
>>of repr(x)).
>
> Thanks. I didn't realize that 'nice' means 'inaccurate':
No, in this case, "nice" means "less precise." You frequently cannot get
the kind of accuracy in floating point numbers that you seem to want.
> >>> a=1e12+1
> >>> b=a-1e12
> >>> b
> 1.0
> >>> str(a)
> '1e+12'
> >>> float(str(a))-1e12
> 0.0
If you had chosen different values, you would have gotten differing
results even without a str/float conversion:
>>> a=1e11+0.1
>>> b=a-1e11
>>> b
0.100006103515625
>>> str(a)
'100000000000.0'
>>> float(str(a))-0.1
99999999999.899994
>>> 0.1
0.10000000000000001
Fun!
--
Michael Hoffman
More information about the Python-list
mailing list