round() function strange behaviour

brobbins333 at shaw.ca brobbins333 at shaw.ca
Mon Mar 11 13:51:01 EST 2002


I understand and agree with what you  are saying. However, it is
possible in some languages (VB, for example) to return a numerical
result from a function with only two (or any other number) of decimal
places. Not a formatted string representation, but an actual numerical
value that can be used in further calculations. This value is, of
course, an approximation and has really been truncated rather than
"rounded."

Anyway, we are arguing now over how many angels can dance on the head
of a pin. I understand the problem and can work around it. This group
is a great resource. Thanks for all your help and time.

Bruce

On Sun, 10 Mar 2002 16:31:51 GMT, grante at visi.com (Grant Edwards)
wrote:

>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