NEMM2020
NEMM2020

Reputation: 129

Python: Converting degrees to radians - Not working

I have tried doing the following in Python with numpy but my answer is not coming out to the value I want. I want cos(30) = 0.866

import math
import numpy as np

print(math.degrees(np.cos(30)))
print(np.cos(math.degrees(30)))
print(math.degrees(math.cos(30)))
print(math.cos(math.degrees(30)))

Any help would be appreciated, thank you

Upvotes: 1

Views: 6031

Answers (5)

AntiMatterDynamite
AntiMatterDynamite

Reputation: 1512

math.degrees converts radians to degrees but you are obviously giving it degrees, you want to use math.radians to convert degrees to radians

print(np.cos(math.radians(30))

Upvotes: 3

Benyamin Jafari
Benyamin Jafari

Reputation: 34236

Manual approach:

import math


def to_radian(degree):
    return degree * math.pi/180

print(math.cos(to_radian(30)))


Out[0]: 0.866025403784

Upvotes: -1

Aditi
Aditi

Reputation: 828

Try this:

from math import cos, radians
print(cos(radians(30)))

Output:

0.8660254037844387

Upvotes: 0

Sociopath
Sociopath

Reputation: 13426

Only using math module

import math
print(math.cos(math.radians(30)))

>>0.8660254037844387

Upvotes: 0

Mitiku
Mitiku

Reputation: 5412

python math.cos expects angle in radian. so you first have to change degrees to radians.

radian = math.radians(30)
print(math.cos(radian))
# 0.866025403784

Upvotes: 7

Related Questions