mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 20:27:35 +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("sqrt", sqrt, 1);
|
||||||
put_native_function("floor", floor, 1);
|
put_native_function("floor", floor, 1);
|
||||||
put_native_function("round", round, 1);
|
put_native_function("round", round, 1);
|
||||||
|
put_native_function("max", max, 2);
|
||||||
|
|
||||||
put("E", Value(M_E));
|
put("E", Value(M_E));
|
||||||
put("LN2", Value(M_LN2));
|
put("LN2", Value(M_LN2));
|
||||||
|
@ -109,4 +110,26 @@ Value MathObject::round(Interpreter& interpreter)
|
||||||
return Value(::roundf(number.as_double()));
|
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 {};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ private:
|
||||||
static Value sqrt(Interpreter&);
|
static Value sqrt(Interpreter&);
|
||||||
static Value floor(Interpreter&);
|
static Value floor(Interpreter&);
|
||||||
static Value round(Interpreter&);
|
static Value round(Interpreter&);
|
||||||
|
static Value max(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue