The problem is this. I need to calculate the cosine of the angle. BUT an error occurred and I decided to check its calculation.

public class Test { public static void main(String[] args) { double a = Math.cos(89); System.out.println(a); } } 

To my surprise, the compiler issued 0.5101770449416689, although calculating on the calculator, I received 0.01745240643. What is the problem?

  • 2
    Apparently in understanding what degree is different from radian? - pavel

1 answer 1

Here's how to correctly count the cosine:

 double degrees = 89.0; double radians = Math.toRadians(degrees); System.out.format("The value of pi is %.4f%n", Math.PI); System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians)); 

Conclusion:

 The value of pi is 3.1416 The cosine of 89.0 degrees is 0.0175 
  • and if you just need to assign the cosine value of a variable and not output it? - Dmitriy Mironov
  • Assign the variable to: Math.cos(radians) - Vyacheslav Martynenko