mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-07 09:31:53 +00:00
LibJS: Fix parseFloat(-0) returning -0 instead of +0
The optimization that skips the string conversion for number values was causing -0 to be returned as-is. This patch adds a check for this case.
This commit is contained in:
parent
61744322ad
commit
53cdb04ee8
Notes:
github-actions[bot]
2025-03-02 16:31:28 +00:00
Author: https://github.com/aplefull
Commit: 53cdb04ee8
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3765
Reviewed-by: https://github.com/tcl3
Reviewed-by: https://github.com/trflynn89 ✅
2 changed files with 27 additions and 2 deletions
|
@ -1,6 +1,16 @@
|
|||
// This is necessary because we can't simply check if 0 is negative using equality,
|
||||
// since -0 === 0 evaluates to true.
|
||||
// test262 checks for negative zero in a similar way here:
|
||||
// https://github.com/tc39/test262/blob/main/test/built-ins/parseFloat/S15.1.2.3_A1_T2.js
|
||||
function isZeroNegative(x) {
|
||||
const isZero = x === 0;
|
||||
const isNegative = 1 / x === Number.NEGATIVE_INFINITY;
|
||||
|
||||
return isZero && isNegative;
|
||||
}
|
||||
|
||||
test("parsing numbers", () => {
|
||||
[
|
||||
[0, 0],
|
||||
[1, 1],
|
||||
[0.23, 0.23],
|
||||
[1.23, 1.23],
|
||||
|
@ -14,6 +24,16 @@ test("parsing numbers", () => {
|
|||
});
|
||||
});
|
||||
|
||||
test("parsing zero", () => {
|
||||
expect(isZeroNegative(parseFloat("0"))).toBeFalse();
|
||||
expect(isZeroNegative(parseFloat("+0"))).toBeFalse();
|
||||
expect(isZeroNegative(parseFloat("-0"))).toBeTrue();
|
||||
|
||||
expect(isZeroNegative(parseFloat(0))).toBeFalse();
|
||||
expect(isZeroNegative(parseFloat(+0))).toBeFalse();
|
||||
expect(isZeroNegative(parseFloat(-0))).toBeFalse();
|
||||
});
|
||||
|
||||
test("parsing strings", () => {
|
||||
[
|
||||
["0", 0],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue