MathObject.cpp 32 KB

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