PEP proposal for round(x,n) enhancement

Markus Schaber markus at schabi.de
Tue Sep 11 16:25:51 EDT 2001


Hi,

Chris Barker <chrishbarker at home.net> schrub:

>> > The enhancement that I would like to see is the ability to indicate
>> > the number of digits counting *from the 1st non-zero digit* that
>> > should be
>> > reported.  These digits are often called the significant digits.
>> I'm curious to know what problem domain this is addressing. And given
> 
> This is a VERY common need for those of us doing numerical/scientific
> programming. round is just fine if you know the expected magnitude of
> the numbers in question, but when you don't, you really need some kind
> of significant figures rounding. (MATLAB had a chop() function that
> did just this, though it must of been in a toolbox I don't currently
> have, as I can't find it now)

But - AFAIK - Matlab can do decimal calculations, while python is stuck 
to the clib's FP (usually IEEE those days) base-2 numbers which cannot 
exactly represent such numbers. Just try 
>>> 1/5.    
0.20000000000000001
(Tested on my Python 2.0.1 (#0, Jun 23 2001, 23:50:30))

Maybe we should think about fixed and floating point decimal numbers 
(using string or bcd internal representation) for inclusion in Python. 
Of course, this leads to complex decimals and even rationals composed 
of complex decimals, too :-)

<running away fast>

> It probably should be a separate function from round, rolling it into
> round seems a little too convoluted and confusing to me.

I fully agree to this, expecially not using floats vs. ints as the 
second argument, as someone proposed.

markus
-- 
"The strength of the Constitution lies entirely in the determination of 
each citizen to defend it. Only if every single citizen feels duty 
bound to do his share in this defense are the constitutional rights 
secure." -- Albert Einstein



More information about the Python-list mailing list