[Python-Dev] Integer division operator can give float result?

Facundo Batista facundo at taniquetil.com.ar
Wed Feb 28 13:22:36 CET 2007


Greg Ewing wrote:

> Is this intentional? I would have expected the
> // operator to always give an integer result.
>
> Python 2.3 (#1, Aug  5 2003, 15:52:30)
> [GCC 3.1 20020420 (prerelease)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> 7.0 // 2
> 3.0

>From the Language Reference:

  The / (division) and // (floor division) operators yield the quotient
  of their arguments. The numeric arguments are first converted to a
  common type. Plain or long integer division yields an integer of the
  same type; the result is that of mathematical division with the
  `floor' function applied to the result.

So, first the arguments are converted to a common type, in this case
float. Then the division is made. Then the 'floor' function is applied.
Result: 3.0.

So yes, it's intentional.

Regards,

-- 
.   Facundo
.
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/




More information about the Python-Dev mailing list