473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 12589

"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 http://pages.prodigy.net/chris_beall/BeallSprings/BSstyle.css The page is a transcript of a...
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 floats. I've put a page at...
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 another thread, I once tried this in rather a...
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 point value, I get 0 (zero) for the result. This...
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 tests confirm this. However, most of the Math...
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); /*--------------pnt02own.c------------ ---1 inch = 2.51...
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 function looks up the variable in an array of...
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 vertically as if the rows were not floated. This same...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.