MathInstance.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. using System;
  2. using Jint.Native.Number;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. using Jint.Runtime.Interop;
  6. namespace Jint.Native.Math
  7. {
  8. public sealed class MathInstance : ObjectInstance
  9. {
  10. private static readonly Random _random = new Random();
  11. private MathInstance(Engine engine) : base(engine, "Math")
  12. {
  13. }
  14. public static MathInstance CreateMathObject(Engine engine)
  15. {
  16. var math = new MathInstance(engine);
  17. math.Extensible = true;
  18. math.Prototype = engine.Object.PrototypeObject;
  19. return math;
  20. }
  21. public void Configure()
  22. {
  23. FastAddProperty("abs", new ClrFunctionInstance(Engine, Abs), true, false, true);
  24. FastAddProperty("acos", new ClrFunctionInstance(Engine, Acos), true, false, true);
  25. FastAddProperty("asin", new ClrFunctionInstance(Engine, Asin), true, false, true);
  26. FastAddProperty("atan", new ClrFunctionInstance(Engine, Atan), true, false, true);
  27. FastAddProperty("atan2", new ClrFunctionInstance(Engine, Atan2), true, false, true);
  28. FastAddProperty("ceil", new ClrFunctionInstance(Engine, Ceil), true, false, true);
  29. FastAddProperty("cos", new ClrFunctionInstance(Engine, Cos), true, false, true);
  30. FastAddProperty("exp", new ClrFunctionInstance(Engine, Exp), true, false, true);
  31. FastAddProperty("floor", new ClrFunctionInstance(Engine, Floor), true, false, true);
  32. FastAddProperty("log", new ClrFunctionInstance(Engine, Log), true, false, true);
  33. FastAddProperty("max", new ClrFunctionInstance(Engine, Max, 2), true, false, true);
  34. FastAddProperty("min", new ClrFunctionInstance(Engine, Min, 2), true, false, true);
  35. FastAddProperty("pow", new ClrFunctionInstance(Engine, Pow, 2), true, false, true);
  36. FastAddProperty("random", new ClrFunctionInstance(Engine, Random), true, false, true);
  37. FastAddProperty("round", new ClrFunctionInstance(Engine, Round), true, false, true);
  38. FastAddProperty("sin", new ClrFunctionInstance(Engine, Sin), true, false, true);
  39. FastAddProperty("sqrt", new ClrFunctionInstance(Engine, Sqrt), true, false, true);
  40. FastAddProperty("tan", new ClrFunctionInstance(Engine, Tan), true, false, true);
  41. FastAddProperty("E", System.Math.E, false, false, false);
  42. FastAddProperty("LN10", System.Math.Log(10), false, false, false);
  43. FastAddProperty("LN2", System.Math.Log(2), false, false, false);
  44. FastAddProperty("LOG2E", System.Math.Log(System.Math.E, 2), false, false, false);
  45. FastAddProperty("LOG10E", System.Math.Log(System.Math.E, 10), false, false, false);
  46. FastAddProperty("PI", System.Math.PI, false, false, false);
  47. FastAddProperty("SQRT1_2", System.Math.Sqrt(0.5), false, false, false);
  48. FastAddProperty("SQRT2", System.Math.Sqrt(2), false, false, false);
  49. }
  50. private static JsValue Abs(JsValue thisObject, JsValue[] arguments)
  51. {
  52. var x = TypeConverter.ToNumber(arguments.At(0));
  53. if (double.IsNaN(x))
  54. {
  55. return double.NaN;
  56. }
  57. else if (NumberInstance.IsNegativeZero(x))
  58. {
  59. return +0;
  60. }
  61. else if (double.IsInfinity(x))
  62. {
  63. return double.PositiveInfinity;
  64. }
  65. return System.Math.Abs(x);
  66. }
  67. private static JsValue Acos(JsValue thisObject, JsValue[] arguments)
  68. {
  69. var x = TypeConverter.ToNumber(arguments.At(0));
  70. if (double.IsNaN(x) || (x > 1) || (x < -1))
  71. {
  72. return double.NaN;
  73. }
  74. else if (x == 1)
  75. {
  76. return 0;
  77. }
  78. return System.Math.Acos(x);
  79. }
  80. private static JsValue Asin(JsValue thisObject, JsValue[] arguments)
  81. {
  82. var x = TypeConverter.ToNumber(arguments.At(0));
  83. if (double.IsNaN(x) || (x > 1) || (x < -1))
  84. {
  85. return double.NaN;
  86. }
  87. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  88. {
  89. return x;
  90. }
  91. return System.Math.Asin(x);
  92. }
  93. private static JsValue Atan(JsValue thisObject, JsValue[] arguments)
  94. {
  95. var x = TypeConverter.ToNumber(arguments.At(0));
  96. if (double.IsNaN(x))
  97. {
  98. return double.NaN;
  99. }
  100. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  101. {
  102. return x;
  103. }
  104. else if (double.IsPositiveInfinity(x))
  105. {
  106. return System.Math.PI / 2;
  107. }
  108. else if (double.IsNegativeInfinity(x))
  109. {
  110. return -System.Math.PI / 2;
  111. }
  112. return System.Math.Atan(x);
  113. }
  114. private static JsValue Atan2(JsValue thisObject, JsValue[] arguments)
  115. {
  116. var y = TypeConverter.ToNumber(arguments.At(0));
  117. var x = TypeConverter.ToNumber(arguments.At(1));
  118. // If either x or y is NaN, the result is NaN.
  119. if (double.IsNaN(x) || double.IsNaN(y))
  120. {
  121. return double.NaN;
  122. }
  123. if (y > 0 && x.Equals(0))
  124. {
  125. return System.Math.PI/2;
  126. }
  127. if (NumberInstance.IsPositiveZero(y))
  128. {
  129. // If y is +0 and x>0, the result is +0.
  130. if (x > 0)
  131. {
  132. return +0;
  133. }
  134. // If y is +0 and x is +0, the result is +0.
  135. if (NumberInstance.IsPositiveZero(x))
  136. {
  137. return +0;
  138. }
  139. // If y is +0 and x is −0, the result is an implementation-dependent approximation to +π.
  140. if (NumberInstance.IsNegativeZero(x))
  141. {
  142. return System.Math.PI;
  143. }
  144. // If y is +0 and x<0, the result is an implementation-dependent approximation to +π.
  145. if (x < 0)
  146. {
  147. return System.Math.PI;
  148. }
  149. }
  150. if (NumberInstance.IsNegativeZero(y))
  151. {
  152. // If y is −0 and x>0, the result is −0.
  153. if (x > 0)
  154. {
  155. return -0;
  156. }
  157. // If y is −0 and x is +0, the result is −0.
  158. if (NumberInstance.IsPositiveZero(x))
  159. {
  160. return -0;
  161. }
  162. // If y is −0 and x is −0, the result is an implementation-dependent approximation to −π.
  163. if (NumberInstance.IsNegativeZero(x))
  164. {
  165. return -System.Math.PI;
  166. }
  167. // If y is −0 and x<0, the result is an implementation-dependent approximation to −π.
  168. if (x < 0)
  169. {
  170. return -System.Math.PI;
  171. }
  172. }
  173. // If y<0 and x is +0, the result is an implementation-dependent approximation to −π/2.
  174. // If y<0 and x is −0, the result is an implementation-dependent approximation to −π/2.
  175. if (y < 0 && x.Equals(0))
  176. {
  177. return -System.Math.PI/2;
  178. }
  179. // If y>0 and y is finite and x is +∞, the result is +0.
  180. if (y > 0 && !double.IsInfinity(y))
  181. {
  182. if (double.IsPositiveInfinity(x))
  183. {
  184. return +0;
  185. }
  186. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  187. if (double.IsNegativeInfinity(x))
  188. {
  189. return System.Math.PI;
  190. }
  191. }
  192. // If y<0 and y is finite and x is +∞, the result is −0.
  193. // If y<0 and y is finite and x is −∞, the result is an implementation-dependent approximation to −π.
  194. if (y < 0 && !double.IsInfinity(y))
  195. {
  196. if (double.IsPositiveInfinity(x))
  197. {
  198. return -0;
  199. }
  200. // If y>0 and y is finite and x is −∞, the result if an implementation-dependent approximation to +π.
  201. if (double.IsNegativeInfinity(x))
  202. {
  203. return -System.Math.PI;
  204. }
  205. }
  206. // If y is +∞ and x is finite, the result is an implementation-dependent approximation to +π/2.
  207. if (double.IsPositiveInfinity(y) && !double.IsInfinity(x))
  208. {
  209. return System.Math.PI/2;
  210. }
  211. // If y is −∞ and x is finite, the result is an implementation-dependent approximation to −π/2.
  212. if (double.IsNegativeInfinity(y) && !double.IsInfinity(x))
  213. {
  214. return -System.Math.PI / 2;
  215. }
  216. // If y is +∞ and x is +∞, the result is an implementation-dependent approximation to +π/4.
  217. if (double.IsPositiveInfinity(y) && double.IsPositiveInfinity(x))
  218. {
  219. return System.Math.PI/4;
  220. }
  221. // If y is +∞ and x is −∞, the result is an implementation-dependent approximation to +3π/4.
  222. if (double.IsPositiveInfinity(y) && double.IsNegativeInfinity(x))
  223. {
  224. return 3 * System.Math.PI / 4;
  225. }
  226. // If y is −∞ and x is +∞, the result is an implementation-dependent approximation to −π/4.
  227. if (double.IsNegativeInfinity(y) && double.IsPositiveInfinity(x))
  228. {
  229. return -System.Math.PI / 4;
  230. }
  231. // If y is −∞ and x is −∞, the result is an implementation-dependent approximation to −3π/4.
  232. if (double.IsNegativeInfinity(y) && double.IsNegativeInfinity(x))
  233. {
  234. return - 3 * System.Math.PI / 4;
  235. }
  236. return System.Math.Atan2(y, x);
  237. }
  238. private static JsValue Ceil(JsValue thisObject, JsValue[] arguments)
  239. {
  240. var x = TypeConverter.ToNumber(arguments.At(0));
  241. if (double.IsNaN(x))
  242. {
  243. return double.NaN;
  244. }
  245. else if (NumberInstance.IsPositiveZero(x))
  246. {
  247. return +0;
  248. }
  249. else if (NumberInstance.IsNegativeZero(x))
  250. {
  251. return -0;
  252. }
  253. else if (double.IsPositiveInfinity(x))
  254. {
  255. return double.PositiveInfinity;
  256. }
  257. else if (double.IsNegativeInfinity(x))
  258. {
  259. return double.NegativeInfinity;
  260. }
  261. return System.Math.Ceiling(x);
  262. }
  263. private static JsValue Cos(JsValue thisObject, JsValue[] arguments)
  264. {
  265. var x = TypeConverter.ToNumber(arguments.At(0));
  266. if (double.IsNaN(x))
  267. {
  268. return double.NaN;
  269. }
  270. else if (NumberInstance.IsPositiveZero(x))
  271. {
  272. return 1;
  273. }
  274. else if (NumberInstance.IsNegativeZero(x))
  275. {
  276. return 1;
  277. }
  278. else if (double.IsInfinity(x))
  279. {
  280. return double.NaN;
  281. }
  282. return System.Math.Cos(x);
  283. }
  284. private static JsValue Exp(JsValue thisObject, JsValue[] arguments)
  285. {
  286. var x = TypeConverter.ToNumber(arguments.At(0));
  287. if (double.IsNaN(x))
  288. {
  289. return double.NaN;
  290. }
  291. else if (NumberInstance.IsPositiveZero(x) || NumberInstance.IsNegativeZero(x))
  292. {
  293. return 1;
  294. }
  295. else if (double.IsPositiveInfinity(x))
  296. {
  297. return double.PositiveInfinity;
  298. }
  299. else if (double.IsNegativeInfinity(x))
  300. {
  301. return +0;
  302. }
  303. return System.Math.Exp(x);
  304. }
  305. private static JsValue Floor(JsValue thisObject, JsValue[] arguments)
  306. {
  307. var x = TypeConverter.ToNumber(arguments.At(0));
  308. if (double.IsNaN(x))
  309. {
  310. return double.NaN;
  311. }
  312. else if (NumberInstance.IsPositiveZero(x))
  313. {
  314. return +0;
  315. }
  316. else if (NumberInstance.IsNegativeZero(x))
  317. {
  318. return -0;
  319. }
  320. else if (double.IsPositiveInfinity(x))
  321. {
  322. return double.PositiveInfinity;
  323. }
  324. else if (double.IsNegativeInfinity(x))
  325. {
  326. return double.NegativeInfinity;
  327. }
  328. return System.Math.Floor(x);
  329. }
  330. private static JsValue Log(JsValue thisObject, JsValue[] arguments)
  331. {
  332. var x = TypeConverter.ToNumber(arguments.At(0));
  333. if (double.IsNaN(x))
  334. {
  335. return double.NaN;
  336. }
  337. if (x < 0)
  338. {
  339. return double.NaN;
  340. }
  341. else if (x == 0)
  342. {
  343. return double.NegativeInfinity;
  344. }
  345. else if (double.IsPositiveInfinity(x))
  346. {
  347. return double.PositiveInfinity;
  348. }
  349. else if (x == 1)
  350. {
  351. return +0;
  352. }
  353. return System.Math.Log(x);
  354. }
  355. private static JsValue Max(JsValue thisObject, JsValue[] arguments)
  356. {
  357. if (arguments.Length == 0)
  358. {
  359. return Double.NegativeInfinity;
  360. }
  361. double max = TypeConverter.ToNumber(arguments.At(0));
  362. if (double.IsNaN(max))
  363. {
  364. return double.NaN;
  365. }
  366. for (int i = 0; i < arguments.Length; i++)
  367. {
  368. var value = TypeConverter.ToNumber(arguments[i]);
  369. if (double.IsNaN(value))
  370. {
  371. return double.NaN;
  372. }
  373. max = System.Math.Max(max, value);
  374. }
  375. return max;
  376. }
  377. private static JsValue Min(JsValue thisObject, JsValue[] arguments)
  378. {
  379. if (arguments.Length == 0)
  380. {
  381. return Double.PositiveInfinity;
  382. }
  383. double min = TypeConverter.ToNumber(arguments.At(0));
  384. for (int i = 0; i < arguments.Length; i++)
  385. {
  386. min = System.Math.Min(min, TypeConverter.ToNumber(arguments[i]));
  387. }
  388. return min;
  389. }
  390. private static JsValue Pow(JsValue thisObject, JsValue[] arguments)
  391. {
  392. var x = TypeConverter.ToNumber(arguments.At(0));
  393. var y = TypeConverter.ToNumber(arguments.At(1));
  394. if (double.IsNaN(y))
  395. {
  396. return double.NaN;
  397. }
  398. if (y.Equals(0))
  399. {
  400. return 1;
  401. }
  402. if (double.IsNaN(x) && !y.Equals(0))
  403. {
  404. return double.NaN;
  405. }
  406. if (System.Math.Abs(x) > 1)
  407. {
  408. if (double.IsPositiveInfinity(y))
  409. {
  410. return double.PositiveInfinity;
  411. }
  412. if (double.IsNegativeInfinity(y))
  413. {
  414. return +0;
  415. }
  416. }
  417. if (System.Math.Abs(x).Equals(1))
  418. {
  419. if (double.IsInfinity(y))
  420. {
  421. return double.NaN;
  422. }
  423. }
  424. if (System.Math.Abs(x) < 1)
  425. {
  426. if (double.IsPositiveInfinity(y))
  427. {
  428. return 0;
  429. }
  430. if (double.IsNegativeInfinity(y))
  431. {
  432. return double.PositiveInfinity;
  433. }
  434. }
  435. if (double.IsPositiveInfinity(x))
  436. {
  437. if (y > 0)
  438. {
  439. return double.PositiveInfinity;
  440. }
  441. if (y < 0)
  442. {
  443. return +0;
  444. }
  445. }
  446. if (double.IsNegativeInfinity(x))
  447. {
  448. if (y > 0)
  449. {
  450. if (System.Math.Abs(y % 2).Equals(1))
  451. {
  452. return double.NegativeInfinity;
  453. }
  454. return double.PositiveInfinity;
  455. }
  456. if (y < 0)
  457. {
  458. if (System.Math.Abs(y % 2).Equals(1))
  459. {
  460. return -0;
  461. }
  462. return +0;
  463. }
  464. }
  465. if (NumberInstance.IsPositiveZero(x))
  466. {
  467. // If x is +0 and y>0, the result is +0.
  468. if (y > 0)
  469. {
  470. return 0;
  471. }
  472. // If x is +0 and y<0, the result is +∞.
  473. if (y < 0)
  474. {
  475. return double.PositiveInfinity;
  476. }
  477. }
  478. if (NumberInstance.IsNegativeZero(x))
  479. {
  480. if (y > 0)
  481. {
  482. // If x is −0 and y>0 and y is an odd integer, the result is −0.
  483. if (System.Math.Abs(y % 2).Equals(1))
  484. {
  485. return -0;
  486. }
  487. // If x is −0 and y>0 and y is not an odd integer, the result is +0.
  488. return +0;
  489. }
  490. if (y < 0)
  491. {
  492. // If x is −0 and y<0 and y is an odd integer, the result is −∞.
  493. if (System.Math.Abs(y % 2).Equals(1))
  494. {
  495. return double.NegativeInfinity;
  496. }
  497. // If x is −0 and y<0 and y is not an odd integer, the result is +∞.
  498. return double.PositiveInfinity;
  499. }
  500. }
  501. // If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
  502. if (x < 0 && !double.IsInfinity(x) && !double.IsInfinity(y) && !y.Equals((int)y))
  503. {
  504. return double.NaN;
  505. }
  506. return System.Math.Pow(x, y);
  507. }
  508. private static JsValue Random(JsValue thisObject, JsValue[] arguments)
  509. {
  510. return _random.NextDouble();
  511. }
  512. private static JsValue Round(JsValue thisObject, JsValue[] arguments)
  513. {
  514. var x = TypeConverter.ToNumber(arguments.At(0));
  515. var round = System.Math.Round(x);
  516. if (round.Equals(x - 0.5))
  517. {
  518. return round + 1;
  519. }
  520. return round;
  521. }
  522. private static JsValue Sin(JsValue thisObject, JsValue[] arguments)
  523. {
  524. var x = TypeConverter.ToNumber(arguments.At(0));
  525. if (double.IsNaN(x))
  526. {
  527. return double.NaN;
  528. }
  529. else if (NumberInstance.IsPositiveZero(x))
  530. {
  531. return +0;
  532. }
  533. else if (NumberInstance.IsNegativeZero(x))
  534. {
  535. return -0;
  536. }
  537. else if (double.IsInfinity(x))
  538. {
  539. return double.NaN;
  540. }
  541. return System.Math.Sin(x);
  542. }
  543. private static JsValue Sqrt(JsValue thisObject, JsValue[] arguments)
  544. {
  545. var x = TypeConverter.ToNumber(arguments.At(0));
  546. return System.Math.Sqrt(x);
  547. }
  548. private static JsValue Tan(JsValue thisObject, JsValue[] arguments)
  549. {
  550. var x = TypeConverter.ToNumber(arguments.At(0));
  551. return System.Math.Tan(x);
  552. }
  553. }
  554. }