0

But when I ran the Math.cos(1) It was different than the cos my calculator returned, would that make a difference with the answer? Will it return a different value?

On my calculator I tested 4 x cos(3) and in js I tested 4 * Math.cos(3).

They were both different, in js these were the first few numbers -3.9599 and on my calc 3.9945

Samir
  • 2,899
  • 4
  • 18
  • 19

3 Answers3

2

Math.cos() expects it's argument in radians. Your calculator is set for degrees.

cos(3) radians = -0.9899

cos(3) degrees = 0.9986

John3136
  • 28,809
  • 4
  • 51
  • 69
2

Math.cos(x), where x is A number given in unit of radians.

I'm assuming you're expecting 0.999(...) [degrees] but are seeing 0.540(...) [radians] coming from JavaScript.

But, you're in luck! There is a questions on SO regarding how to convert to degrees.

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

can you post the 2 values that you got? I blindly think you're dealing with http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems . And if this is the issue, I think you will have to check if the results are within a certain tolerance, as Alnitak said here: How can i make the php cos function return the correct value?

Community
  • 1
  • 1
Nicolae Olariu
  • 2,487
  • 2
  • 18
  • 30