0

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?

Milo
  • 31
  • 1
  • 2
  • 1
    + http://stackoverflow.com/q/1458633/251311 + http://stackoverflow.com/q/11695618/251311 + ... – zerkms Jun 09 '15 at 04:25
  • 1
    PS: given that `Math.PI` is an *irrational* number, how do you expect it to be represented precisely? – zerkms Jun 09 '15 at 04:26
  • @zerkms thank you for the link to an answer that included a link to a GitHub BigDecimal.js -- I knew that I was dealing with irrational numbers represented with floating points and I was trying to find a way around that problem (which your link to a link does solve -- or at least I hope it does) -- again thanks – Milo Jun 10 '15 at 05:33

0 Answers0