MathObject.cpp 32 KB

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