a865a21fedf64545af3935653a37e93e4776553c42b95fdda7e467526e054b67050dfe02a31f352e4e42848285371534d0c1bb64340bfb3beab4e71c160b1b 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var Mexp = function (parsed) {
  3. this.value = parsed
  4. }
  5. Mexp.math = {
  6. isDegree: true, // mode of calculator
  7. acos: function (x) {
  8. return (Mexp.math.isDegree ? 180 / Math.PI * Math.acos(x) : Math.acos(x))
  9. },
  10. add: function (a, b) {
  11. return a + b
  12. },
  13. asin: function (x) {
  14. return (Mexp.math.isDegree ? 180 / Math.PI * Math.asin(x) : Math.asin(x))
  15. },
  16. atan: function (x) {
  17. return (Mexp.math.isDegree ? 180 / Math.PI * Math.atan(x) : Math.atan(x))
  18. },
  19. acosh: function (x) {
  20. return Math.log(x + Math.sqrt(x * x - 1))
  21. },
  22. asinh: function (x) {
  23. return Math.log(x + Math.sqrt(x * x + 1))
  24. },
  25. atanh: function (x) {
  26. return Math.log((1 + x) / (1 - x))
  27. },
  28. C: function (n, r) {
  29. var pro = 1
  30. var other = n - r
  31. var choice = r
  32. if (choice < other) {
  33. choice = other
  34. other = r
  35. }
  36. for (var i = choice + 1; i <= n; i++) {
  37. pro *= i
  38. }
  39. return pro / Mexp.math.fact(other)
  40. },
  41. changeSign: function (x) {
  42. return -x
  43. },
  44. cos: function (x) {
  45. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  46. return Math.cos(x)
  47. },
  48. cosh: function (x) {
  49. return (Math.pow(Math.E, x) + Math.pow(Math.E, -1 * x)) / 2
  50. },
  51. div: function (a, b) {
  52. return a / b
  53. },
  54. fact: function (n) {
  55. if (n % 1 !== 0) return 'NaN'
  56. var pro = 1
  57. for (var i = 2; i <= n; i++) {
  58. pro *= i
  59. }
  60. return pro
  61. },
  62. inverse: function (x) {
  63. return 1 / x
  64. },
  65. log: function (i) {
  66. return Math.log(i) / Math.log(10)
  67. },
  68. mod: function (a, b) {
  69. return a % b
  70. },
  71. mul: function (a, b) {
  72. return a * b
  73. },
  74. P: function (n, r) {
  75. var pro = 1
  76. for (var i = Math.floor(n) - Math.floor(r) + 1; i <= Math.floor(n); i++) {
  77. pro *= i
  78. }
  79. return pro
  80. },
  81. Pi: function (low, high, ex) {
  82. var pro = 1
  83. for (var i = low; i <= high; i++) {
  84. pro *= Number(ex.postfixEval({
  85. n: i
  86. }))
  87. }
  88. return pro
  89. },
  90. pow10x: function (e) {
  91. var x = 1
  92. while (e--) {
  93. x *= 10
  94. }
  95. return x
  96. },
  97. sigma: function (low, high, ex) {
  98. var sum = 0
  99. for (var i = low; i <= high; i++) {
  100. sum += Number(ex.postfixEval({
  101. n: i
  102. }))
  103. }
  104. return sum
  105. },
  106. sin: function (x) {
  107. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  108. return Math.sin(x)
  109. },
  110. sinh: function (x) {
  111. return (Math.pow(Math.E, x) - Math.pow(Math.E, -1 * x)) / 2
  112. },
  113. sub: function (a, b) {
  114. return a - b
  115. },
  116. tan: function (x) {
  117. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  118. return Math.tan(x)
  119. },
  120. tanh: function (x) {
  121. return Mexp.sinha(x) / Mexp.cosha(x)
  122. },
  123. toRadian: function (x) {
  124. return x * Math.PI / 180
  125. },
  126. and: function (a, b) {
  127. return a & b
  128. }
  129. }
  130. Mexp.Exception = function (message) {
  131. this.message = message
  132. }
  133. module.exports = Mexp