mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:24:57 +00:00
LibJS: Unify exponentiation logic for ** operator and Math.pow
The JS behaviour of exponentiation on two number typed values is not a simple matter of forwarding to ::pow(double, double). So, this factors out the Math.pow logic to allow it to be shared with Value::exp.
This commit is contained in:
parent
c0735b047e
commit
c45922c637
3 changed files with 70 additions and 48 deletions
|
@ -216,53 +216,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::pow)
|
|||
{
|
||||
auto base = TRY(vm.argument(0).to_number(global_object));
|
||||
auto exponent = TRY(vm.argument(1).to_number(global_object));
|
||||
if (exponent.is_nan())
|
||||
return js_nan();
|
||||
if (exponent.is_positive_zero() || exponent.is_negative_zero())
|
||||
return Value(1);
|
||||
if (base.is_nan())
|
||||
return js_nan();
|
||||
if (base.is_positive_infinity())
|
||||
return exponent.as_double() > 0 ? js_infinity() : Value(0);
|
||||
if (base.is_negative_infinity()) {
|
||||
auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
|
||||
if (exponent.as_double() > 0)
|
||||
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
|
||||
else
|
||||
return is_odd_integral_number ? Value(-0.0) : Value(0);
|
||||
}
|
||||
if (base.is_positive_zero())
|
||||
return exponent.as_double() > 0 ? Value(0) : js_infinity();
|
||||
if (base.is_negative_zero()) {
|
||||
auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
|
||||
if (exponent.as_double() > 0)
|
||||
return is_odd_integral_number ? Value(-0.0) : Value(0);
|
||||
else
|
||||
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
|
||||
}
|
||||
VERIFY(base.is_finite_number() && !base.is_positive_zero() && !base.is_negative_zero());
|
||||
if (exponent.is_positive_infinity()) {
|
||||
auto absolute_base = fabs(base.as_double());
|
||||
if (absolute_base > 1)
|
||||
return js_infinity();
|
||||
else if (absolute_base == 1)
|
||||
return js_nan();
|
||||
else if (absolute_base < 1)
|
||||
return Value(0);
|
||||
}
|
||||
if (exponent.is_negative_infinity()) {
|
||||
auto absolute_base = fabs(base.as_double());
|
||||
if (absolute_base > 1)
|
||||
return Value(0);
|
||||
else if (absolute_base == 1)
|
||||
return js_nan();
|
||||
else if (absolute_base < 1)
|
||||
return js_infinity();
|
||||
}
|
||||
VERIFY(exponent.is_finite_number() && !exponent.is_positive_zero() && !exponent.is_negative_zero());
|
||||
if (base.as_double() < 0 && !exponent.is_integral_number())
|
||||
return js_nan();
|
||||
return Value(::pow(base.as_double(), exponent.as_double()));
|
||||
return JS::exp(global_object, base, exponent);
|
||||
}
|
||||
|
||||
// 21.3.2.14 Math.exp ( x ), https://tc39.es/ecma262/#sec-math.exp
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/AllOf.h>
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
|
@ -1192,6 +1193,58 @@ ThrowCompletionOr<Value> mod(GlobalObject& global_object, Value lhs, Value rhs)
|
|||
return vm.throw_completion<TypeError>(global_object, ErrorType::BigIntBadOperatorOtherType, "modulo");
|
||||
}
|
||||
|
||||
static Value exp_double(Value base, Value exponent)
|
||||
{
|
||||
VERIFY(both_number(base, exponent));
|
||||
if (exponent.is_nan())
|
||||
return js_nan();
|
||||
if (exponent.is_positive_zero() || exponent.is_negative_zero())
|
||||
return Value(1);
|
||||
if (base.is_nan())
|
||||
return js_nan();
|
||||
if (base.is_positive_infinity())
|
||||
return exponent.as_double() > 0 ? js_infinity() : Value(0);
|
||||
if (base.is_negative_infinity()) {
|
||||
auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
|
||||
if (exponent.as_double() > 0)
|
||||
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
|
||||
else
|
||||
return is_odd_integral_number ? Value(-0.0) : Value(0);
|
||||
}
|
||||
if (base.is_positive_zero())
|
||||
return exponent.as_double() > 0 ? Value(0) : js_infinity();
|
||||
if (base.is_negative_zero()) {
|
||||
auto is_odd_integral_number = exponent.is_integral_number() && (exponent.as_i32() % 2 != 0);
|
||||
if (exponent.as_double() > 0)
|
||||
return is_odd_integral_number ? Value(-0.0) : Value(0);
|
||||
else
|
||||
return is_odd_integral_number ? js_negative_infinity() : js_infinity();
|
||||
}
|
||||
VERIFY(base.is_finite_number() && !base.is_positive_zero() && !base.is_negative_zero());
|
||||
if (exponent.is_positive_infinity()) {
|
||||
auto absolute_base = fabs(base.as_double());
|
||||
if (absolute_base > 1)
|
||||
return js_infinity();
|
||||
else if (absolute_base == 1)
|
||||
return js_nan();
|
||||
else if (absolute_base < 1)
|
||||
return Value(0);
|
||||
}
|
||||
if (exponent.is_negative_infinity()) {
|
||||
auto absolute_base = fabs(base.as_double());
|
||||
if (absolute_base > 1)
|
||||
return Value(0);
|
||||
else if (absolute_base == 1)
|
||||
return js_nan();
|
||||
else if (absolute_base < 1)
|
||||
return js_infinity();
|
||||
}
|
||||
VERIFY(exponent.is_finite_number() && !exponent.is_positive_zero() && !exponent.is_negative_zero());
|
||||
if (base.as_double() < 0 && !exponent.is_integral_number())
|
||||
return js_nan();
|
||||
return Value(::pow(base.as_double(), exponent.as_double()));
|
||||
}
|
||||
|
||||
// 13.6 Exponentiation Operator, https://tc39.es/ecma262/#sec-exp-operator
|
||||
ThrowCompletionOr<Value> exp(GlobalObject& global_object, Value lhs, Value rhs)
|
||||
{
|
||||
|
@ -1199,7 +1252,7 @@ ThrowCompletionOr<Value> exp(GlobalObject& global_object, Value lhs, Value rhs)
|
|||
auto lhs_numeric = TRY(lhs.to_numeric(global_object));
|
||||
auto rhs_numeric = TRY(rhs.to_numeric(global_object));
|
||||
if (both_number(lhs_numeric, rhs_numeric))
|
||||
return Value(pow(lhs_numeric.as_double(), rhs_numeric.as_double()));
|
||||
return exp_double(lhs_numeric, rhs_numeric);
|
||||
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
||||
if (rhs_numeric.as_bigint().big_integer().is_negative())
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::NegativeExponent);
|
||||
|
|
|
@ -35,3 +35,18 @@ test("exponentiation that produces NaN", () => {
|
|||
expect(2 ** "foo").toBeNaN();
|
||||
expect("foo" ** 2).toBeNaN();
|
||||
});
|
||||
|
||||
test("exponentiation with infinities", () => {
|
||||
expect((-1) ** Infinity).toBeNaN();
|
||||
expect(0 ** Infinity).toBe(0);
|
||||
expect(1 ** Infinity).toBeNaN();
|
||||
expect((-1) ** -Infinity).toBeNaN();
|
||||
expect(0 ** -Infinity).toBe(Infinity);
|
||||
expect(1 ** -Infinity).toBeNaN();
|
||||
expect(Infinity ** -1).toBe(0);
|
||||
expect(Infinity ** 0).toBe(1);
|
||||
expect(Infinity ** 1).toBe(Infinity);
|
||||
expect((-Infinity) ** -1).toBe(-0);
|
||||
expect((-Infinity) ** 0).toBe(1);
|
||||
expect((-Infinity) ** 1).toBe(-Infinity);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue