Possible Duplicate:
python limiting floats to two decimal points
If I make this: 12.45-12
in Python the answer I get is: 0.44999999999999929
I do I do it to make it: 0.45?
BTW I did remember to do: 12.45-12.0 But no result.
Possible Duplicate:
python limiting floats to two decimal points
If I make this: 12.45-12
in Python the answer I get is: 0.44999999999999929
I do I do it to make it: 0.45?
BTW I did remember to do: 12.45-12.0 But no result.
This is a result of the fact that many decimals cannot be exactly represented in binary.
For example, 0.25 can: it's 0.01
(0*1, 0*1/2, 1*1/4). 0.1 can't (0.0001100110011...
), just like you can't write 1/3 as a complete decimal (0.3333333333...
).
If you do
print(12.45-12)
you get
0.45
because print
only displays the first significant digits.
See the Python docs for an excellent summary.
If you do care about decimal values being exact (for example to avoid a Superman III scenario in a financial institution), look at the Decimal module.
It's perfectly OK, but you need to google (and search around on StackOverflow) for "IEEE 754".
EDIT: Take a look e.g. here: Inaccurate Logarithm in Python or here: Another floating point question