1/2 evaluates to 0

L

Laurent Claessens

Hi all


This is well known :
0

This is because the division is an "integer division".

My question is : how can I evaluate 1/2 to 0.5 ? Is there some non
integer division operator ?
Up to now I workarounded writing float(1)/2. Is there an other way ?

My Zen of python says :
There should be one-- and preferably only one --obvious way to do it.

and I don't believe that float(1)/2 is an "obvious way" (I'm not Dutch)

Have a good afternoon
Laurent
 
J

Jean-Michel Pichavant

Laurent said:
Hi all


This is well known :

0

This is because the division is an "integer division".

My question is : how can I evaluate 1/2 to 0.5 ? Is there some non
integer division operator ?
Up to now I workarounded writing float(1)/2. Is there an other way ?

My Zen of python says :
There should be one-- and preferably only one --obvious way to do it.

and I don't believe that float(1)/2 is an "obvious way" (I'm not Dutch)

Have a good afternoon
Laurent
Hi,

1./2


JM
 
N

Noah Hall

This is well known :

0

This is because the division is an "integer division".

My question is : how can I evaluate 1/2 to 0.5 ? Is there some non integer

Include from __future__ import division on the top of your file
0.5
 
A

Aage Andersen

Laurent Claessens said:
Hi all


This is well known :

0

This is because the division is an "integer division".

My question is : how can I evaluate 1/2 to 0.5 ? Is there some non integer
division operator ?
Up to now I workarounded writing float(1)/2. Is there an other way ?

what about 1.0/2 ?
 
P

Paul Rudin

Laurent Claessens said:
Hi all


This is well known :

0

This is because the division is an "integer division".

My question is : how can I evaluate 1/2 to 0.5 ? Is there some non
integer division operator ?
Up to now I workarounded writing float(1)/2. Is there an other way ?

from __future__ import division

Or use operator.truediv

Or use python 3.
 
C

Chris Angelico

Hi all

This is well known :

0

Only in Python 2.
This is because the division is an "integer division".

My question is : how can I evaluate 1/2 to 0.5 ? Is there some non integer
division operator ?

1.0/2 is floating point division, but you can also use:
0

This is the most portable option. Stick with // for integer division
and tell / to produce a float (which it will do even if the result
would have fitted into an integer).

Note that the __future__ directive needs to be at the top of your program.

ChrisA
 
P

Peter Otten

Laurent said:
This is well known :

0

This is because the division is an "integer division".

My question is : how can I evaluate 1/2 to 0.5 ? Is there some non
integer division operator ?
Up to now I workarounded writing float(1)/2. Is there an other way ?

My Zen of python says :
There should be one-- and preferably only one --obvious way to do it.

and I don't believe that float(1)/2 is an "obvious way" (I'm not Dutch)

In Python 3 there is an obvious way:
0.5

In Python 2 you can trigger that behaviour with the magic incantation
0.5

In both cases the traditional integer division can be forced with
0
 
L

Laurent

Include from __future__ import division on the top of your file

0.5

Wohaw. This means that this behavior is going to be default in a
foreseeable future ?

Thanks
Laurent
 
N

Nobody

Wohaw. This means that this behavior is going to be default in a
foreseeable future ?

It's the default in 3.x. I can't imagine it ever being the default in 2.x.
 
S

Steven D'Aprano

Nobody said:
It's the default in 3.x. I can't imagine it ever being the default in 2.x.


2.7 is now in "bug-fix only" mode, so no new features, and there won't be a
2.8, so true division will never be the default in 2.x.
 
L

Laurent Claessens

2.7 is now in "bug-fix only" mode, so no new features, and there won't be a
2.8, so true division will never be the default in 2.x.

Thanks all for your ansers. I'll import division from __future__
Most of what I'm using in Python is with Sage[1]. Thus I'm not about to
step to 3.x :(

Laurent

[1] www.sagemath.org
 
S

SigmundV

Thanks all for your ansers. I'll import division from __future__
Most of what I'm using in Python is with Sage[1]. Thus I'm not about to
step to 3.x :(

You should get in touch with the Sage developers. In the Sage FAQ they
say that "until SciPy is ported to run with Python 3.x and Cython
supports Python 3.x, Sage will continue to use Python 2.x."

However, both SciPy and Cython are currently compatible with Python
3.2. From the Cython FAQ: "As of 0.14, the Cython compiler runs in all
Python versions from 2.3 to 3.2 inclusive." NumPy has supported Python
3 since version 1.5.0. From the release notes: "This is the first
NumPy release which is compatible with Python 3." SciPy has supported
Python 3 since version 0.9.0. From the release notes: "Scipy 0.9.0 is
the first SciPy release to support Python 3. The only module that is
not yet ported is ``scipy.weave``."

So according to the Sage FAQ there is no reason why Sage shouldn't
support Python 3.


Sigmund
 
L

Laurent Claessens

You should get in touch with the Sage developers. In the Sage FAQ they
say that "until SciPy is ported to run with Python 3.x and Cython
supports Python 3.x, Sage will continue to use Python 2.x." ``scipy.weave``."

So according to the Sage FAQ there is no reason why Sage shouldn't
support Python 3.

Maybe the FAQ is not up to date. Nowadays the reason for not stepping to
3.x is "manpower" ;)
(this question comes back two or three times a year on the Sages' list.
Next time the question arises, I'll point out your remark)

Scipy and Cython are just two of the biggest python parts in Sage, but
there are many others ... 'till sage itself.

Have a good afternoon
Laurent
 
8

88888 dihedral

How about fractions to be computed in hundreds or even thousands of digits in precision?

OK, just write programs to compute PI and the Euler number in hundreds or even thousands of digits to test various kind of programming languages.

This is a sophomore school home work for gifted kids to play with programmings.
 
8

88888 dihedral

Maybe one should try to compute C(n,k) in additions only by the Pascal triangle first!

This is simpler for senior high school kids to play with python!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top