Convert value from radians to degrees

767 Views Asked by At

I have programmed the following function:

def horizontal(yAngle, yAcceleration, xAcceleration):
     a = (math.cos(yAngle)*yAcceleration)-(math.sin(yAngle)*xAcceleration)
     return a

The problem is the following: math.cos() and math.sin() return a value in radians. I want that value, which I get from math.cos() and math.sin(), in degrees.

So the calculation should be like this: First the value which comes in math.cos(x) and math.sin(x) instead of x is output in radians. (This means that there are two values at the end, because they are two different functions, which get the same value). These two values should now be converted into degrees. The calculation should then proceed as follows: a = ((Value from cos in degrees) *yAcceleration)-( (Value from sin in degrees) *xAcceleration)

I find out that there are the functions math.degrees and math.radians in Python. I just tried different options but nothing was the right one. Could someone show me how to use it correctly?

How can I reach this? Thanks for helping me!

2

There are 2 best solutions below

0
Jason Furr On

Degrees = 2π * radians so you could use:

def horizontal(yAngle, yAcceleration, xAcceleration): a = (2*math.pi*math.cos(yAngle)*yAcceleration)-(2*math.pi*math.sin(yAngle)*xAcceleration) return a

Good luck!

You will need to repair the formatting, there is not a code block option on my phone.

12
LeopardShark On
def cos_deg(x):
    return math.cos(math.radians(x))


def sin_deg(x):
    return math.sin(math.radians(x))

Note that sin and cos do not return a value in radians, they take a parameter in radians and return a dimensionless value (although radians are technically dimensionless anyway).