PEP proposal for round(x,n) enhancement

Chris Barker chrishbarker at
Tue Sep 11 18:51:45 CEST 2001

-RZ wrote:

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

I agree that it is a very useful function. I would be very surprised if
it is not already in NumPy or SciPy.

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

By the way, I'm pretty sure there is a nifty way to compute it with
log10, but it escapes me at the moment.

> the floating point representation issues, how exactly does one determine
> the first non-zero digit?

I don't think this is any more of a problem here than for round() or
just about any other function. The errors in floating point
representation start at the LEAST significant bit. If the MOST
significant bit is in error, you've just got junk anyway, so that won't


Christopher Barker,
ChrisHBarker at                 ---           ---           --- ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------

More information about the Python-list mailing list