-1

I tried the code .The answer should be 0.66913060635885821382627333068678 by calculator.

But I use netbean to run Java code to calculate eh -0.9165215479156338

 time = Math.sin(42.0);

System.out.println(time); 

Why?

Vito
  • 299
  • 3
  • 19
  • Tip: if you find yourself writing "X doesn't work in Java" - particularly if it's something so fundamental - you should probably think "I'm using X wrongly" instead. [Look at the Javadoc](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sin(double)), and find out what you're doing wrong. – Andy Turner Oct 06 '16 at 10:31

2 Answers2

8

Because you're calculating the sine of 42 radians.

Try converting to radians first:

time = Math.sin(Math.toRadians(42.0));
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

As per the java doc of the method.

Parameters:

  • a - an angle, in radians.

Returns:

the sine of the argument.

The values you see is for sin of 42 radians not for sin of 42 degrees

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
nits.kk
  • 5,204
  • 4
  • 33
  • 55