mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
LibJS: Add Math.max()
This commit is contained in:
parent
ca90f88d4e
commit
d84de532f1
2 changed files with 24 additions and 0 deletions
|
@ -39,6 +39,7 @@ MathObject::MathObject()
|
|||
put_native_function("sqrt", sqrt, 1);
|
||||
put_native_function("floor", floor, 1);
|
||||
put_native_function("round", round, 1);
|
||||
put_native_function("max", max, 2);
|
||||
|
||||
put("E", Value(M_E));
|
||||
put("LN2", Value(M_LN2));
|
||||
|
@ -109,4 +110,26 @@ Value MathObject::round(Interpreter& interpreter)
|
|||
return Value(::roundf(number.as_double()));
|
||||
}
|
||||
|
||||
Value MathObject::max(Interpreter& interpreter)
|
||||
{
|
||||
if (!interpreter.argument_count()) {
|
||||
// FIXME: I think this should return *negative* infinity.
|
||||
return js_infinity();
|
||||
}
|
||||
|
||||
if (interpreter.argument_count() == 1)
|
||||
return interpreter.argument(0).to_number();
|
||||
|
||||
if (interpreter.argument_count() == 2) {
|
||||
auto a = interpreter.argument(0).to_number();
|
||||
auto b = interpreter.argument(1).to_number();
|
||||
return Value(a.as_double() > b.as_double() ? a : b);
|
||||
}
|
||||
|
||||
// FIXME: Support Math.max() with more arguments.
|
||||
ASSERT_NOT_REACHED();
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue