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.
My program is to find the number of types, the number of tokens, and the type-to-token ratio. However, I don't know how to tell Python the answer to ttr is not an integer.
from nltk.corpus import inaugural
print inaugural.fileids()
tokens = inaugural.words("1789-Washington.txt")
numtokens = len(tokens)
print numtokens
types = sorted(set(tokens))
numtypes = len(types)
print numtypes
# This is the part I'm unsure about.
ttr = numtypes/numtokens
print ttr
If you are working in Python 3, the division operator / is performing floating point division by default:
>>> 3 / 2
1.5
>>> 4 / 2
2.0
since integer division is handled by the // operator.
In Python 2.x, if you want decimal accuracy in integer division, you can convert either the nominator or the denominator to float(), like this:
ttf = float(numtypes) / numtokens
Alternatively, as tobias_k points out, you can do
>>> from __future__ import division
>>> 3 / 2
1.5
to get Python3-esque division in Python 2.x
Related
This question already has answers here:
What is the difference between '/' and '//' when used for division?
(16 answers)
Closed 4 years ago.
I recently transitioned one script to use Python 2.7 from 3.4 (at the request of a colleague), and I'm finding that the math.ceil function does not work the same...
When I divide 5/2 and round it up with math.ceil(), I expect to get math.ceil(5/2) = 3.
Given the following code:
from __future__ import print_function
import math
a = 5
b = 2
c = math.ceil(a/b)
print("Ceiling output: {}".format(c))
Python 2.7 will report 2 as the answer, Python 3.4 reports 3 as expected.
Why is this?
I know I can get 2.7 to work if I cast a and b as float:
c = math.ceil(float(a)/float(b)
However, casting the division won't work either:
c = math.ceil(float(a/b))
How does 3.4 work around this?
Finding this makes me question how much math I need to re-check in the 2.7 version of the script.
It's because the division operator / is integer division in Python 2.7. Note that the function arguments are fully evaluated before the function call, so math.ceil was actually an innocent bystander here.
math.ceil(5/2) # same as math.ceil(2)!
To get the Python 3 behaviour in 2.7, use a future statement:
from __future__ import division
This is because / in Python 2 is regular integer division.
If you want the / operator to behavethe way it does in Python 3 then add this line to your imports from __future__ import division.
Also, note that if you wanted to regular integer division in Python 3 use // instead of /.
after you add the import you will find that
In [16]: from __future__ import division
In [17]: a = 5
In [18]: b = 2
In [19]: import math
In [20]: c = math.ceil(a/b)
In [21]: c
Out[21]: 3.0
This is not a rounding issue, it is a default numeric type issue. Python 2.7 looks at the numbers and sees integers. Since all numbers involved are integers, 5/2 ==> 2 instead of 2.5, so ceil() is 2. Note that the / operator WILL do floating point division too, provided at least one value is floating point. So if x = 5.0 instead of 5 or if y = 2.0 instead of 2 then you will get 2.5 ==> ceil() = 3.
This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
My question is why does,
import math
a = math.sqrt(3)
area = a * (1 / 2)
print area
give an output of 0.0, but,
import math
a = math.sqrt(3)
area = a / 2
print area
give an output of 0.866025403784
Actually this is a problem of integer truncation in 2.X.
X / Y
Classic and true division.
In Python 2.X, this operator performs classic division, truncating results for integers, and keeping remainders (i.e., fractional parts) for floating-point numbers.
In 2.X, the / does classic division, performing truncating integer division if both operands are integers and float division (keeping remainders) otherwise.
In Python 3.X, it performs true division, always keeping remainders in floating-point results, regardless of types.
In 3.X, the / now always performs true division, returning a float result that includes any remainder, regardless of operand types.
In Python 2.X You can "fix" this by adding from __future__ import division to your script. This will always perform a float division when using the / operator and use // for integer division.
But you can make at least one of the operands in the division is a float, you'll get a float result.
>>> from __future__ import division
area=a*(1/2)
area
0.8660254037844386
I hope this will help you.
i think you are using python 2.7 this is the problem with python 2.7 it evaluate 1/2 as zero because it think 1&&2 both as integer so it gives result as integer but in second case your math.sqrt() function return an double value so it gives result as double . but if you use python 3.0 the problem is fixed you get same result in both the cases you can try it it gives 1/2 as 0.5 .
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:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
I'm trying to calculate 27^(1/3) = 3. But it turns out not as expected.
>>> 27 ** (1/3)
1
>>> 27 ** (1/3.)
3.0
Firstly, why the first calculation will result in 1, is it because 1/3 is casted to an int so it equals zero?
Secondly, what does the '.' means in Python in general, could I append a dot to any number in order to cast it from int to double?
Thanks.
Firstly, why the first calculation will result in 1, is it because 1/3
is casted to an int so it equals zero?
Yes, that's exactly it.
what does the '.' means in Python in general, could I append a dot to
any number in order to cast it from int to double?
The . is explicitly making the number a float as 1.0 would be opposed to just 1.
You experience integer division in python 2.
in python 2:
>>> 2/3
0
In python 3:
>>> 2/3
0.6666666
If you want the python3 behaviour in python2 you can do
from __future__ import division
as your first import.
For the integer division, use
>>> 2 // 3
0
Or you can use 2.0 / 3.0 to use floats.
In python 2.x, dividing integers defaults to floor division, so any non-integer quotient automatically rounds down to the nearest integer for example
>>> 1/3
0
>>> 5/2
2
So when you try to calculate 27 ** (1/3) you are just calculating 27 ** 0
There are numerous workarounds to the problem, the simplest way would be to just use floating points in the exponent - (1.0/3.0), or even just (1/3.0). However, if you were to be doing a lot of these calculations it might become annoying to constantly type out all the numbers in floating point form. In this case, you could use the future module to utilize Python 3.x division behavior, which always results in a floating point value:
>>> from __future__ import division
>>> 1/3
0.3333333333333333
This way the calculation will never default to floor division. And if at some point you want floor division in the same program, you can just use the // operator to get behavior similar to python 2.x integer division, like so:
>>> 1//3
0