round() function strange behaviour
Erik de Castro Lopo
nospam at mega-nerd.com
Sat Mar 9 18:13:50 EST 2002
brobbins333 at shaw.ca wrote:
>
> the round() function will sometimes return a predictable result, as
> in:
>
> round(45.67891, 2)
> >>> 45.68
>
> but sometimes it will return something like:
>
> round(56.78923, 2)
> >>> 56.789999999999999
>
> 2.1 and 2.2 both behave this way on two different (Windows) computers.
> What's going on here?
You are running into problems with the floating point representation
of numbers. You may want to look up how floating point numbers are
stored in computers. Search google.
What you will fins is that some numbers like 2, 0.25 etc can be
represented exactly in floating point format while others like
2/3 cannot.
Thats just the way it is.
Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo nospam at mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
BSD: A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts. Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.) The full chemical name is "Berkeley Standard Distribution".
More information about the Python-list
mailing list