math.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { StoreObject } from '../store/storeObject';
  2. import * as Commands from './../../ast/commands';
  3. import { Types, toReal } from './../../ast/types';
  4. import { BigNumber } from 'bignumber.js';
  5. /**
  6. * sin
  7. * cos
  8. * tan
  9. * sqrt
  10. * pow
  11. * log
  12. * abs
  13. * negate
  14. * invert
  15. * max
  16. * min
  17. */
  18. export function createSinFun () {
  19. const sinFun = (sto, _) => {
  20. const x = sto.applyStore('x');
  21. const result = toReal(Math.sin(x.number));
  22. const temp = new StoreObject(Types.REAL, result);
  23. return Promise.resolve(sto.updateStore('$', temp));
  24. };
  25. const block = new Commands.CommandBlock([], [new Commands.SysCall(sinFun)]);
  26. const func = new Commands.Function('$sin', Types.REAL,
  27. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  28. block);
  29. return func;
  30. }
  31. export function createCosFun () {
  32. const cosFun = (sto, _) => {
  33. const x = sto.applyStore('x');
  34. const result = toReal(Math.cos(x.number));
  35. const temp = new StoreObject(Types.REAL, result);
  36. return Promise.resolve(sto.updateStore('$', temp));
  37. };
  38. const block = new Commands.CommandBlock([], [new Commands.SysCall(cosFun)]);
  39. const func = new Commands.Function('$cos', Types.REAL,
  40. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  41. block);
  42. return func;
  43. }
  44. export function createTanFun () {
  45. const tanFun = (sto, _) => {
  46. const x = sto.applyStore('x');
  47. const result = toReal(Math.tan(x.number));
  48. const temp = new StoreObject(Types.REAL, result);
  49. return Promise.resolve(sto.updateStore('$', temp));
  50. };
  51. const block = new Commands.CommandBlock([], [new Commands.SysCall(tanFun)]);
  52. const func = new Commands.Function('$tan', Types.REAL,
  53. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  54. block);
  55. return func;
  56. }
  57. export function createSqrtFun () {
  58. const sqrtFun = (sto, _) => {
  59. const x = sto.applyStore('x');
  60. const result = x.value.sqrt();
  61. const temp = new StoreObject(Types.REAL, result);
  62. return Promise.resolve(sto.updateStore('$', temp));
  63. };
  64. const block = new Commands.CommandBlock([], [new Commands.SysCall(sqrtFun)]);
  65. const func = new Commands.Function('$sqrt', Types.REAL,
  66. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  67. block);
  68. return func;
  69. }
  70. export function createPowFun () {
  71. const powFun = (sto, _) => {
  72. const x = sto.applyStore('x');
  73. const y = sto.applyStore('y');
  74. const result = toReal(Math.pow(x.number, y.number));
  75. const temp = new StoreObject(Types.REAL, result);
  76. return Promise.resolve(sto.updateStore('$', temp));
  77. };
  78. const block = new Commands.CommandBlock([], [new Commands.SysCall(powFun)]);
  79. const func = new Commands.Function('$pow', Types.REAL,
  80. [new Commands.FormalParameter(Types.REAL, 'x', 0, false),
  81. new Commands.FormalParameter(Types.REAL, 'y', 0, false)],
  82. block);
  83. return func;
  84. }
  85. export function createLogFun () {
  86. const logFun = (sto, _) => {
  87. const x = sto.applyStore('x');
  88. if (x.isNegative()) {
  89. return Promise.reject("the value passed to log function cannot be negative");
  90. }
  91. const result = toReal(Math.log10(x.number));
  92. const temp = new StoreObject(Types.REAL, result);
  93. return Promise.resolve(sto.updateStore('$', temp));
  94. };
  95. const block = new Commands.CommandBlock([], [new Commands.SysCall(logFun)]);
  96. const func = new Commands.Function('$log', Types.REAL,
  97. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  98. block);
  99. return func;
  100. }
  101. export function createAbsFun () {
  102. const absFun = (sto, _) => {
  103. const x = sto.applyStore('x');
  104. const result = x.value.abs();
  105. const temp = new StoreObject(Types.REAL, result);
  106. return Promise.resolve(sto.updateStore('$', temp));
  107. };
  108. const block = new Commands.CommandBlock([], [new Commands.SysCall(absFun)]);
  109. const func = new Commands.Function('$abs', Types.REAL,
  110. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  111. block);
  112. return func;
  113. }
  114. export function createNegateFun () {
  115. const negateFun = (sto, _) => {
  116. const x = sto.applyStore('x');
  117. const result = x.value.negated();
  118. const temp = new StoreObject(Types.REAL, result);
  119. return Promise.resolve(sto.updateStore('$', temp));
  120. };
  121. const block = new Commands.CommandBlock([], [new Commands.SysCall(negateFun)]);
  122. const func = new Commands.Function('$negate', Types.REAL,
  123. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  124. block);
  125. return func;
  126. }
  127. export function createInvertFun () {
  128. const invertFun = (sto, _) => {
  129. const x = sto.applyStore('x');
  130. const result = toReal(1).dividedBy(x.value);
  131. const temp = new StoreObject(Types.REAL, result);
  132. return Promise.resolve(sto.updateStore('$', temp));
  133. };
  134. const block = new Commands.CommandBlock([], [new Commands.SysCall(invertFun)]);
  135. const func = new Commands.Function('$invert', Types.REAL,
  136. [new Commands.FormalParameter(Types.REAL, 'x', 0, false)],
  137. block);
  138. return func;
  139. }
  140. export function createMaxFun () {
  141. const minFun = (sto, _) => {
  142. const x = sto.applyStore('x');
  143. const y = sto.applyStore('y');
  144. const result = BigNumber.max(x.value, y.value);
  145. const temp = new StoreObject(Types.REAL, result);
  146. return Promise.resolve(sto.updateStore('$', temp));
  147. };
  148. const block = new Commands.CommandBlock([], [new Commands.SysCall(maxFun)]);
  149. const func = new Commands.Function('$max', Types.ALL,
  150. [new Commands.FormalParameter(Types.ALL, 'x', 0, false),
  151. new Commands.FormalParameter(Types.ALL, 'y', 0, false)],
  152. block);
  153. return func;
  154. }
  155. export function createMinFun () {
  156. const minFun = (sto, _) => {
  157. const x = sto.applyStore('x');
  158. const y = sto.applyStore('y');
  159. const result = BigNumber.max(x.value, y.value);
  160. const temp = new StoreObject(Types.REAL, result);
  161. return Promise.resolve(sto.updateStore('$', temp));
  162. };
  163. const block = new Commands.CommandBlock([], [new Commands.SysCall(minFun)]);
  164. const func = new Commands.Function('$min', Types.ALL,
  165. [new Commands.FormalParameter(Types.ALL, 'x', 0, false),
  166. new Commands.FormalParameter(Types.ALL, 'y', 0, false)],
  167. block);
  168. return func;
  169. }