float problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Indigo Moon Man

    float problem

    for the formula J = I / (12 * 100) where I is low (like about 8 to 15) I
    get 0. But when I do it with a calculator it's actually .008333 for example
    if I were 10. Is there a way I can get python to recognize the .008333
    instead of it just giving me 0?

    TIA for your help!

    --
    Audio Bible Online:
    Audio Bible, The King James Version narrated by Stephen Johnston is on-line



  • Greg Krohn

    #2
    Re: float problem


    "Indigo Moon Man" <indigomoon@bon bon.net> wrote in message
    news:bkrai0$50p ag$1@ID-70710.news.uni-berlin.de...[color=blue]
    > for the formula J = I / (12 * 100) where I is low (like about 8 to 15) I
    > get 0. But when I do it with a calculator it's actually .008333 for[/color]
    example[color=blue]
    > if I were 10. Is there a way I can get python to recognize the .008333
    > instead of it just giving me 0?
    >
    > TIA for your help!
    >
    > --
    > Audio Bible Online:
    > http://www.audio-bible.com/
    >[/color]

    Normally division with integers gives an integer result losing everything
    after the decimal. Couple of things you can do about that, but basically you
    have to convert your denominator or divisor to a float before you divide:

    J = I / float(12 * 100)

    or

    J = float(I) / (12 * 100)

    Alternativly you could use 12.0 instead of 12 (or 100.0 instead of 100 for
    that matter)

    J = I / (12.0 * 100)

    or

    J = I / (12 * 100.0)

    And last, but not least, you can import from future to make all division
    "lossless":

    from __future__ import division
    J = I / (12 * 100)


    I personally prefer the first one, but they will all work fine.


    greg


    Comment

    • Gary Herron

      #3
      Re: float problem

      On Tuesday 23 September 2003 10:34 pm, Indigo Moon Man wrote:[color=blue]
      > for the formula J = I / (12 * 100) where I is low (like about 8 to 15) I
      > get 0. But when I do it with a calculator it's actually .008333 for
      > example if I were 10. Is there a way I can get python to recognize the
      > .008333 instead of it just giving me 0?[/color]

      You're getting bit by the difference between integer and float
      division.

      In versions of python prior to 2.3 (and in 2.3 under normal
      circumstances), division of two integers returns an integer and
      division of two floats (or a float and an integer )returns a float:
      [color=blue][color=green][color=darkred]
      >>> 2/3[/color][/color][/color]
      0[color=blue][color=green][color=darkred]
      >>> 2/3.0[/color][/color][/color]
      0.6666666666666 6663[color=blue][color=green][color=darkred]
      >>> 2.0/3.0[/color][/color][/color]
      0.6666666666666 6663[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      So one solution would be to make sure the numbers you are dividing are
      floats when you want a float in return.

      In future version of Python, there will be two dividion operators:
      / will always return a float
      // will always return an int

      In Python 2.3, you can experiment with the future behavior by starting
      your program with:
      [color=blue][color=green][color=darkred]
      >>> from __future__ import division
      >>> 2/3[/color][/color][/color]
      0.6666666666666 6663[color=blue][color=green][color=darkred]
      >>> 2//3[/color][/color][/color]
      0[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      So depending on your version of Python, this may be another solution.

      Gary Herron



      Comment

      • Indigo Moon Man

        #4
        Re: float problem

        Gary Herron <gherron@island training.com> spake thusly:[color=blue]
        >
        > You're getting bit by the difference between integer and float
        > division.
        >
        > In versions of python prior to 2.3 (and in 2.3 under normal
        > circumstances), division of two integers returns an integer and
        > division of two floats (or a float and an integer )returns a float:
        >[/color]
        Great! Thank you very much!


        --
        Audio Bible Online:
        Audio Bible, The King James Version narrated by Stephen Johnston is on-line



        Comment

        • Indigo Moon Man

          #5
          Re: float problem

          Greg Krohn <ask@me.com> spake thusly:[color=blue]
          >
          > Normally division with integers gives an integer result losing everything
          > after the decimal. Couple of things you can do about that, but basically
          > you have to convert your denominator or divisor to a float before you
          > divide:
          >[/color]
          That's great! Thank you very much for your help!

          --
          Audio Bible Online:
          Audio Bible, The King James Version narrated by Stephen Johnston is on-line



          Comment

          Working...