javascript trig functions in degrees (slightly wrong answers)

22 views Asked by At

How do I option CORRECT answers to trigonometric functions in JavaScript when the original input is in degrees?

I understand the standard math formulas for converting back and forth between degrees and radians.

I multiplied the degrees by ( Math.PI/180 ).

I am building a free open source program that includes an RPN calculator ( see http://www.osdata.com/milo.html ).

CODE: answer = firstitem * ( Math.PI / 180 );

I am receiving slightly incorrect answers that tend to indicate floating point math errors during the degrees to radians conversion.

EXAMPLES:

135 degrees shows a tan of -1.0000000000000002 (should be -1)

60 degrees shows a tan of 1.7320508075688767 (should be √3 or about 1.7320508075688767)

90 degrees shows a tan of 16331239353195370 (should be NaN)

Yes, the examples are from the comic FoxTrot.

Is there any way to either:

(1) Force JavaScript to accept degrees as input for trig functions?

or

(2) Automatically correct for these errors before reporting the result to the user?

0

There are 0 answers