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
  • 20,549
  • 18
  • 61
  • 88
joanlopez
  • 579
  • 1
  • 6
  • 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

63

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

Math.cos(Math.toRadians(354))
Pubby
  • 51,882
  • 13
  • 139
  • 180
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
  • 321,522
  • 82
  • 660
  • 783
5
public static double sin(double a)

Parameters:
a - an angle, in radians.
Returns:
the sine of the argument.
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
3

You can use this also:

degrees *= Math.PI / 180;
Math.cos(degrees)
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
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
  • 10,086
  • 14
  • 69
  • 111