By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 440,551 Members | 1,142 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

# round function problem

 P: n/a Hi everybody... We try to white scripts with Pyrhon 2.4 for an acoustic simulation and we wrote these follow lines : 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 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 Sep 6 '05 #1
Share this Question
3 Replies

 P: n/a mg wrote: 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 Sep 6 '05 #2

 P: n/a mg wrote: 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: 10/3. 3.3333333333333335 round(_,1) 3.2999999999999998 print _ 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)) Sep 6 '05 #3

 P: n/a On Tue, 06 Sep 2005 09:27:48 +0200, mg wrote: Hi everybody...We try to white scripts with Pyrhon 2.4 for an acoustic simulation andwe wrote these follow lines :c = 340 340 is an integer, which is different from 340.i =j=k= 1sum_ = 23 also an integertable = []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 freqtable.append([freq,(i,j,k)])print i,j,k,' freq',freqfor item in table: print itemThe problem is simple. The function 'round' allow to obtain the valuewith the specified number of digits after the ",". Then, when thevariable 'freq' is printed, there is no problem; but when freq is set inthe 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 Sep 7 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion.