-1

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!

mathflower
  • 109
  • 7

2 Answers2

1
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).

LeopardShark
  • 3,820
  • 2
  • 19
  • 33
  • Thank you for these both functions. How can I use them now? – mathflower Jun 17 '20 at 16:03
  • If you copy them into your code somewhere near the start, you can call `a = (cos_deg(yAngle)*yAcceleration)-(sin_deg(yAngle)*xAcceleration)`, for example. – LeopardShark Jun 17 '20 at 16:22
  • I think that's not that what I really wanted. I meant: I get a value in radians from m.cos(x). This radian value should then be converted to degrees and then inserted into the equation. – mathflower Jun 17 '20 at 21:41
  • @mathflower `math.cos(x)` doesn’t return a value in radians, it returns a dimensionless value (−1 to 1). If you want, you can call `math.degrees` on it, but that wouldn’t really make sense. – LeopardShark Jun 17 '20 at 21:46
  • In the documentation of math states that math.cos(x) return the cosine of x radians. So okay: I get a value x. This is written into the function m.cos(). The value I get from this, this should be converted to degrees and then inserted into the equation. – mathflower Jun 17 '20 at 21:52
  • @mathflower “Return the cosine of x radians” means “Return the cosine of (x radians)”, not “Return (the cosine of x) radians”. The value `math.cos` returns isn’t an angle, so you can’t measure it in degrees or radians. It’s a ratio of two lengths. [This](https://betterexplained.com/articles/intuitive-trigonometry/) is a good article for an in-depth explanation of the trigonometric functions. – LeopardShark Jun 17 '20 at 22:00
  • If I write `math.cos?` in the console then it says "Return the cosine of x (measured in radians)." Okay, anyway, whatever you say doesn't matter what x is. I just want to convert the value that comes out after math.cos(x) into degrees. – mathflower Jun 18 '20 at 08:39
  • You can do `math.degrees(math.cos(x))`, which would pretend that the result of `math.cos(x)` was in radians and then convert it into degrees. This is unlikely to achieve anything useful, though. What are you actually trying to achieve with this function? – LeopardShark Jun 18 '20 at 09:21
  • It's complicated to say what I intend to do with it. It's just a formula. But the value that comes out at m.cos(x) at the end should be converted to degrees. – mathflower Jun 18 '20 at 09:39
  • You can’t convert the result of `math.cos(x)` to degrees in a meaningful way, because a degree is a measure of an angle, and `math.cos` doesn’t return an angle. It’s like trying to convert between metres and seconds. The closest you can do, probably, is `math.degrees(math.cos(x))` (or `math.degrees(math.cos(math.radians(x)))` if `x` is in degrees) but if you’re having to do this, there’s probably something wrong elsewhere. – LeopardShark Jun 18 '20 at 09:50
  • so it should be like this for example: `x = math.cos(5)` this is 0.28366218546322625. Then `y = m.degrees(0.28366218546322625)` and this is 16.252646034500078. Hereby I have converted the value of math.cos(x) to degrees. And that is what I want to have for my equation. So I thin that `math.degrees(math.cos(x))` is the right way. – mathflower Jun 18 '20 at 09:51
  • If you want to do that, then your equation could just be `a = (math.degrees(math.cos(yAngle))*yAcceleration)-(math.degrees(math.sin(yAngle))*xAcceleration)` (or `a = (math.degrees(math.cos(math.radians(yAngle)))*yAcceleration)-(math.degrees(math.sin(math.radians(yAngle)))*xAcceleration)` if `yAngle` is in degrees), but it doesn’t make sense to think of this as converting `math.cos(x)` to degrees—because that is only a thing you can do with angles—it’s just multiplying it by 180 ∕ π (which is about 57.3). – LeopardShark Jun 18 '20 at 09:56
0

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.

Jason Furr
  • 317
  • 3
  • 2