input.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * License: Public domain
  3. *
  4. * This file is evaluated by ECMAScript engines before evaluating expressions
  5. * entered in input fields or the command line.
  6. */
  7. // add common functions of Math to global scope for use in math input fields:
  8. PI = Math.PI;
  9. LN2 = Math.LN2;
  10. LN10 = Math.LN10;
  11. LOG2E = Math.LOG2E;
  12. LOG10E = Math.LOG10E;
  13. SQRT1_2 = Math.SQRT1_2;
  14. SQRT2 = Math.SQRT2;
  15. abs = Math.abs;
  16. ceil = Math.ceil;
  17. floor = Math.floor;
  18. exp = Math.exp;
  19. log = Math.log;
  20. max = Math.max;
  21. min = Math.min;
  22. pow = Math.pow;
  23. sqrt = Math.sqrt;
  24. random = Math.random;
  25. round = Math.round;
  26. // converting rad to deg:
  27. rad2deg = function(a) { return a / (2.0 * Math.PI) * 360.0; };
  28. // converting deg to rad:
  29. deg2rad = function(a) { return (a / 360.0) * (2.0 * Math.PI); };
  30. // trigonometry (for convenience in degrees!):
  31. sin = function(v) { return Math.sin(deg2rad(v)); };
  32. cos = function(v) { return Math.cos(deg2rad(v)); };
  33. tan = function(v) { return Math.tan(deg2rad(v)); };
  34. asin = function(v) { return rad2deg(Math.asin(v)); };
  35. acos = function(v) { return rad2deg(Math.acos(v)); };
  36. atan = function(v) { return rad2deg(Math.atan(v)); };
  37. atan2 = function(y,x) { return rad2deg(Math.atan2(y,x)); };
  38. // polyfills for ECMAScript 2015 (ES6):
  39. log10 = Math.log10 = function(x) { return Math.log(x) / Math.LN10; };
  40. log1p = Math.log1p = function(x) { return Math.log(1 + x); };
  41. log2 = Math.log2 = function(x) { return Math.log(x) / Math.LN2; };
  42. sign = Math.sign = function(x) { x = +x; if (x === 0 || isNaN(x)) { return Number(x); } return x > 0 ? 1 : -1; };
  43. cosh = Math.cosh = function(x) { return (Math.exp(x) + Math.exp(-x)) / 2; };
  44. sinh = Math.sinh = function(x) { return (Math.exp(x) - Math.exp(-x)) / 2; };
  45. tanh = Math.tanh = function(x) { if (x === Infinity) { return 1; } else if (x === -Infinity) { return -1; } else { return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x)); } };
  46. acosh = Math.acosh = function(x) { return Math.log(x + Math.sqrt(x * x - 1)); };
  47. asinh = Math.asinh = function(x) { if (x === -Infinity) { return x; } else { return Math.log(x + Math.sqrt(x * x + 1)); } };
  48. atanh = Math.atanh = function(x) { return Math.log((1+x)/(1-x)) / 2; };
  49. expm1 = Math.expm1 = function(x) { return Math.exp(x) - 1; };
  50. hypot = Math.hypot = function() { var y = 0; var length = arguments.length; for (var i = 0; i < length; i++) { if (arguments[i] === Infinity || arguments[i] === -Infinity) { return Infinity; } y += arguments[i] * arguments[i]; } return Math.sqrt(y); };
  51. cbrt = Math.cbrt = function(x) { var y = Math.pow(Math.abs(x), 1/3); return x < 0 ? -y : y; };
  52. trunc = Math.trunc = function(x) { return x < 0 ? Math.ceil(x) : Math.floor(x); };