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

Ronald Oussoren ronaldoussoren at mac.com
Thu Aug 3 22:12:11 CEST 2006

On Aug 3, 2006, at 9:43 PM, Nick Maclaren wrote:

> Ka-Ping Yee <python-dev at zesty.ca> wrote:
>> That's my experience as well.  In my opinion, the purpose of round()
>> is most commonly described as "to make an integer".  So it should
>> yield an integer.
> Grrk.  No, that logic is flawed.
> 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).

 >>> v = 1e200
 >>> int(v)
 >>> type(v)
<type 'float'>
 >>> t = int(v)
 >>> t ** 2
 >>> float(t**2)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2157 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20060803/5e233fa2/attachment.bin 

More information about the Python-Dev mailing list