round() function strange behaviour

Grant Edwards grante at visi.com
Sun Mar 10 11:31:51 EST 2002


In article <3c8b8112.27836128 at news>, brobbins333 at shaw.ca wrote:

> OK, that makes perfect sense. In my case, I was simply looking
> for a predictable way to return a number with two decimal
> places

That's just not possible.  The "number" you're talking about
doesn't _have_ decimal places.  It's a binary floating point
number. It has "binary places".  In theory you could write a
function that rounds to N binary places, but I don't know how
useful that is going to be.

In order to talk in a meaningful way about decimal places, you
have to use a base-10 representation: BCD floating point or BCD
fixed point.  Strings such as "123.456" are sort-of BCD
floating point, and you can round to N decimal places when
converting from binary floating point to "String/BCD FP":

  f = 123.4567
  s = "%0.2f" % f

>>> f = 123.456
>>> s = "%0.2f" % f
>>> f
123.456
>>> s
'123.46'

"String/BCD FP" as I've termed it can represent numbers just
fine, but since you can't do any mathematical operations on them
it's of limited value.

Since you're using binary floating point, the round() function
is actually converting it's binary floating point input value
to the closest value to X such that if X were converted to BCD
in the future it will have the requested number of decimal
places.  But, X may not be exactly representable. If you're
using round(x,N) your probably just fooling yourself. 

If you _really_ do want a number with N decimal places: 

the topic of base-10 numbers for Python comes up occasionally,
and I believe that there are BCD packages available.  If you
really want to do your calculations in base 10, you might want
to search the newsgroup archives for "BCD".

-- 
Grant Edwards                   grante             Yow!  Please come home with
                                  at               me... I have Tylenol!!
                               visi.com            



More information about the Python-list mailing list