Reputation: 128
How does Python calculate trigonometric functions? I try to calculate using
x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
I'm getting
x = 0.1
why is that? in a usual calculator (radian) i'm getting 0.001
Upvotes: 1
Views: 1706
Reputation: 2320
Just make your integers such as 2
float 2.0
, else Python 2.x uses integer division, also known as floor division (rounding towards minus infinity e.g. -9/8
gives -2
, 9/8
gives 1
), when dividing integers by other integers (whether plain or long):
x = ((0.1-0.001)/2.0)*math.sin(((1/20.0)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2.0)+0.001
and:
print x
0.001
Upvotes: 1
Reputation: 19264
In python2.x, python takes the floor of integer division. Thus, you need to import division
from the __future__
library at the top of your program.
from __future__ import division
x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
print x
Upvotes: 3
Reputation: 107287
In Python 2, /
is integer division,you need to import __future__ .division
for floating division :
>>> from __future__ import division
>>> import math
>>> x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
>>> x
0.001
Upvotes: 4