14

I'm using Math.cos and Math.sin but it returns me unexpected results like these:

 Angle   Sin      Cos
 354     0.8414  -0.5403
 352     0.1411   0.98998
 350    -0.958   -0.2836

Why I get these results?

Ali Shakiba
  • 19,721
  • 18
  • 60
  • 87
joanlopez
  • 569
  • 1
  • 5
  • 17
  • For sine: Float x = (float) Math.sin ( Math.toRadians (270 degree) ) For cos: Float x = (float) Math.sin ( Math.toRadians (90 + 270 degree) ) – Bay Oct 13 '19 at 21:32

5 Answers5

62

Are you trying to use degrees? Keep in mind that sin and cos are expecting radians.

Math.cos(Math.toRadians(354))
Pubby
  • 50,432
  • 13
  • 129
  • 175
17

Math.cos and Math.sin take angles in radians, not degrees. So you can use:

double angleInDegree = 354;
double angleInRadian = Math.toRadians(angleInDegree);
double cos = Math.cos(angleInRadian); // cos = 0.9945218953682733
assylias
  • 310,138
  • 72
  • 642
  • 762
5
public static double sin(double a)

Parameters:
a - an angle, in radians.
Returns:
the sine of the argument.
Nikolay Kuznetsov
  • 9,217
  • 12
  • 53
  • 97
3

You can use this also:

degrees *= Math.PI / 180;
Math.cos(degrees)
Khuong
  • 10,526
  • 13
  • 75
  • 108
0

None of the answers fitted my needs. Here's what I had:

A sinus in degree (not radiant). This is how you turn it back into a degree angle:

double angle = Math.toDegree(Math.asin(sinusInDegree));

Explanation: If you have 2 sides of a triangle, it's the only solution that worked for me. You can calcluate everything using the normal degree metric, but you have to convert the Math.asin() back to degree for the right result. :)

codepleb
  • 9,318
  • 13
  • 63
  • 101