MathObject.cpp 33 KB

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