MathObject.cpp 32 KB

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