472,330 Members | 1,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

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:
http://www.audio-bible.com/
Jul 18 '05 #1
4 12511

"Indigo Moon Man" <in********@bonbon.net> wrote in message
news:bk************@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
Jul 18 '05 #2
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

Jul 18 '05 #3
Gary Herron <gh*****@islandtraining.com> 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/
Jul 18 '05 #4
Greg Krohn <as*@me.com> 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/
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bengt Richter | last post by:
Peculiar boundary cases: >>> 2.0**31-1.0 2147483647.0 >>> int(2147483647.0) 2147483647L >>> int(2147483647L ) 2147483647 >>> >>> -2.0**31
5
by: Chris Beall | last post by:
See http://pages.prodigy.net/chris_beall/BeallSprings/WC.Deed%20GG.116-17.html using the CSS at...
2
by: Dr. Richard E. Hawkins | last post by:
I've googled around, and asked everyone I know, but I still can't find any reference to anyone else having faced this particular IE bug with...
14
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating...
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my...
8
by: Rene | last post by:
The following code: float someFloat = 0; for(int x = 1; x <= 10; x++) { someFloat += .1F; Console.WriteLine(someFloat.ToString()); }
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); ...
3
by: bofh1234 | last post by:
I am trying to write a function that returns a float. This is what the function should do: It takes the socket number and a variable name. The...
3
by: Harry | last post by:
Using IE7, I'm trying to display a table in a horizontal manner by floating the rows. The following html does not work, displaying the table...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.