mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:38:11 +00:00
LibJS: Add support for Math.ceil() and Math.trunc()
Introduce support for the both of these Math methods. Math.trunc is implemented in terms of Math.ceil or Math.floor based on the input value. Added tests as well.
This commit is contained in:
parent
4ea71c9571
commit
240a5b5fd7
4 changed files with 66 additions and 0 deletions
|
@ -38,8 +38,10 @@ MathObject::MathObject()
|
|||
put_native_function("random", random);
|
||||
put_native_function("sqrt", sqrt, 1);
|
||||
put_native_function("floor", floor, 1);
|
||||
put_native_function("ceil", ceil, 1);
|
||||
put_native_function("round", round, 1);
|
||||
put_native_function("max", max, 2);
|
||||
put_native_function("trunc", trunc, 1);
|
||||
|
||||
put("E", Value(M_E));
|
||||
put("LN2", Value(M_LN2));
|
||||
|
@ -98,6 +100,17 @@ Value MathObject::floor(Interpreter& interpreter)
|
|||
return Value(::floor(number.as_double()));
|
||||
}
|
||||
|
||||
Value MathObject::ceil(Interpreter& interpreter)
|
||||
{
|
||||
if (!interpreter.argument_count())
|
||||
return js_nan();
|
||||
|
||||
auto number = interpreter.argument(0).to_number();
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::ceil(number.as_double()));
|
||||
}
|
||||
|
||||
Value MathObject::round(Interpreter& interpreter)
|
||||
{
|
||||
if (!interpreter.argument_count())
|
||||
|
@ -127,4 +140,18 @@ Value MathObject::max(Interpreter& interpreter)
|
|||
}
|
||||
}
|
||||
|
||||
Value MathObject::trunc(Interpreter& interpreter)
|
||||
{
|
||||
if (!interpreter.argument_count())
|
||||
return js_nan();
|
||||
|
||||
auto number = interpreter.argument(0).to_number();
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
|
||||
if (number.as_double() < 0)
|
||||
return MathObject::ceil(interpreter);
|
||||
return MathObject::floor(interpreter);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue