LibJS: Add Math.round()

This commit is contained in:
Andreas Kling 2020-04-05 00:29:01 +02:00
parent afff228308
commit 16bd99aa52
Notes: sideshowbarker 2024-07-19 07:55:13 +09:00
2 changed files with 14 additions and 0 deletions

View file

@ -38,6 +38,7 @@ MathObject::MathObject()
put_native_function("random", random);
put_native_function("sqrt", sqrt, 1);
put_native_function("floor", floor, 1);
put_native_function("round", round, 1);
put("E", Value(M_E));
put("LN2", Value(M_LN2));
@ -96,4 +97,16 @@ Value MathObject::floor(Interpreter& interpreter)
return Value(::floor(number.as_double()));
}
Value MathObject::round(Interpreter& interpreter)
{
if (!interpreter.argument_count())
return js_nan();
auto number = interpreter.argument(0).to_number();
if (number.is_nan())
return js_nan();
// FIXME: Use ::round() instead of ::roundf().
return Value(::roundf(number.as_double()));
}
}

View file

@ -42,6 +42,7 @@ private:
static Value random(Interpreter&);
static Value sqrt(Interpreter&);
static Value floor(Interpreter&);
static Value round(Interpreter&);
};
}