Velocity Reviews > NEWB: dividing numbers

# NEWB: dividing numbers

Lo
Guest
Posts: n/a

 03-08-2009
I just tried python first time.

2/3

the result is zero

I want the result to be .333...

How do I get this?

Thanks a lot

L

Michal Wyrebski
Guest
Posts: n/a

 03-08-2009
Lo pisze:
> I just tried python first time.
>
> 2/3
>
> the result is zero

Float type must be specified explicitly:
2/3.0 or 2.0/3

In Python 3.x operators behave differently and '2/3' would give float
number as a result.

>
> I want the result to be .333...

Than try: 1/3.0 because 2/3.0 will never show you that

Michal

Diez B. Roggisch
Guest
Posts: n/a

 03-08-2009
Lo schrieb:
> I just tried python first time.
>
> 2/3
>
> the result is zero
>
> I want the result to be .333...

Well, that's not going to happen - 2/3 is .666 if not done with integers...

>
> How do I get this?

Use floating points.

>>> 2.0 / 3.0

0.6666666666663

Diez

Chris Rebert
Guest
Posts: n/a

 03-08-2009
On Sun, Mar 8, 2009 at 3:08 PM, Lo <(E-Mail Removed)> wrote:
> I just tried python first time.
>
> 2/3
>
> the result is zero
>
> I want the result to be .333...
>
> How do I get this?

from __future__ import division

That tells Python to use the proper kind of division, which is now the
default in Python 3.0.

Cheers,
Chris

--
I have a blog:
http://blog.rebertia.com

MRAB
Guest
Posts: n/a

 03-08-2009
Lo wrote:
> I just tried python first time.
>
> 2/3
>
> the result is zero
>
> I want the result to be .333...
>
> How do I get this?
>

That's integer division (integer divided by integer is integer).

If you want the result to be floating point then make one of them
floating point:

2.0 / 3

or do this first:

from __future__ import division

In the future and in Python 3.x "/" will always return floating point.
For an integer result use "//" instead.

BTW, 2.0/3.0 still isn't 0.333...

Daniel Dalton
Guest
Posts: n/a

 03-09-2009
Hi,

On Mon, Mar 09, 2009 at 12:08:16AM +0100, Lo wrote:
> I just tried python first time.
>
> 2/3
>
> the result is zero

That's because your dividing an int by an int to an int. The definition
of an int is a "whole number". So just use floating point I think it's
called, this should work, and does just tested with the python
interactive shell:
2.0/3

Cheers,
Daniel.