NEWB: dividing numbers

Discussion in 'Python' started by Lo, Mar 8, 2009.

  1. Lo

    Lo Guest

    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
     
    Lo, Mar 8, 2009
    #1
    1. Advertising

  2. 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
     
    Michal Wyrebski, Mar 8, 2009
    #2
    1. Advertising

  3. 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
     
    Diez B. Roggisch, Mar 8, 2009
    #3
  4. Lo

    Chris Rebert Guest

    On Sun, Mar 8, 2009 at 3:08 PM, 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?


    Add the following to the top of your program:

    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
     
    Chris Rebert, Mar 8, 2009
    #4
  5. Lo

    MRAB Guest

    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... ;-)
     
    MRAB, Mar 8, 2009
    #5
  6. 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.
     
    Daniel Dalton, Mar 9, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve

    Dividing a clock

    Steve, Jan 1, 2004, in forum: VHDL
    Replies:
    0
    Views:
    1,625
    Steve
    Jan 1, 2004
  2. kwaj

    Dividing Real Numbers?

    kwaj, Feb 9, 2004, in forum: VHDL
    Replies:
    2
    Views:
    1,232
  3. =?Utf-8?B?cm9kY2hhcg==?=

    dividing numbers

    =?Utf-8?B?cm9kY2hhcg==?=, Jun 28, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    469
    =?Utf-8?B?cm9kY2hhcg==?=
    Jun 28, 2005
  4. M. Akkerman
    Replies:
    5
    Views:
    387
    Jeff Schwab
    Dec 14, 2003
  5. eltower

    [NEWB]: List with random numbers

    eltower, Aug 20, 2006, in forum: Python
    Replies:
    4
    Views:
    336
    eltower
    Aug 20, 2006
Loading...

Share This Page