[Pythonmac-SIG] round() not consistent?
israel at uandmedance.com
israel at uandmedance.com
Fri May 28 18:05:37 EDT 2004
Thanks Bob and Calvin
I would have though round() would cut off those extra digits, but I'll
go and do my freshly assigned homework and educate myself. :)
Thanks again,
~Israel~
On Friday, May 28, 2004, at 01:47 PM, Bob Ippolito wrote:
>
> On May 28, 2004, at 2:41 PM, israel at uandmedance.com wrote:
>
>>
>> hello everyone..
>>
>> I've been playing around with generating some random numbers with
>> random.random and then rounding them off to two decimal points with
>> round() but for some reason this doesn't always work.
>>
>> here is a sample session
>> '''
> ---
>> >>> round(random.random(),2)
>> 0.57999999999999996
> ---
>> Am I just not understanding how things work here?
>
> You are not understanding how things work here. Floating point
> numbers can not exactly represent all decimal numbers. What you are
> looking at is the __repr__ of the floating point return values, which
> shows the precise value of what the float actually contains so that it
> can be used to reconstruct the exact value.
>
> The __str__ of those values would be what you expect to see as a
> 'human'. Try str(0.57999999999999996), and you will see '0.58'.
>
> Google for "ieee 754", and you will find all the information you ever
> wanted to know about floating point numbers (as implemented by typical
> computers and compilers). To be more specific, Python's float
> corresponds to C's double, which is 64bits wide on almost all
> platforms.
>
> -bob
>
>
> _______________________________________________
> Pythonmac-SIG maillist - Pythonmac-SIG at python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
>
>
~Israel~
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 1705 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20040528/047cb85e/attachment.bin
More information about the Pythonmac-SIG
mailing list