MathObject.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/BuiltinWrappers.h>
  9. #include <AK/Function.h>
  10. #include <AK/Random.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/MathObject.h>
  13. #include <math.h>
  14. namespace JS {
  15. MathObject::MathObject(Realm& realm)
  16. : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
  17. {
  18. }
  19. ThrowCompletionOr<void> MathObject::initialize(Realm& realm)
  20. {
  21. auto& vm = this->vm();
  22. MUST_OR_THROW_OOM(Base::initialize(realm));
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(realm, vm.names.abs, abs, 1, attr);
  25. define_native_function(realm, vm.names.random, random, 0, attr);
  26. define_native_function(realm, vm.names.sqrt, sqrt, 1, attr);
  27. define_native_function(realm, vm.names.floor, floor, 1, attr);
  28. define_native_function(realm, vm.names.ceil, ceil, 1, attr);
  29. define_native_function(realm, vm.names.round, round, 1, attr);
  30. define_native_function(realm, vm.names.max, max, 2, attr);
  31. define_native_function(realm, vm.names.min, min, 2, attr);
  32. define_native_function(realm, vm.names.trunc, trunc, 1, attr);
  33. define_native_function(realm, vm.names.sin, sin, 1, attr);
  34. define_native_function(realm, vm.names.cos, cos, 1, attr);
  35. define_native_function(realm, vm.names.tan, tan, 1, attr);
  36. define_native_function(realm, vm.names.pow, pow, 2, attr);
  37. define_native_function(realm, vm.names.exp, exp, 1, attr);
  38. define_native_function(realm, vm.names.expm1, expm1, 1, attr);
  39. define_native_function(realm, vm.names.sign, sign, 1, attr);
  40. define_native_function(realm, vm.names.clz32, clz32, 1, attr);
  41. define_native_function(realm, vm.names.acos, acos, 1, attr);
  42. define_native_function(realm, vm.names.acosh, acosh, 1, attr);
  43. define_native_function(realm, vm.names.asin, asin, 1, attr);
  44. define_native_function(realm, vm.names.asinh, asinh, 1, attr);
  45. define_native_function(realm, vm.names.atan, atan, 1, attr);
  46. define_native_function(realm, vm.names.atanh, atanh, 1, attr);
  47. define_native_function(realm, vm.names.log1p, log1p, 1, attr);
  48. define_native_function(realm, vm.names.cbrt, cbrt, 1, attr);
  49. define_native_function(realm, vm.names.atan2, atan2, 2, attr);
  50. define_native_function(realm, vm.names.fround, fround, 1, attr);
  51. define_native_function(realm, vm.names.hypot, hypot, 2, attr);
  52. define_native_function(realm, vm.names.imul, imul, 2, attr);
  53. define_native_function(realm, vm.names.log, log, 1, attr);
  54. define_native_function(realm, vm.names.log2, log2, 1, attr);
  55. define_native_function(realm, vm.names.log10, log10, 1, attr);
  56. define_native_function(realm, vm.names.sinh, sinh, 1, attr);
  57. define_native_function(realm, vm.names.cosh, cosh, 1, attr);
  58. define_native_function(realm, vm.names.tanh, tanh, 1, attr);
  59. // 21.3.1 Value Properties of the Math Object, https://tc39.es/ecma262/#sec-value-properties-of-the-math-object
  60. define_direct_property(vm.names.E, Value(M_E), 0);
  61. define_direct_property(vm.names.LN2, Value(M_LN2), 0);
  62. define_direct_property(vm.names.LN10, Value(M_LN10), 0);
  63. define_direct_property(vm.names.LOG2E, Value(::log2(M_E)), 0);
  64. define_direct_property(vm.names.LOG10E, Value(::log10(M_E)), 0);
  65. define_direct_property(vm.names.PI, Value(M_PI), 0);
  66. define_direct_property(vm.names.SQRT1_2, Value(M_SQRT1_2), 0);
  67. define_direct_property(vm.names.SQRT2, Value(M_SQRT2), 0);
  68. // 21.3.1.9 Math [ @@toStringTag ], https://tc39.es/ecma262/#sec-math-@@tostringtag
  69. define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, vm.names.Math.as_string()), Attribute::Configurable);
  70. return {};
  71. }
  72. // 21.3.2.1 Math.abs ( x ), https://tc39.es/ecma262/#sec-math.abs
  73. JS_DEFINE_NATIVE_FUNCTION(MathObject::abs)
  74. {
  75. // Let n be ? ToNumber(x).
  76. auto number = TRY(vm.argument(0).to_number(vm));
  77. // 2. If n is NaN, return NaN.
  78. if (number.is_nan())
  79. return js_nan();
  80. // 3. If n is -0𝔽, return +0𝔽.
  81. if (number.is_negative_zero())
  82. return Value(0);
  83. // 4. If n is -∞𝔽, return +∞𝔽.
  84. if (number.is_negative_infinity())
  85. return js_infinity();
  86. // 5. If n < -0𝔽, return -n.
  87. // 6. Return n.
  88. return Value(number.as_double() < 0 ? -number.as_double() : number.as_double());
  89. }
  90. // 21.3.2.2 Math.acos ( x ), https://tc39.es/ecma262/#sec-math.acos
  91. JS_DEFINE_NATIVE_FUNCTION(MathObject::acos)
  92. {
  93. // 1. Let n be ? ToNumber(x).
  94. auto number = TRY(vm.argument(0).to_number(vm));
  95. // 2. If n is NaN, n > 1𝔽, or n < -1𝔽, return NaN.
  96. if (number.is_nan() || number.as_double() > 1 || number.as_double() < -1)
  97. return js_nan();
  98. // 3. If n is 1𝔽, return +0𝔽.
  99. if (number.as_double() == 1)
  100. return Value(0);
  101. // 4. Return an implementation-approximated Number value representing the result of the inverse cosine of ℝ(n).
  102. return Value(::acos(number.as_double()));
  103. }
  104. // 21.3.2.3 Math.acosh ( x ), https://tc39.es/ecma262/#sec-math.acosh
  105. JS_DEFINE_NATIVE_FUNCTION(MathObject::acosh)
  106. {
  107. // 1. Let n be ? ToNumber(x).
  108. auto number = TRY(vm.argument(0).to_number(vm));
  109. // 2. If n is NaN or n is +∞𝔽, return n.
  110. if (number.is_nan() || number.is_positive_infinity())
  111. return number;
  112. // 3. If n is 1𝔽, return +0𝔽.
  113. if (number.as_double() == 1.0)
  114. return Value(0.0);
  115. // 4. If n < 1𝔽, return NaN.
  116. if (number.as_double() < 1)
  117. return js_nan();
  118. // 5. Return an implementation-approximated Number value representing the result of the inverse hyperbolic cosine of ℝ(n).
  119. return Value(::acosh(number.as_double()));
  120. }
  121. // 21.3.2.4 Math.asin ( x ), https://tc39.es/ecma262/#sec-math.asin
  122. JS_DEFINE_NATIVE_FUNCTION(MathObject::asin)
  123. {
  124. // 1. Let n be ? ToNumber(x).
  125. auto number = TRY(vm.argument(0).to_number(vm));
  126. // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
  127. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  128. return number;
  129. // 3. If n > 1𝔽 or n < -1𝔽, return NaN.
  130. if (number.as_double() > 1 || number.as_double() < -1)
  131. return js_nan();
  132. // 4. Return an implementation-approximated Number value representing the result of the inverse sine of ℝ(n).
  133. return Value(::asin(number.as_double()));
  134. }
  135. // 21.3.2.5 Math.asinh ( x ), https://tc39.es/ecma262/#sec-math.asinh
  136. JS_DEFINE_NATIVE_FUNCTION(MathObject::asinh)
  137. {
  138. // 1. Let n be ? ToNumber(x).
  139. auto number = TRY(vm.argument(0).to_number(vm));
  140. // 2. If n is not finite or n is either +0𝔽 or -0𝔽, return n.
  141. if (!number.is_finite_number() || number.is_positive_zero() || number.is_negative_zero())
  142. return number;
  143. // 3. Return an implementation-approximated Number value representing the result of the inverse hyperbolic sine of ℝ(n).
  144. return Value(::asinh(number.as_double()));
  145. }
  146. // 21.3.2.6 Math.atan ( x ), https://tc39.es/ecma262/#sec-math.atan
  147. JS_DEFINE_NATIVE_FUNCTION(MathObject::atan)
  148. {
  149. // Let n be ? ToNumber(x).
  150. auto number = TRY(vm.argument(0).to_number(vm));
  151. // 2. If n is one of NaN, +0𝔽, or -0𝔽, return n.
  152. if (number.is_nan() || number.as_double() == 0)
  153. return number;
  154. // 3. If n is +∞𝔽, return an implementation-approximated Number value representing π / 2.
  155. if (number.is_positive_infinity())
  156. return Value(M_PI_2);
  157. // 4. If n is -∞𝔽, return an implementation-approximated Number value representing -π / 2.
  158. if (number.is_negative_infinity())
  159. return Value(-M_PI_2);
  160. // 5. Return an implementation-approximated Number value representing the result of the inverse tangent of ℝ(n).
  161. return Value(::atan(number.as_double()));
  162. }
  163. // 21.3.2.7 Math.atanh ( x ), https://tc39.es/ecma262/#sec-math.atanh
  164. JS_DEFINE_NATIVE_FUNCTION(MathObject::atanh)
  165. {
  166. // 1. Let n be ? ToNumber(x).
  167. auto number = TRY(vm.argument(0).to_number(vm));
  168. // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
  169. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  170. return number;
  171. // 3. If n > 1𝔽 or n < -1𝔽, return NaN.
  172. if (number.as_double() > 1. || number.as_double() < -1.)
  173. return js_nan();
  174. // 4. If n is 1𝔽, return +∞𝔽.
  175. if (number.as_double() == 1.)
  176. return js_infinity();
  177. // 5. If n is -1𝔽, return -∞𝔽.
  178. if (number.as_double() == -1.)
  179. return js_negative_infinity();
  180. // 6. Return an implementation-approximated Number value representing the result of the inverse hyperbolic tangent of ℝ(n).
  181. return Value(::atanh(number.as_double()));
  182. }
  183. // 21.3.2.8 Math.atan2 ( y, x ), https://tc39.es/ecma262/#sec-math.atan2
  184. JS_DEFINE_NATIVE_FUNCTION(MathObject::atan2)
  185. {
  186. auto constexpr three_quarters_pi = M_PI_4 + M_PI_2;
  187. auto y = TRY(vm.argument(0).to_number(vm));
  188. auto x = TRY(vm.argument(1).to_number(vm));
  189. if (y.is_nan() || x.is_nan())
  190. return js_nan();
  191. if (y.is_positive_infinity()) {
  192. if (x.is_positive_infinity())
  193. return Value(M_PI_4);
  194. else if (x.is_negative_infinity())
  195. return Value(three_quarters_pi);
  196. else
  197. return Value(M_PI_2);
  198. }
  199. if (y.is_negative_infinity()) {
  200. if (x.is_positive_infinity())
  201. return Value(-M_PI_4);
  202. else if (x.is_negative_infinity())
  203. return Value(-three_quarters_pi);
  204. else
  205. return Value(-M_PI_2);
  206. }
  207. if (y.is_positive_zero()) {
  208. if (x.as_double() > 0 || x.is_positive_zero())
  209. return Value(0.0);
  210. else
  211. return Value(M_PI);
  212. }
  213. if (y.is_negative_zero()) {
  214. if (x.as_double() > 0 || x.is_positive_zero())
  215. return Value(-0.0);
  216. else
  217. return Value(-M_PI);
  218. }
  219. VERIFY(y.is_finite_number() && !y.is_positive_zero() && !y.is_negative_zero());
  220. if (y.as_double() > 0) {
  221. if (x.is_positive_infinity())
  222. return Value(0);
  223. else if (x.is_negative_infinity())
  224. return Value(M_PI);
  225. else if (x.is_positive_zero() || x.is_negative_zero())
  226. return Value(M_PI_2);
  227. }
  228. if (y.as_double() < 0) {
  229. if (x.is_positive_infinity())
  230. return Value(-0.0);
  231. else if (x.is_negative_infinity())
  232. return Value(-M_PI);
  233. else if (x.is_positive_zero() || x.is_negative_zero())
  234. return Value(-M_PI_2);
  235. }
  236. VERIFY(x.is_finite_number() && !x.is_positive_zero() && !x.is_negative_zero());
  237. return Value(::atan2(y.as_double(), x.as_double()));
  238. }
  239. // 21.3.2.9 Math.cbrt ( x ), https://tc39.es/ecma262/#sec-math.cbrt
  240. JS_DEFINE_NATIVE_FUNCTION(MathObject::cbrt)
  241. {
  242. // 1. Let n be ? ToNumber(x).
  243. auto number = TRY(vm.argument(0).to_number(vm));
  244. // 2. If n is not finite or n is either +0𝔽 or -0𝔽, return n.
  245. if (!number.is_finite_number() || number.as_double() == 0)
  246. return number;
  247. // 3. Return an implementation-approximated Number value representing the result of the cube root of ℝ(n).
  248. return Value(::cbrt(number.as_double()));
  249. }
  250. // 21.3.2.10 Math.ceil ( x ), https://tc39.es/ecma262/#sec-math.ceil
  251. JS_DEFINE_NATIVE_FUNCTION(MathObject::ceil)
  252. {
  253. // 1. Let n be ? ToNumber(x).
  254. auto number = TRY(vm.argument(0).to_number(vm));
  255. // 2. If n is not finite or n is either +0𝔽 or -0𝔽, return n.
  256. if (!number.is_finite_number() || number.as_double() == 0)
  257. return number;
  258. // 3. If n < -0𝔽 and n > -1𝔽, return -0𝔽.
  259. if (number.as_double() < 0 && number.as_double() > -1)
  260. return Value(-0.f);
  261. // 4. If n is an integral Number, return n.
  262. // 5. Return the smallest (closest to -∞) integral Number value that is not less than n.
  263. return Value(::ceil(number.as_double()));
  264. }
  265. // 21.3.2.11 Math.clz32 ( x ), https://tc39.es/ecma262/#sec-math.clz32
  266. JS_DEFINE_NATIVE_FUNCTION(MathObject::clz32)
  267. {
  268. // 1. Let n be ? ToUint32(x).
  269. auto number = TRY(vm.argument(0).to_u32(vm));
  270. // 2. Let p be the number of leading zero bits in the unsigned 32-bit binary representation of n.
  271. // 3. Return 𝔽(p).
  272. return Value(count_leading_zeroes_safe(number));
  273. }
  274. // 21.3.2.12 Math.cos ( x ), https://tc39.es/ecma262/#sec-math.cos
  275. JS_DEFINE_NATIVE_FUNCTION(MathObject::cos)
  276. {
  277. // 1. Let n be ? ToNumber(x).
  278. auto number = TRY(vm.argument(0).to_number(vm));
  279. // 2. If n is NaN, n is +∞𝔽, or n is -∞𝔽, return NaN.
  280. if (number.is_nan() || number.is_infinity())
  281. return js_nan();
  282. // 3. If n is +0𝔽 or n is -0𝔽, return 1𝔽.
  283. if (number.is_positive_zero() || number.is_negative_zero())
  284. return Value(1);
  285. // 4. Return an implementation-approximated Number value representing the result of the cosine of ℝ(n).
  286. return Value(::cos(number.as_double()));
  287. }
  288. // 21.3.2.13 Math.cosh ( x ), https://tc39.es/ecma262/#sec-math.cosh
  289. JS_DEFINE_NATIVE_FUNCTION(MathObject::cosh)
  290. {
  291. // 1. Let n be ? ToNumber(x).
  292. auto number = TRY(vm.argument(0).to_number(vm));
  293. // 2. If n is NaN, return NaN.
  294. if (number.is_nan())
  295. return js_nan();
  296. // 3. If n is +∞𝔽 or n is -∞𝔽, return +∞𝔽.
  297. if (number.is_positive_infinity() || number.is_negative_infinity())
  298. return js_infinity();
  299. // 4. If n is +0𝔽 or n is -0𝔽, return 1𝔽.
  300. if (number.is_positive_zero() || number.is_negative_zero())
  301. return Value(1);
  302. // 5. Return an implementation-approximated Number value representing the result of the hyperbolic cosine of ℝ(n).
  303. return Value(::cosh(number.as_double()));
  304. }
  305. // 21.3.2.14 Math.exp ( x ), https://tc39.es/ecma262/#sec-math.exp
  306. JS_DEFINE_NATIVE_FUNCTION(MathObject::exp)
  307. {
  308. // 1. Let n be ? ToNumber(x).
  309. auto number = TRY(vm.argument(0).to_number(vm));
  310. // 2. If n is either NaN or +∞𝔽, return n.
  311. if (number.is_nan() || number.is_positive_infinity())
  312. return number;
  313. // 3. If n is either +0𝔽 or -0𝔽, return 1𝔽.
  314. if (number.as_double() == 0)
  315. return Value(1);
  316. // 4. If n is -∞𝔽, return +0𝔽.
  317. if (number.is_negative_infinity())
  318. return Value(0);
  319. // 5. Return an implementation-approximated Number value representing the result of the exponential function of ℝ(n).
  320. return Value(::exp(number.as_double()));
  321. }
  322. // 21.3.2.15 Math.expm1 ( x ), https://tc39.es/ecma262/#sec-math.expm1
  323. JS_DEFINE_NATIVE_FUNCTION(MathObject::expm1)
  324. {
  325. // 1. Let n be ? ToNumber(x).
  326. auto number = TRY(vm.argument(0).to_number(vm));
  327. // 2. If n is one of NaN, +0𝔽, -0𝔽, or +∞𝔽, return n.
  328. if (number.is_nan() || number.as_double() == 0 || number.is_positive_infinity())
  329. return number;
  330. // 3. If n is -∞𝔽, return -1𝔽.
  331. if (number.is_negative_infinity())
  332. return Value(-1);
  333. // 4. Return an implementation-approximated Number value representing the result of subtracting 1 from the exponential function of ℝ(n).
  334. return Value(::expm1(number.as_double()));
  335. }
  336. // 21.3.2.16 Math.floor ( x ), https://tc39.es/ecma262/#sec-math.floor
  337. JS_DEFINE_NATIVE_FUNCTION(MathObject::floor)
  338. {
  339. // 1. Let n be ? ToNumber(x).
  340. auto number = TRY(vm.argument(0).to_number(vm));
  341. // 2. If n is not finite or n is either +0𝔽 or -0𝔽, return n.
  342. if (!number.is_finite_number() || number.as_double() == 0)
  343. return number;
  344. // 3. If n < 1𝔽 and n > +0𝔽, return +0𝔽.
  345. // 4. If n is an integral Number, return n.
  346. // 5. Return the greatest (closest to +∞) integral Number value that is not greater than n.
  347. return Value(::floor(number.as_double()));
  348. }
  349. // 21.3.2.17 Math.fround ( x ), https://tc39.es/ecma262/#sec-math.fround
  350. JS_DEFINE_NATIVE_FUNCTION(MathObject::fround)
  351. {
  352. // 1. Let n be ? ToNumber(x).
  353. auto number = TRY(vm.argument(0).to_number(vm));
  354. // 2. If n is NaN, return NaN.
  355. if (number.is_nan())
  356. return js_nan();
  357. // 3. If n is one of +0𝔽, -0𝔽, +∞𝔽, or -∞𝔽, return n.
  358. if (number.as_double() == 0 || number.is_infinity())
  359. return number;
  360. // 4. Let n32 be the result of converting n to a value in IEEE 754-2019 binary32 format using roundTiesToEven mode.
  361. // 5. Let n64 be the result of converting n32 to a value in IEEE 754-2019 binary64 format.
  362. // 6. Return the ECMAScript Number value corresponding to n64.
  363. return Value((float)number.as_double());
  364. }
  365. // 21.3.2.18 Math.hypot ( ...args ), https://tc39.es/ecma262/#sec-math.hypot
  366. JS_DEFINE_NATIVE_FUNCTION(MathObject::hypot)
  367. {
  368. // 1. Let coerced be a new empty List.
  369. Vector<Value> coerced;
  370. // 2. For each element arg of args, do
  371. for (size_t i = 0; i < vm.argument_count(); ++i) {
  372. // a. Let n be ? ToNumber(arg).
  373. auto number = TRY(vm.argument(i).to_number(vm));
  374. // b. Append n to coerced.
  375. coerced.append(number);
  376. }
  377. // 3. For each element number of coerced, do
  378. for (auto& number : coerced) {
  379. // a. If number is either +∞𝔽 or -∞𝔽, return +∞𝔽.
  380. if (number.is_infinity())
  381. return js_infinity();
  382. }
  383. // 4. Let onlyZero be true.
  384. auto only_zero = true;
  385. double sum_of_squares = 0;
  386. // 5. For each element number of coerced, do
  387. for (auto& number : coerced) {
  388. // a. If number is NaN, return NaN.
  389. // OPTIMIZATION: For infinities, the result will be infinity with the same sign, so we can return early.
  390. if (number.is_nan() || number.is_infinity())
  391. return number;
  392. // b. If number is neither +0𝔽 nor -0𝔽, set onlyZero to false.
  393. if (number.as_double() != 0)
  394. only_zero = false;
  395. sum_of_squares += number.as_double() * number.as_double();
  396. }
  397. // 6. If onlyZero is true, return +0𝔽.
  398. if (only_zero)
  399. return Value(0);
  400. // 7. Return an implementation-approximated Number value representing the square root of the sum of squares of the mathematical values of the elements of coerced.
  401. return Value(::sqrt(sum_of_squares));
  402. }
  403. // 21.3.2.19 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul
  404. JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
  405. {
  406. // 1. Let a be ℝ(? ToUint32(x)).
  407. auto a = TRY(vm.argument(0).to_u32(vm));
  408. // 2. Let b be ℝ(? ToUint32(y)).
  409. auto b = TRY(vm.argument(1).to_u32(vm));
  410. // 3. Let product be (a × b) modulo 2^32.
  411. // 4. If product ≥ 2^31, return 𝔽(product - 2^32); otherwise return 𝔽(product).
  412. return Value(static_cast<i32>(a * b));
  413. }
  414. // 21.3.2.20 Math.log ( x ), https://tc39.es/ecma262/#sec-math.log
  415. JS_DEFINE_NATIVE_FUNCTION(MathObject::log)
  416. {
  417. // 1. Let n be ? ToNumber(x).
  418. auto number = TRY(vm.argument(0).to_number(vm));
  419. // 2. If n is NaN or n is +∞𝔽, return n.
  420. if (number.is_nan() || number.is_positive_infinity())
  421. return number;
  422. // 3. If n is 1𝔽, return +0𝔽.
  423. if (number.as_double() == 1.)
  424. return Value(0);
  425. // 4. If n is +0𝔽 or n is -0𝔽, return -∞𝔽.
  426. if (number.is_positive_zero() || number.is_negative_zero())
  427. return js_negative_infinity();
  428. // 5. If n < -0𝔽, return NaN.
  429. if (number.as_double() < -0.)
  430. return js_nan();
  431. // 6. Return an implementation-approximated Number value representing the result of the natural logarithm of ℝ(n).
  432. return Value(::log(number.as_double()));
  433. }
  434. // 21.3.2.21 Math.log1p ( x ), https://tc39.es/ecma262/#sec-math.log1p
  435. JS_DEFINE_NATIVE_FUNCTION(MathObject::log1p)
  436. {
  437. // 1. Let n be ? ToNumber(x).
  438. auto number = TRY(vm.argument(0).to_number(vm));
  439. // 2. If n is NaN, n is +0𝔽, n is -0𝔽, or n is +∞𝔽, return n.
  440. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero() || number.is_positive_infinity())
  441. return number;
  442. // 3. If n is -1𝔽, return -∞𝔽.
  443. if (number.as_double() == -1.)
  444. return js_negative_infinity();
  445. // 4. If n < -1𝔽, return NaN.
  446. if (number.as_double() < -1.)
  447. return js_nan();
  448. // 5. Return an implementation-approximated Number value representing the result of the natural logarithm of 1 + ℝ(n).
  449. return Value(::log1p(number.as_double()));
  450. }
  451. // 21.3.2.22 Math.log10 ( x ), https://tc39.es/ecma262/#sec-math.log10
  452. JS_DEFINE_NATIVE_FUNCTION(MathObject::log10)
  453. {
  454. // 1. Let n be ? ToNumber(x).
  455. auto number = TRY(vm.argument(0).to_number(vm));
  456. // 2. If n is NaN or n is +∞𝔽, return n.
  457. if (number.is_nan() || number.is_positive_infinity())
  458. return number;
  459. // 3. If n is 1𝔽, return +0𝔽.
  460. if (number.as_double() == 1.)
  461. return Value(0);
  462. // 4. If n is +0𝔽 or n is -0𝔽, return -∞𝔽.
  463. if (number.is_positive_zero() || number.is_negative_zero())
  464. return js_negative_infinity();
  465. // 5. If n < -0𝔽, return NaN.
  466. if (number.as_double() < -0.)
  467. return js_nan();
  468. // 6. Return an implementation-approximated Number value representing the result of the base 10 logarithm of ℝ(n).
  469. return Value(::log10(number.as_double()));
  470. }
  471. // 21.3.2.23 Math.log2 ( x ), https://tc39.es/ecma262/#sec-math.log2
  472. JS_DEFINE_NATIVE_FUNCTION(MathObject::log2)
  473. {
  474. // 1. Let n be ? ToNumber(x).
  475. auto number = TRY(vm.argument(0).to_number(vm));
  476. // 2. If n is NaN or n is +∞𝔽, return n.
  477. if (number.is_nan() || number.is_positive_infinity())
  478. return number;
  479. // 3. If n is 1𝔽, return +0𝔽.
  480. if (number.as_double() == 1.)
  481. return Value(0);
  482. // 4. If n is +0𝔽 or n is -0𝔽, return -∞𝔽.
  483. if (number.is_positive_zero() || number.is_negative_zero())
  484. return js_negative_infinity();
  485. // 5. If n < -0𝔽, return NaN.
  486. if (number.as_double() < -0.)
  487. return js_nan();
  488. // 6. Return an implementation-approximated Number value representing the result of the base 2 logarithm of ℝ(n).
  489. return Value(::log2(number.as_double()));
  490. }
  491. // 21.3.2.24 Math.max ( ...args ), https://tc39.es/ecma262/#sec-math.max
  492. JS_DEFINE_NATIVE_FUNCTION(MathObject::max)
  493. {
  494. // 1. Let coerced be a new empty List.
  495. Vector<Value> coerced;
  496. // 2. For each element arg of args, do
  497. for (size_t i = 0; i < vm.argument_count(); ++i) {
  498. // a. Let n be ? ToNumber(arg).
  499. auto number = TRY(vm.argument(i).to_number(vm));
  500. // b. Append n to coerced.
  501. coerced.append(number);
  502. }
  503. // 3. Let highest be -∞𝔽.
  504. auto highest = js_negative_infinity();
  505. // 4. For each element number of coerced, do
  506. for (auto& number : coerced) {
  507. // a. If number is NaN, return NaN.
  508. if (number.is_nan())
  509. return js_nan();
  510. // b. If number is +0𝔽 and highest is -0𝔽, set highest to +0𝔽.
  511. // c. If number > highest, set highest to number.
  512. if ((number.is_positive_zero() && highest.is_negative_zero()) || number.as_double() > highest.as_double())
  513. highest = number;
  514. }
  515. // 5. Return highest.
  516. return highest;
  517. }
  518. // 21.3.2.25 Math.min ( ...args ), https://tc39.es/ecma262/#sec-math.min
  519. JS_DEFINE_NATIVE_FUNCTION(MathObject::min)
  520. {
  521. // 1. Let coerced be a new empty List.
  522. Vector<Value> coerced;
  523. // 2. For each element arg of args, do
  524. for (size_t i = 0; i < vm.argument_count(); ++i) {
  525. // a. Let n be ? ToNumber(arg).
  526. auto number = TRY(vm.argument(i).to_number(vm));
  527. // b. Append n to coerced.
  528. coerced.append(number);
  529. }
  530. // 3. Let lowest be +∞𝔽.
  531. auto lowest = js_infinity();
  532. // 4. For each element number of coerced, do
  533. for (auto& number : coerced) {
  534. // a. If number is NaN, return NaN.
  535. if (number.is_nan())
  536. return js_nan();
  537. // b. If number is -0𝔽 and lowest is +0𝔽, set lowest to -0𝔽.
  538. // c. If number < lowest, set lowest to number.
  539. if ((number.is_negative_zero() && lowest.is_positive_zero()) || number.as_double() < lowest.as_double())
  540. lowest = number;
  541. }
  542. // 5. Return lowest.
  543. return lowest;
  544. }
  545. // 21.3.2.26 Math.pow ( base, exponent ), https://tc39.es/ecma262/#sec-math.pow
  546. JS_DEFINE_NATIVE_FUNCTION(MathObject::pow)
  547. {
  548. // Set base to ? ToNumber(base).
  549. auto base = TRY(vm.argument(0).to_number(vm));
  550. // 2. Set exponent to ? ToNumber(exponent).
  551. auto exponent = TRY(vm.argument(1).to_number(vm));
  552. // 3. Return Number::exponentiate(base, exponent).
  553. return JS::exp(vm, base, exponent);
  554. }
  555. // 21.3.2.27 Math.random ( ), https://tc39.es/ecma262/#sec-math.random
  556. JS_DEFINE_NATIVE_FUNCTION(MathObject::random)
  557. {
  558. // This function returns a Number value with positive sign, greater than or equal to +0𝔽 but strictly less than 1𝔽,
  559. // chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an
  560. // implementation-defined algorithm or strategy.
  561. double r = (double)get_random<u32>() / (double)UINT32_MAX;
  562. return Value(r);
  563. }
  564. // 21.3.2.28 Math.round ( x ), https://tc39.es/ecma262/#sec-math.round
  565. JS_DEFINE_NATIVE_FUNCTION(MathObject::round)
  566. {
  567. // 1. Let n be ? ToNumber(x).
  568. auto number = TRY(vm.argument(0).to_number(vm));
  569. // 2. If n is not finite or n is an integral Number, return n.
  570. if (!number.is_finite_number() || number.as_double() == ::trunc(number.as_double()))
  571. return number;
  572. // 3. If n < 0.5𝔽 and n > +0𝔽, return +0𝔽.
  573. // 4. If n < -0𝔽 and n ≥ -0.5𝔽, return -0𝔽.
  574. // 5. Return the integral Number closest to n, preferring the Number closer to +∞ in the case of a tie.
  575. double integer = ::ceil(number.as_double());
  576. if (integer - 0.5 > number.as_double())
  577. integer--;
  578. return Value(integer);
  579. }
  580. // 21.3.2.29 Math.sign ( x ), https://tc39.es/ecma262/#sec-math.sign
  581. JS_DEFINE_NATIVE_FUNCTION(MathObject::sign)
  582. {
  583. // 1. Let n be ? ToNumber(x).
  584. auto number = TRY(vm.argument(0).to_number(vm));
  585. // 2. If n is one of NaN, +0𝔽, or -0𝔽, return n.
  586. if (number.is_nan() || number.as_double() == 0)
  587. return number;
  588. // 3. If n < -0𝔽, return -1𝔽.
  589. if (number.as_double() < 0)
  590. return Value(-1);
  591. // 4. Return 1𝔽.
  592. return Value(1);
  593. }
  594. // 21.3.2.30 Math.sin ( x ), https://tc39.es/ecma262/#sec-math.sin
  595. JS_DEFINE_NATIVE_FUNCTION(MathObject::sin)
  596. {
  597. // 1. Let n be ? ToNumber(x).
  598. auto number = TRY(vm.argument(0).to_number(vm));
  599. // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
  600. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  601. return number;
  602. // 3. If n is +∞𝔽 or n is -∞𝔽, return NaN.
  603. if (number.is_infinity())
  604. return js_nan();
  605. // 4. Return an implementation-approximated Number value representing the result of the sine of ℝ(n).
  606. return Value(::sin(number.as_double()));
  607. }
  608. // 21.3.2.31 Math.sinh ( x ), https://tc39.es/ecma262/#sec-math.sinh
  609. JS_DEFINE_NATIVE_FUNCTION(MathObject::sinh)
  610. {
  611. // 1. Let n be ? ToNumber(x).
  612. auto number = TRY(vm.argument(0).to_number(vm));
  613. // 2. If n is not finite or n is either +0𝔽 or -0𝔽, return n.
  614. if (!number.is_finite_number() || number.is_positive_zero() || number.is_negative_zero())
  615. return number;
  616. // 3. Return an implementation-approximated Number value representing the result of the hyperbolic sine of ℝ(n).
  617. return Value(::sinh(number.as_double()));
  618. }
  619. // 21.3.2.32 Math.sqrt ( x ), https://tc39.es/ecma262/#sec-math.sqrt
  620. JS_DEFINE_NATIVE_FUNCTION(MathObject::sqrt)
  621. {
  622. // Let n be ? ToNumber(x).
  623. auto number = TRY(vm.argument(0).to_number(vm));
  624. // 2. If n is one of NaN, +0𝔽, -0𝔽, or +∞𝔽, return n.
  625. if (number.is_nan() || number.as_double() == 0 || number.is_positive_infinity())
  626. return number;
  627. // 3. If n < -0𝔽, return NaN.
  628. if (number.as_double() < 0)
  629. return js_nan();
  630. // 4. Return an implementation-approximated Number value representing the result of the square root of ℝ(n).
  631. return Value(::sqrt(number.as_double()));
  632. }
  633. // 21.3.2.33 Math.tan ( x ), https://tc39.es/ecma262/#sec-math.tan
  634. JS_DEFINE_NATIVE_FUNCTION(MathObject::tan)
  635. {
  636. // Let n be ? ToNumber(x).
  637. auto number = TRY(vm.argument(0).to_number(vm));
  638. // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
  639. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  640. return number;
  641. // 3. If n is +∞𝔽, or n is -∞𝔽, return NaN.
  642. if (number.is_infinity())
  643. return js_nan();
  644. // 4. Return an implementation-approximated Number value representing the result of the tangent of ℝ(n).
  645. return Value(::tan(number.as_double()));
  646. }
  647. // 21.3.2.34 Math.tanh ( x ), https://tc39.es/ecma262/#sec-math.tanh
  648. JS_DEFINE_NATIVE_FUNCTION(MathObject::tanh)
  649. {
  650. // 1. Let n be ? ToNumber(x).
  651. auto number = TRY(vm.argument(0).to_number(vm));
  652. // 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
  653. if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
  654. return number;
  655. // 3. If n is +∞𝔽, return 1𝔽.
  656. if (number.is_positive_infinity())
  657. return Value(1);
  658. // 4. If n is -∞𝔽, return -1𝔽.
  659. if (number.is_negative_infinity())
  660. return Value(-1);
  661. // 5. Return an implementation-approximated Number value representing the result of the hyperbolic tangent of ℝ(n).
  662. return Value(::tanh(number.as_double()));
  663. }
  664. // 21.3.2.35 Math.trunc ( x ), https://tc39.es/ecma262/#sec-math.trunc
  665. JS_DEFINE_NATIVE_FUNCTION(MathObject::trunc)
  666. {
  667. // 1. Let n be ? ToNumber(x).
  668. auto number = TRY(vm.argument(0).to_number(vm));
  669. // 2. If n is not finite or n is either +0𝔽 or -0𝔽, return n.
  670. if (number.is_nan() || number.is_infinity() || number.as_double() == 0)
  671. return number;
  672. // 3. If n < 1𝔽 and n > +0𝔽, return +0𝔽.
  673. // 4. If n < -0𝔽 and n > -1𝔽, return -0𝔽.
  674. // 5. Return the integral Number nearest n in the direction of +0𝔽.
  675. return Value(number.as_double() < 0
  676. ? ::ceil(number.as_double())
  677. : ::floor(number.as_double()));
  678. }
  679. }