MathObject.cpp 33 KB

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