mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-02 17:28:48 +00:00
LibJS: Add Math.sign()
This commit is contained in:
parent
bdfd1f1545
commit
b27834cf16
Notes:
sideshowbarker
2024-07-19 06:29:33 +09:00
Author: https://github.com/linusg
Commit: b27834cf16
Pull-request: https://github.com/SerenityOS/serenity/pull/2285
3 changed files with 60 additions and 0 deletions
|
@ -50,6 +50,7 @@ MathObject::MathObject()
|
||||||
put_native_function("cos", cos, 1, attr);
|
put_native_function("cos", cos, 1, attr);
|
||||||
put_native_function("tan", tan, 1, attr);
|
put_native_function("tan", tan, 1, attr);
|
||||||
put_native_function("pow", pow, 2, attr);
|
put_native_function("pow", pow, 2, attr);
|
||||||
|
put_native_function("sign", sign, 1, attr);
|
||||||
|
|
||||||
put("E", Value(M_E), 0);
|
put("E", Value(M_E), 0);
|
||||||
put("LN2", Value(M_LN2), 0);
|
put("LN2", Value(M_LN2), 0);
|
||||||
|
@ -206,4 +207,20 @@ Value MathObject::pow(Interpreter& interpreter)
|
||||||
return exp(interpreter, interpreter.argument(0), interpreter.argument(1));
|
return exp(interpreter, interpreter.argument(0), interpreter.argument(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value MathObject::sign(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
auto number = interpreter.argument(0).to_number(interpreter);
|
||||||
|
if (interpreter.exception())
|
||||||
|
return {};
|
||||||
|
if (number.is_positive_zero())
|
||||||
|
return Value(0);
|
||||||
|
if (number.is_negative_zero())
|
||||||
|
return Value(-0.0);
|
||||||
|
if (number.as_double() > 0)
|
||||||
|
return Value(1);
|
||||||
|
if (number.as_double() < 0)
|
||||||
|
return Value(-1);
|
||||||
|
return js_nan();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,6 +51,7 @@ private:
|
||||||
static Value cos(Interpreter&);
|
static Value cos(Interpreter&);
|
||||||
static Value tan(Interpreter&);
|
static Value tan(Interpreter&);
|
||||||
static Value pow(Interpreter&);
|
static Value pow(Interpreter&);
|
||||||
|
static Value sign(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
42
Libraries/LibJS/Tests/Math.sign.js
Normal file
42
Libraries/LibJS/Tests/Math.sign.js
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
load("test-common.js");
|
||||||
|
|
||||||
|
function isPositiveZero(value) {
|
||||||
|
return value === 0 && 1 / value === Infinity;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isNegativeZero(value) {
|
||||||
|
return value === 0 && 1 / value === -Infinity;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert(Math.sign.length === 1);
|
||||||
|
|
||||||
|
assert(Math.sign(0.0001) === 1);
|
||||||
|
assert(Math.sign(1) === 1);
|
||||||
|
assert(Math.sign(42) === 1);
|
||||||
|
assert(Math.sign(Infinity) === 1);
|
||||||
|
assert(isPositiveZero(Math.sign(0)));
|
||||||
|
assert(isPositiveZero(Math.sign(null)));
|
||||||
|
assert(isPositiveZero(Math.sign('')));
|
||||||
|
assert(isPositiveZero(Math.sign([])));
|
||||||
|
|
||||||
|
assert(Math.sign(-0.0001) === -1);
|
||||||
|
assert(Math.sign(-1) === -1);
|
||||||
|
assert(Math.sign(-42) === -1);
|
||||||
|
assert(Math.sign(-Infinity) === -1);
|
||||||
|
assert(isNegativeZero(Math.sign(-0)));
|
||||||
|
assert(isNegativeZero(Math.sign(-null)));
|
||||||
|
assert(isNegativeZero(Math.sign(-'')));
|
||||||
|
assert(isNegativeZero(Math.sign(-[])));
|
||||||
|
|
||||||
|
assert(isNaN(Math.sign()));
|
||||||
|
assert(isNaN(Math.sign(undefined)));
|
||||||
|
assert(isNaN(Math.sign([1, 2, 3])));
|
||||||
|
assert(isNaN(Math.sign({})));
|
||||||
|
assert(isNaN(Math.sign(NaN)));
|
||||||
|
assert(isNaN(Math.sign("foo")));
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue