PEP proposal for round(x,n) enhancement

Chris Barker chrishbarker at home.net
Mon Sep 17 17:02:58 EDT 2001


Christopher Smith wrote:

> Would you mind giving me an opinion here?  I think you made the best
> argument for not using the name "round" since floating n is already
> accepted by round and changing how n is interpreted would break anyone's
> code who passed in n with a non-zero decimal portion.  Sigfig or chop is
> an alternative but I really don't like the name "chop" since it's not
> descriptive of the rounding that takes place.  Sigfig is ok, but I am
> proposing a round function (below) that offers more than just sigfigs.
> So...how about "round2" which has precedence in the atan2() function.

I don't love it, but I don't hate it either. Part of the idea behind
atan2 is that it takes two arguments, rather than one, not just that it
is a second version of atan. I still like SigFig or something like that
better.

> The two sorts of roundings that can be done with round2() are 1) rounding
> to a specified number of significant figures or 2) rounding to the nearest
> "x".

While this latter function would be pretty handy now and then, I'm not
at all sure it should be part of the same function. Besides, while it is
usefull, it's also a special case, even if you do have a given
uncertainly, you don't neccessrily want to center things on zero. I
you're bins are 3 units wide, you might want a bin at (-1, 1), or you
might want it at: (0,3).

By the way, the NumPy searchsorted() function is an existing way to
build histograms.

Rounding to significant figures is a very common procedure, and will be
a familar concept to  anyone with even a smattering of a science
backgound. While your other suggested functionality is realated, it is
not the same, as evidenced by the fact that your code is completely
differnt for the two functions. If you want to add someting else, I
suppose you could have the base an optional parameter, although I doubt
it would get used much.

> from math import floor,log10
> def round2(x,n=0,sigs4n=1):
>         '''Return x rounded to the specified number of significant digits, n, as
>         counted from the first non-zero digit.
> 
>         If n=0 (the default value for round2) then the magnitude of the
>         number will be returned (e.g. round2(12) returns 10.0).

I'm not at all sure that I like this: it means that round2(0) ==
round2(1). Zero significant figures is kind of a meaningless concept, it
might as well be an error (or NaN, if Python supported that!)

>         If n<0 then x will be rounded to the nearest multiple of n which, by
>         default, will be rounded to 1 digit (e.g. round2(1.23,-.28) will round
>         1.23 to the nearest multiple of 0.3.

yuck! a negative number has meaning, and this is using it as a notation
trick that has nothing to do with the meaning, it is not clear or
intuitive. I tried to come up with a better option, and couldn't, which
is evidence that you really have two different functions here!

>         Regardless of n, if x=0, 0 will be returned.'''

reasonable enough, but maybe not worth even saying (though you do need
to code the special case)
 

Despite my criticisms, I do think it's a good idea. In Numpy, if no
where else...

-Chris


-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------




More information about the Python-list mailing list