math_function.js 2.7 KB

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