mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 02:27:35 +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:
parent
9eed7444de
commit
a939ffc617
1 changed files with 5 additions and 12 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue