Math.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. register_simple_module('Math', {
  2. PI: Math.PI,
  3. E: Math.E,
  4. sqrt: fun (
  5. 'function sqrt (x: Number) -> Number',
  6. x => {
  7. ensure(x >= 0, 'sqrt_of_negative')
  8. return Math.sqrt(x)
  9. }
  10. ),
  11. cbrt: fun (
  12. 'function cbrt (x: Number) -> Number',
  13. x => Math.cbrt(x)
  14. ),
  15. exp: fun (
  16. 'function exp (x: Number) -> Number',
  17. x => Math.exp(x)
  18. ),
  19. log: fun (
  20. 'function log (x: Number) -> Number',
  21. x => {
  22. ensure(x > 0, 'log_of_non_positive')
  23. return Math.log(x)
  24. }
  25. ),
  26. rad2deg: fun (
  27. 'function rad2deg (x: Number) -> Number',
  28. x => 180 * x / Math.PI
  29. ),
  30. deg2rad: fun (
  31. 'function deg2rad (x: Number) -> Number',
  32. x => Math.PI * x / 180
  33. ),
  34. sin: fun (
  35. 'function sin (x: Number) -> Number',
  36. x => Math.sin(x)
  37. ),
  38. cos: fun (
  39. 'function cos (x: Number) -> Number',
  40. x => Math.cos(x)
  41. ),
  42. tan: fun (
  43. 'function tan (x: Number) -> Number',
  44. x => Math.tan(x)
  45. ),
  46. asin: fun (
  47. 'function asin (x: Number) -> Number',
  48. x => {
  49. ensure(-1 <= x && x <= 1, 'asin_out_of_domain')
  50. return Math.asin(x)
  51. }
  52. ),
  53. acos: fun (
  54. 'function acos (x: Number) -> Number',
  55. x => {
  56. ensure(-1 <= x && x <= 1, 'acos_out_of_domain')
  57. return Math.acos(x)
  58. }
  59. ),
  60. atan: fun (
  61. 'function atan (x: Number) -> Number',
  62. x => Math.atan(x),
  63. ),
  64. atan2: fun (
  65. 'function atan2 (y: Number, x: Number) -> Number',
  66. (y, x) => Math.atan2(y, x)
  67. )
  68. })