mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:47:43 +00:00
LibJS: Add Math.pow()
This commit is contained in:
parent
687096cadd
commit
97366f4dd4
3 changed files with 36 additions and 0 deletions
|
@ -48,6 +48,7 @@ MathObject::MathObject()
|
||||||
put_native_function("sin", sin, 1);
|
put_native_function("sin", sin, 1);
|
||||||
put_native_function("cos", cos, 1);
|
put_native_function("cos", cos, 1);
|
||||||
put_native_function("tan", tan, 1);
|
put_native_function("tan", tan, 1);
|
||||||
|
put_native_function("pow", pow, 2);
|
||||||
|
|
||||||
put("E", Value(M_E));
|
put("E", Value(M_E));
|
||||||
put("LN2", Value(M_LN2));
|
put("LN2", Value(M_LN2));
|
||||||
|
@ -180,4 +181,9 @@ Value MathObject::tan(Interpreter& interpreter)
|
||||||
return Value(::tan(number.as_double()));
|
return Value(::tan(number.as_double()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value MathObject::pow(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
return exp(interpreter, interpreter.argument(0), interpreter.argument(1));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,7 @@ private:
|
||||||
static Value sin(Interpreter&);
|
static Value sin(Interpreter&);
|
||||||
static Value cos(Interpreter&);
|
static Value cos(Interpreter&);
|
||||||
static Value tan(Interpreter&);
|
static Value tan(Interpreter&);
|
||||||
|
static Value pow(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
29
Libraries/LibJS/Tests/Math.pow.js
Normal file
29
Libraries/LibJS/Tests/Math.pow.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
load("test-common.js");
|
||||||
|
|
||||||
|
try {
|
||||||
|
assert(Math.pow(2, 0) === 1);
|
||||||
|
assert(Math.pow(2, 1) === 2);
|
||||||
|
assert(Math.pow(2, 2) === 4);
|
||||||
|
assert(Math.pow(2, 3) === 8);
|
||||||
|
assert(Math.pow(2, -3) === 0.125);
|
||||||
|
assert(Math.pow(3, 2) === 9);
|
||||||
|
assert(Math.pow(0, 0) === 1);
|
||||||
|
assert(Math.pow(2, Math.pow(3, 2)) === 512);
|
||||||
|
assert(Math.pow(Math.pow(2, 3), 2) === 64);
|
||||||
|
assert(Math.pow("2", "3") === 8);
|
||||||
|
assert(Math.pow("", []) === 1);
|
||||||
|
assert(Math.pow([], null) === 1);
|
||||||
|
assert(Math.pow(null, null) === 1);
|
||||||
|
assert(Math.pow(undefined, null) === 1);
|
||||||
|
assert(isNaN(Math.pow(NaN, 2)));
|
||||||
|
assert(isNaN(Math.pow(2, NaN)));
|
||||||
|
assert(isNaN(Math.pow(undefined, 2)));
|
||||||
|
assert(isNaN(Math.pow(2, undefined)));
|
||||||
|
assert(isNaN(Math.pow(null, undefined)));
|
||||||
|
assert(isNaN(Math.pow(2, "foo")));
|
||||||
|
assert(isNaN(Math.pow("foo", 2)));
|
||||||
|
|
||||||
|
console.log("PASS");
|
||||||
|
} catch (e) {
|
||||||
|
console.log("FAIL: " + e);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue