MathObject.cpp 32 KB

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