Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

Integer division operator can give float result?

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


greg.ewing at canterbury

Feb 27, 2007, 7:34 PM

Post #1 of 4 (308 views)
Permalink
Integer division operator can give float result?

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

--
Greg
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


fdrake at acm

Feb 27, 2007, 8:30 PM

Post #2 of 4 (292 views)
Permalink
Re: Integer division operator can give float result? [In reply to]

On Tuesday 27 February 2007 22:34, Greg Ewing wrote:
> Is this intentional? I would have expected the
> // operator to always give an integer result.

Think "floor division", not "integer division". The result (r) may be a
float, but it'll hold to the constraint:

r == int(r)


-Fred

--
Fred L. Drake, Jr. <fdrake at acm.org>
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


facundo at taniquetil

Feb 28, 2007, 4:22 AM

Post #3 of 4 (285 views)
Permalink
Re: Integer division operator can give float result? [In reply to]

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/


_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


guido at python

Feb 28, 2007, 1:44 PM

Post #4 of 4 (274 views)
Permalink
Re: Integer division operator can give float result? [In reply to]

Also consider this example:

>>> 7.2 // 0.5
14.0
>>>

On 2/28/07, Facundo Batista <facundo[at]taniquetil.com.ar> wrote:
> 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/
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev[at]python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>


--
--Guido van Rossum (home page: http://www.python.org/~guido/)
_______________________________________________
Python-Dev mailing list
Python-Dev[at]python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.