MathObject.cpp 33 KB

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