1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:17:44 +00:00

LibJS: Handle values close to -0.5 correctly in Math.round(x)

This is done by just using the built-in ceiling and subtracting from
the result if its in the 0.5 range.
This commit is contained in:
Idan Horowitz 2021-06-28 02:04:02 +03:00 committed by Linus Groh
parent 9eed7444de
commit a939ffc617

View file

@ -146,18 +146,11 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::round)
auto number = vm.argument(0).to_number(global_object);
if (vm.exception())
return {};
if (number.is_nan())
return js_nan();
double intpart = 0;
double frac = modf(number.as_double(), &intpart);
if (intpart >= 0) {
if (frac >= 0.5)
intpart += 1.0;
} else {
if (frac < -0.5)
intpart -= 1.0;
}
return Value(intpart);
auto value = number.as_double();
double integer = ::ceil(value);
if (integer - 0.5 > value)
integer--;
return Value(integer);
}
// 21.3.2.24 Math.max ( ...args ), https://tc39.es/ecma262/#sec-math.max