is int(round(val)) safe?

Peter Hansen peter at engcorp.com
Mon Nov 22 16:46:02 EST 2004


Steven Bethard wrote:
> Peter Hansen wrote:
>> Since round() returns an integer
> 
> Just to clarify in case anyone else misreads this, I belive the intent 
> here was to say that round(f) -- that is, round with only a single 
> argument -- returns a floating point number with no fractional component 
> after the decimal point.  The result is still a float, not an int though:
> 
>  >>> round(3.14159265359)
> 3.0
>  >>> round(3.14159265359, 2)
> 3.1400000000000001
> 
> I think the intent was clear from the rest of the post, but I figured it 
> wouldn't hurt to clarify this for any newbies who misread it like I did.

All true.

I wonder if it would be appropriate to say something along
the lines of '''round() returns an integer, but not an "int".'''

Obviously the mathematicians will have something to say about
this.  In computers, 1.0 may not be an integer data type, but
I think in math it's still considered an integer.  I am most
definitely not going to claim authority in this area, however,
since as an engineer I consider 1.0 and 1 merely "equal to a
first approximation". <wink>

-Peter



More information about the Python-list mailing list