Math.cos() gives wrong result

48,588

Solution 1

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

Math.cos(Math.toRadians(50));

Solution 2

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

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

Solution 3

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)));

Solution 4

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

Share:
48,588
z3on
Author by

z3on

Updated on March 09, 2020

Comments

  • z3on
    z3on about 4 years

    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?

  • Ignacio Contreras Pinilla
    Ignacio Contreras Pinilla over 11 years
    +1. Just for reference, as OP was talking about wolfram wolframalpha.com/input/?i=cos%2850rad%29
  • phant0m
    phant0m over 11 years
    Wolfram Alpha does not use degrees by default! Rather, it is trying to be clever.
  • Manu
    Manu over 11 years
    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
    Manu over 11 years
    What this answer means ? Degree is equivalent to radian... but 1 degree = 0.0174532925 radians
  • phant0m
    phant0m over 11 years
    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
    phant0m over 11 years
    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
    phant0m over 11 years
    @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.
  • Manu
    Manu over 11 years
    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
    Manu over 11 years
    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 ?
  • bestsss
    bestsss over 11 years
    new Double(50) servers no purpose at all.
  • Stevoisiak
    Stevoisiak about 7 years
    Is there a version of Math.cos() in Java that works with Degrees, or do we constantly need to remember to manually convert degrees to radians?