float problem

Greg Krohn ask at me.com
Wed Sep 24 01:57:25 EDT 2003


"Indigo Moon Man" <indigomoon at bonbon.net> wrote in message
news:bkrai0$50pag$1 at ID-70710.news.uni-berlin.de...
> 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:
> http://www.audio-bible.com/
>

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






More information about the Python-list mailing list