-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcosine.py
More file actions
33 lines (30 loc) · 802 Bytes
/
cosine.py
File metadata and controls
33 lines (30 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def myCos(x):
'''
objective: to compute the value of cos(x)
input parameters:
x: enter the number whose cosine value user wants to find out
approach: write the infinite series of cosine using while loop
return value: value of cos(x)
'''
epsilon=0.00001
multBy=-x**2
term=1
total=1
nxtInSeq=1
while abs(term)>epsilon:
divBy=nxtInSeq*(nxtInSeq+1)
term=term*multBy/divBy
total+=term
nxtInSeq+=2
return total
def main():
'''
objective: to compute the value of cos(x)
input parameters:
x: enter the number whose cosine value user wants to find out
return value: cos(x)
'''
x=float(input('Enter a number :'))
print('cos(x) = ',myCos(x))
if __name__=='__main__':
main()