round function problem

M

mg

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
i =j=k= 1
sum_ = 23
table = []
freq = round((c/2*(sum_)**0.5),2)
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 ?!?!?!?!...

Thanks

Mathieu
 
G

Gregory Bond

mg said:
Is it a bug or a control behavour ? I don't understand ?!?!?!?!...

It's a case of applying different float-to-text rounding in different
situations, on a variable that (even after round()) is not representable
in binary floatingpoint.

"print var" does one sort of string conversion (using str()), "print
[var]" does another type (using repr() on the list element). The
underlying value is the same, it's just printed differently.
var = round(0.5**0.5, 2)
var 0.70999999999999996
print var 0.71
print [var] [0.70999999999999996]
print str(var) 0.71
print repr(var) 0.70999999999999996
 
M

Magnus Lycka

mg said:
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 ?!?!?!?!...

There are two things to consider here:

First of all, a floating point number is binary in nature, and
a rounded floating point number is still a binary number, not
a base ten number. E.g:
3.3

See? The internal representation of the number roughly consists of a
mantissa and an exponent, so that the mantissa times two to the power
of the exponent gives you the number. Just as you can't represent ten
thirds exactly as a finite decimal number (3.333333333...), you can't
represent 3.3 exactly as a binary floating point number.
3.2999999999999998 is about as close as you get (this is still rounded).

The result of the round() function is still a floating point number,
it's not a string of text or a number internally represented with base
10.

Second, you need to understand that there are two ways of converting
every Python object to a sting representation. You can read about the
str() and repr() functions in the library manual, chapter 2.

When you print a list, you use repr(), which shows you lots of
significant digits for floats.

You can solve your immediate problem by changing "print item" to
print "map(str,item)" or something like that. Perhaps you prefer
print ", ".join(map(str,item))
 
B

Bengt Richter

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: 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,264
Messages
2,571,315
Members
48,000
Latest member
SusannahSt

Latest Threads

Top