[Python-Dev] Rounding float to int directly ...

Nick Maclaren nmm1 at cus.cam.ac.uk
Thu Aug 3 22:49:30 CEST 2006


Ronald Oussoren <ronaldoussoren at mac.com> wrote:
> 
> > There are algorithms where the operation of rounding (or truncation)
> > is needed, but where the value may be larger than can be held in an
> > integer, and that is not an error.
> 
> Is that really true for python? Python integers are unbounded in  
> magnitute, they are not the same as int or long in C, therefore any  
> float except exceptional values like NaN can be converted to an  
> integer value. The converse is not true, python integers can contain  
> values that are larger than any float (aka C's double).

It depends a great deal on what you mean by a Python integer!  Yes,
I was assuming the (old) Python model, where it is a C long, but so
were many (most?) of the other postings.

If you are assuming the (future?) model, where there is a single
integer type of unlimited size, then that is true.  There is still
an efficiency point, in that such algorithms positively don't want
a float value like 1.0e300 (or, worse, 1.0e4000) expanded to its
full decimal representation as an intermediate step.

Whatever.  There should still be at least one operation that rounds
or truncates a float value, returning a float of the same type, on
either functionality or efficiency grounds.  I and most programmers
of such algorithms don't give a damn which it does, provided that it
is clearly documented, at least half-sane and doesn't change with
versions of Python.


Regards,
Nick Maclaren,
University of Cambridge Computing Service,
New Museums Site, Pembroke Street, Cambridge CB2 3QH, England.
Email:  nmm1 at cam.ac.uk
Tel.:  +44 1223 334761    Fax:  +44 1223 334679


More information about the Python-Dev mailing list