math_function.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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,other=n-r,choice=r;
  29. if(choice<other){
  30. choice=other;
  31. other=r;
  32. }
  33. for(var i=choice+1;i<=n;i++)
  34. pro*=i;
  35. return pro/Mexp.math.fact(other);
  36. },
  37. changeSign:function(x){
  38. return -x;
  39. },
  40. cos:function(x){
  41. if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
  42. return Math.cos(x);
  43. },
  44. cosh:function(x){
  45. return (Math.pow(Math.E,x)+Math.pow(Math.E,-1*x))/2;
  46. },
  47. div:function(a,b){
  48. return a/b;
  49. },
  50. fact:function(n) {
  51. if(n%1!==0)return "NAN";
  52. var pro=1;
  53. for(var i=2;i<=n;i++)
  54. pro*=i;
  55. return pro;
  56. },
  57. inverse:function(x){
  58. return 1/x;
  59. },
  60. log:function(i){
  61. return Math.log(i)/Math.log(10);
  62. },
  63. mod:function(a,b){
  64. return a%b;
  65. },
  66. mul:function(a,b){
  67. return a*b;
  68. },
  69. P:function(n,r){var pro=1;
  70. for(var i=Math.floor(n)-Math.floor(r)+1;i<=Math.floor(n);i++)
  71. pro*=i;
  72. return pro;
  73. },
  74. Pi:function(low,high,ex){
  75. var pro=1;
  76. for(var i=low;i<=high;i++){
  77. pro*=Number(ex.postfixEval({n:i}));
  78. }
  79. return pro;
  80. },
  81. pow10x:function(e){
  82. var x=1;
  83. while(e--){x*=10;}
  84. return x;
  85. },
  86. sigma:function(low,high,ex){
  87. var sum=0;
  88. for(var i=low;i<=high;i++){
  89. sum+=Number(ex.postfixEval({n:i}));
  90. }
  91. return sum;
  92. },
  93. sin:function(x){
  94. if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
  95. return Math.sin(x);
  96. },
  97. sinh:function(x){
  98. return (Math.pow(Math.E,x)-Math.pow(Math.E,-1*x))/2;
  99. },
  100. sub:function(a,b){
  101. return a-b;
  102. },
  103. tan:function(x){
  104. if(Mexp.math.isDegree)x=Mexp.math.toRadian(x);
  105. return Math.tan(x);
  106. },
  107. tanh:function(x){
  108. return Mexp.sinha(x)/Mexp.cosha(x);
  109. },
  110. toRadian:function(x){
  111. return x*Math.PI/180;
  112. }
  113. };
  114. Mexp.exception=function(message){
  115. this.message=message;
  116. };
  117. module.exports=Mexp;