Reputation: 25
I already read the similar problems in here but nothing works with me .. what's wrong with this python code .. It keeps returning this error message:
TypeError: a float is required
Beta = float( math.atan([(2 - yr)/(1 - xr)]))
print Beta;
Note that: xr = 2 , yr = 2 are predefined before on the previous lines of the code
Upvotes: 0
Views: 1384
Reputation: 55469
In standard mathematical notation, square brackets []
can be used like round brackets ()
for grouping, but they don't work like that in Python (or most other programming languages). In Python, square brackets are used to denote a list. And as Martijn said, the math.atan()
function expects a float argument, not a list.
However, it's generally much better to use the math.atan2()
function, which accepts two arguments, like this
math.atan2(delta_y, delta_x)
instead of
math.atan(delta_y / delta_x)
The advantage of math.atan2()
is that you can do things like math.atan2(delta_y, 0)
, which would fail with math.atan()
. Also, because the Y
and X
parts are separate, math.atan2()
can determine the correct quadrant for the arctangent from the signs of Y
and X
.
Upvotes: 0
Reputation: 1121366
You are passing in a list:
[(2 - yr)/(1 - xr)]
That's not a float
, it is a list with one element. Remove the square brackets:
Beta = float( math.atan((2 - yr) / (1 - xr)))
Demo:
>>> import math
>>> xr, yr = 3, 4
>>> math.atan([(2 - yr)/(1 - xr)])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a float is required
>>> math.atan((2 - yr)/(1 - xr))
0.7853981633974483
Be careful with using /
in Python 2; if yr
and xr
are integers, you'll get floor divison. See How can I force division to be floating point? Division keeps rounding down to 0 for ways around that.
Upvotes: 6