mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-11 17:00:37 +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
Notes:
sideshowbarker
2024-07-18 11:25:51 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/a939ffc6174 Pull-request: https://github.com/SerenityOS/serenity/pull/8279
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…
Reference in a new issue