This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 5 years ago.
I made a small python file on my desktop for fun, and I'm frustrated to find that no values are being returned. Here is the code from the file:
def calc(x):
chance =1
for i in range(1,x+1):
chance *= ((365-i)/365)
return chance * 100
string = calc(23)
print string
And here is what I type into the terminal:
cd Desktop #where file is located
python py.py #py is the name of the file
And this is what it returns:
0
Since 365 and i are integers, you are performing integer division, which only returns the "whole" part of the result. Since 365-i is lesser than 365 dividing the two will result in 0, and continuously multiplying it by anything will still give 0.
You could use the 365.0 notation to use floating point division, which will eventually generate the result 46.1655742085:
chance = 1
for i in range(1,x+1):
chance *= ((365.0 - i) / 365.0)
# Floating point^-----------^
Well so on your first iteration your i = 1. 365-1 = 364. 364/365 = 0 because it is integer division in python. So the answer will always give you 0 unless you change it to allow for you to give a float.
Changing 365 to 365.0 will give you your answer
chance = chance * ((365-i)/365.0)
I just changed the 365 to 365.0 and I got
2.23071139399e-05
As my output
The result is correct in Python2.x. When a/b and a is less than b, result is 0.
Modify 365 to 365.0 it will cast the division result to float
chance *= ((365-i)/365.0)
Related
This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 4 years ago.
I try to compute x = (2**83 + 1)/3 which is mathematically an integer and a float in python-x.
In python 2, I get :
x = 2**83 + 1
x = 9671406556917033397649409L
then
y = x/3 = 3223802185639011132549803L
In python 3, I get :
x = 2**83 + 1
x = 9671406556917033397649409 --> OK
then
y = x/3 = 3.223802185639011e+24
To compare the 2 results, I use a format string instruction in python 3:
z = '%25d' % y and I get z = '3223802185639010953592832'
and z = '3223802185639011132549803' in python 2.
(%i gives the same results, quite normal).
The strange thing is that when I compute 3*Z, I get the good result in python2 and a wrong one in python3.
I can't see where the problem is with my test (computing, formatting, ...).
I'd like to use python 3 and to display x = (2**83 + 1)/3 with no 'e+24' but with all numbers.
Does anybody have an idea?
I have to add thet the problem remains the same when using / or // in python2. We get the same result sinc it is mathematically an integer. I should say that the problem is rather with python 3. How can i get the good result (the whole dispaly of (2**83 + 1)/3 in python 3) ?
You seem to be looking for integer division as opposed to the floating point one.
/ operator returns a floating point number in Python3. To perform integer division in Python3, use //.
So, I guess all you need is
(2**83 + 1)//3
which gives
3223802185639011132549803
instead of the (2**83 + 1)/3.
In Python2.7, both / and // are effectively the same unless you do something like from __future__ import division.
I am trying to use the python field calculator in ArcMap to do a simple calculation, where:
there are two input variables, the numerator and the denominator
if the numerator is zero a zero is returned
else, the numerator is divided by the denominator and multiplied by 100
The code I tried:
def getScore(num, den):
if num == 0:
return 0
else:
return (num/den) * 100
when I run the script i get no errors yet the 'else' statement is not getting returned.
there will be no zeros in the denominator, so div/0 will not be an issue. The input fields are all 'long' integers and the target field is a 'double.'
I've attached a couple of images showing a test in python where the same exact code works perfectly, as well as the the field calculator where it does not work.
The way ArcGIS Desktop divides integers will result in an integer, so percentages will always end up as zero (e.g. 1 / 10 = 0) -- annoying, and counterintuitive, but that's what it's doing.
ArcGIS Pro uses Python 3 and in ArcGIS Desktop, it uses Python 2. Python 2 uses integer math, meaning that dividing two integer values will always produce an integer value (3 / 2 = 1). In Python 3, dividing two integer values will produce a float (3 / 2 = 1.5).
You can instead explicitly cast the variables as float, which will then do float division and give you a float result (1 / 10 = 0.1). The following works for me:
def getScore(num, den):
if num == 0:
return 0
else:
return (float(num)/float(den)) * 100
One observation: your conditional is basically saying "if the numerator is zero, don't bother dividing, just return zero" -- however, that's what 0 divided by anything is going to be anyway. You can skip the conditional, and the entire codeblock, and just directly calculate -- but still remember to cast the values as float first, or you'll still get a bunch of zeros :)
float(!Healthy!) / float(!Total!)
Isn't this just what you want?
If you do it step by step:
healthy = 1
total = 10
Of course healthy is not 0, so it goes in the else block:
return (healthy / total) * 100
Which is equal to
return (1 / 10) * 100
Which then is equal to:
return (0.1) * 100
Which is 10, just as you get it in the screenshot.
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 6 years ago.
I am trying to do the calculation
Using the python decimal module with the following code:
from decimal import *
getcontext().prec = 9
sum = Decimal(0)
for i in range(1,11):
sum += Decimal(1/(i**4))
print sum
however, this outputs 1, not a very small fraction like I would expect. I can't find much information here https://docs.python.org/2/library/decimal.html about what is wrong with the code. My guess is sum is not being used as a Decimal in the loop, but I am unsure how to resolve that.
If you use Python 2.x, then in the expression: 1/(i**4), the integer devision is used, as result for i=1, it equals to 1 and for all other i>1, it gets 0.
Just add floating point to 1: 1./(i**4), this should fix the problem.
PS In Python 3.x, your code should work as expected, because operator / is defined on floating point numbers, while operator // is defined for integers.
First of all, don't use sum as a variable name, as it is a built-in.
And its sort of necessary to provide at least one float for arithmetic if you expect a float-type answer, here:
s = Decimal(0)
for i in range(1,11):
s += Decimal(1./(i**4)) # dividing 1. or 1.0 instead of just 1
print s
this gives:
1.08203658
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 6 years ago.
I have a simple problem:
print 1 / 100
returns
0
and
print float(1 / 100)
returns
0.0
Why does this happen? Shouldn't it return 0.01? Thank you for any help.
Simple solution!
print 1 / float(100)
Your problem is that by default in Python 2 the division operator will do integer division (rounding down to integer). By making one of the operands a float, Python will divide in the expected way. You were almost there with float(1 / 100), however all this accomplishes is doing the integer division of 1 by 100, which equals zero, then converting zero to a floating point number.
This is a recognized issue in Python 2, fixed in Python 3. If you get tired of writing x / float(y) all the time, you can do from __future__ import division to make the division operator behave as in Python 3.
You are doing integer division. What you've written is, for all intents and purposes:
float(int(1)/int(10))
For example:
assert float(10/3) == 3.0 # True
You need to have at least one of them implicitly or explicitly a float. All of these are valid:
float(1.0/100.0)
float(1.0/100)
float(1/100.0)
float(float(1)/float(100))
etc...
You are doing integer division, when expecting a decimal (float), you can assert an integer to be a float by placing a decimal at the end, i.e.
print 1 / 100.
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 8 years ago.
I am trying to make a BMI calculator function. I am learning python at pyschools.
This is my code:
# Note: Return a string of 1 decimal place.
def BMI(weight, height):
x = weight /(height*height)
g = round(x,1)
return g
And pyschools shows me that these are the right answers:
With 110 = weight and 2 = height I am supposed to get a BMI of 27,5.
But I instead get 27.
Then it does a second check to make sure I wrote the code right and tells me 24,2 is the right answer but my program did return 24,2. But it still marks my answer in red and says "my" 24,2 is wrong and the website's is right.
If someone has a better site or anything to learn python it would also be appreciated since this website seems to be kind of wrong sometimes. And I am looking for free online resources. No books please.
To fix it for all cases, add this line to the top:
from __future__ import division # Make division work like in Python 3.
in Python 2, / means integer division.
With this in mind, in Python 2 if you pass intgers into division, it will give you an integer back. Anything that would have been a float is floored*. Therefore another option to get the desired result is to pass a float in, so instead of:
weight / (height*height)
do:
float(weight) / (height*height) # float in means float out.
*This means that only full times that the divisor goes in are counted. So 1/2 will get 0 because 2 goes fully into 1 0 times.
def BMI(weight, height):
x = float(weight) /(height*height)
g = round(x,1)
return g
see Python division
and Binary arithmetic operations
The issue lies with your division.
Division as we intrinsically know it is floating point division, or division where 1 / 2 evaluates to a fraction, 0.5. In standard programatic division, the 1, 2 are ints() and therefore cant be fractions, or floats() as the type is called in python. The expression, 1 / 2 therefore evaluates as 0, as 2 as a whole integer cant go into one entirely any times.
Ex:
In [1]: 1 / 2
Out[1]: 0
# Explicitly what is going on, since 1 and 2 are ints.
In [2]: int(1) / int(2)
Out[2]: 0
#fixed with floating division
In [3]: float(1) / float(2)
Out[3]: 0.5
# protip: only one of the divisors needs to be a float for python to divide correctly.
In [4]: 1 / float(2)
Out[4]: 0.5
Use x = weight / float((height*height)) to get the results you expect.
# Note: Return a string of 1 decimal place.
def BMI(weight, height):
x = weight / float((height*height))
g = round(x,1)
return g