#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    3
    Rep Power
    0

    Division error in python


    Hi All!

    I am first time here. Got just registered. I am learning the python programming...but there is a problem... when i divide a numerator with lower value than denominator i get value = 0 i.e.
    >x=2/3
    >print x
    >0
    it need complete value on division. how to obtain that?
    I shall be thankful for the solution.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    In the next release (Python 2.4 coming soon!) you should get the right exact answer by default. Unfortunatly you're gonna have to wait for a month or two for this to get though the alpha and bete stages.

    So, in the mean time, you can get do this by importing the feature from the __future__ module i.e.

    >>> 2/3
    0
    >>> 2//3
    0
    >>> from __future__ import division
    >>> 2/3
    0.66666666666666663
    >>> 2//3
    0
    >>>
    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    3
    Rep Power
    0
    if this module future is available..then no need to program itself boss. I think ti will have then everything to do....even an inbuilt command liner i.e. run program.py to run itself auto!
    Any serious reply???
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Originally Posted by BlueBaazigar
    Any serious reply???
    What?! The __future__ module holds features that will be added in the near "future". So if you have the latest version of Python (2.3) then division will be available. And, i would like to know what you mean by "Any serious reply"!!!

    Dont want to use the future, then try dividing floats instead of ints.

    Code:
    >>> 2.0/3.0
    0.66666666666666663
    >>>
    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Yep, if you want a floating point result you should use floating point numbers.
    Last edited by Grim Archon; July 22nd, 2004 at 07:37 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    3
    Rep Power
    0
    Thanks a lot to both of you. mark I am sorry...i really thought it was a joke...u see peculier module "__future__". I am impressed. Mark can you tell me best few books on python where i could get all these details. Right now i have wesley chun's core book and Mark Lutz's programming python. I really din't find description of future there!
    Again thanks lot.
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Not a problem. There's also __main__ and a __builtin__ modules too. Anyway you should check out "Learning Python, Second Edition" a great book, for newbies and Pythonists alike.

    Welcome to the forums,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo