PEP proposal for round(x,n) enhancement

Chris Barker chrishbarker at home.net
Mon Sep 17 22:35:32 CEST 2001


Tim Peters wrote:
> > The proposal is not concerned with how numbers are represented
> > internally, just with how they are printed.

I hope this was a mistake. I think it was in reply to the issue that
floating point numbers are stored in binary internally, so if you round
to n decimal digits, the internal representation may still require all
digits (or moare, of course) to represent. I think what he meant to say
was:

The proposal is not concerned with how numbers are represented
internally, just with what the values are, which, to the degree it is
possible, should be the value rounded appropriately to the required
decimal digit. 

> Since printing supports significant-digit rounding directly (for example,
> you can use format "%.3g" if you want 3 significant digits),

%.3g does not do a very clean job of it, as you get exponent notation
when you might not want it.
>>> "%.3g"%1234
'1.23e+03'
>>> "%.3g"%123.4
'123'


> round() predates printf-style formats in Python, and has little good use
> anymore.

you're kidding, right??? I use round All the time!! when converting a
float to an integer, sometimes I want floor(), sometimes ceil() and
sometimes round()

> > There should be a way to say "round to the 3rd digit from the first
> > non-zero digit.
> 
> So long as you're concerned about printing, there are direct ways to do that
> already.

There are many reasons to do this that have nothing to do with printing.

-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