40

According to Wolfram Mathematica: cos(50) = 0.6427876096865394;

But this code in Java:

    System.out.println(Math.cos(50));

gives 0.9649660284921133.

What is wrong with java.lang.Math?

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
z3on
  • 411
  • 1
  • 4
  • 4

5 Answers5

100

Math.cos() expects the parameter to be in radians. This will return the result you need:

Math.cos(Math.toRadians(50));
Dan D.
  • 32,246
  • 5
  • 63
  • 79
13

Math.cos() uses radians, so to get your expected result you need to do

System.out.println(Math.cos(Math.toRadians(50)));
Keppil
  • 45,603
  • 8
  • 97
  • 119
2

Degrees <> radians...........

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • What this answer means ? Degree is equivalent to radian... but `1 degree = 0.0174532925 radians` – Manu Oct 20 '12 at 16:15
  • 3
    @Manu The `but` part in your comment is why they are not equivalent ;) It's like saying kilos and stone are equivalent. They measure the same quantity, sure, but in different ways. – phant0m Oct 20 '12 at 18:12
2

Most Java trigonometric functions expects parameters to be in radians. You can use Math.toRadians() to convert:

System.out.println(Math.cos(Math.toRadians(50)));
fbafelipe
  • 4,862
  • 2
  • 25
  • 40
-2

For me...

System.out.println(Math.cos(50));
System.out.println(Math.cos(new Double(50)));
System.out.println(Math.cos(Math.toRadians(50)));
System.out.println(Math.cos(Math.toRadians(new Double(50))));

returns

0.9649660284921133
0.9649660284921133
0.6427876096865394
0.6427876096865394



http://www.wolframalpha.com/input/?i=cos%2850deg%29

cos(50deg) give same result as cos(50)... so Wolfram is degree by default.

Manu
  • 809
  • 1
  • 9
  • 28
  • Wolfram Alpha does not use degrees by default! Rather, it is trying to be clever. – phant0m Oct 19 '12 at 19:36
  • What is Wolfram Alpha default, degree or radian or ... ? Is there another base ? Can you give more argument about what you write ? Your comment does not give any information in fact ! My response does not respond to the initial question, so ok for "-1" flag but It give more information usefull about this conversation, I think. – Manu Oct 20 '12 at 16:13
  • Wolfram Alpha tries to guess whether your input is in radians or degrees: If the value is below 5, it seems to interpret it as radians, if it's larger or equal than 5, it interprets it as degrees ;) I have taken the liberty to correct the output that Java actually produces upon executing that code. – phant0m Oct 20 '12 at 18:03
  • My `-1` was mainly due to this: `I don't know exactly what the meaning of these results but for me...`, apart from it not being a proper answer to the question. Given your code, you seem to try to guess what is happening, instead of having an understanding for it. This is further demonstrated by this: `Math.cos(Math.toDegrees(50))` You feed a function that expects radians the value, that you get after converting 50 radians to degrees, a value that is completely random. – phant0m Oct 20 '12 at 18:10
  • I'm agree with you... It is a mistake from me to feed by degree a method that expect radian. Like I said, I agree with people who give -1 AND give information about this : I expect it cannot be so easy to give -1. So thanks for explain, @phant0m. I will correct my answer so. – Manu Oct 22 '12 at 07:22
  • Do I have to aks an other question and make a relation between mine and this question if I try/want to understand why these method return different results ? – Manu Oct 22 '12 at 07:25
  • `new Double(50)` servers no purpose at all. – bestsss Oct 25 '12 at 17:17