Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > float problem

Reply
Thread Tools

float problem

 
 
Indigo Moon Man
Guest
Posts: n/a
 
      09-24-2003
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/


 
Reply With Quote
 
 
 
 
Greg Krohn
Guest
Posts: n/a
 
      09-24-2003

"Indigo Moon Man" <> wrote in message
news:bkrai0$50pag$...
> 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


 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      09-24-2003
On Tuesday 23 September 2003 10:34 pm, Indigo Moon Man wrote:
> 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?


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:

>>> 2/3

0
>>> 2/3.0

0.66666666666666663
>>> 2.0/3.0

0.66666666666666663
>>>


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:

>>> from __future__ import division
>>> 2/3

0.66666666666666663
>>> 2//3

0
>>>


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

Gary Herron



 
Reply With Quote
 
Indigo Moon Man
Guest
Posts: n/a
 
      09-24-2003
Gary Herron <> spake thusly:
>
> 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:
>

Great! Thank you very much!


--
Audio Bible Online:
http://www.audio-bible.com/


 
Reply With Quote
 
Indigo Moon Man
Guest
Posts: n/a
 
      09-24-2003
Greg Krohn <> spake thusly:
>
> 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:
>

That's great! Thank you very much for your help!

--
Audio Bible Online:
http://www.audio-bible.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
operator== (float, float) Jukka Lehtonen C++ 5 08-05-2004 08:28 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
static_cast<float>(a) versus float(a) Jim West C++ 4 01-16-2004 12:36 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments