MathInstance.cs 20 KB

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