round function problem

Bengt Richter bokr at oz.net
Wed Sep 7 02:57:10 EDT 2005


On Tue, 06 Sep 2005 09:27:48 +0200, mg <mg.mailing-list at laposte.net> wrote:

>Hi everybody...
>
>We try to white scripts with Pyrhon 2.4 for an acoustic simulation and 
>we wrote these follow lines :
>
><begin script>
>c = 340
340 is an integer, which is different from 340.
>i =j=k= 1
>sum_ = 23
also an integer
>table = []
>freq = round((c/2*(sum_)**0.5),2)
c/2 is integer/integer, which is not the same as c/2.0
rules all-integer arimetic generally produces integer results,
so (unless you import division from __future__) pay attention or
write your constants with decimal points even if the values are exact integers.
note:
 >>> 5/2
 2
 >>> from __future__ import division
 >>> 5/2
 2.5

>print freq
>table.append([freq,(i,j,k)])
>print i,j,k,' freq',freq
>for item in table: print item
><end script>
>
>The problem is simple. The function 'round' allow to obtain the value 
>with the specified number of digits after the ",". Then, when the 
>variable 'freq' is printed, there is no problem; but when freq is set in 
>the table and the table printed, all the digits are come back.
>
>Is it a bug or a control behavour ? I don't understand ?!?!?!?!...
>
Others have pointed out the output formatting and binary representation
problems behind your question, but I thought you might not realize the above.

Regards,
Bengt Richter



More information about the Python-list mailing list