Skip to content

float.__mod__ and float.__divmod__ produce incorrect results for negative numbers#777

Open
pauvrepetit wants to merge 2 commits intoexaloop:developfrom
pauvrepetit:fix/float-mod-divmod
Open

float.__mod__ and float.__divmod__ produce incorrect results for negative numbers#777
pauvrepetit wants to merge 2 commits intoexaloop:developfrom
pauvrepetit:fix/float-mod-divmod

Conversation

@pauvrepetit
Copy link
Copy Markdown
Contributor

The current __mod__ (using LLVM frem) and __floordiv__ implementations have mismatched semantics. When dealing with negative numbers, this leads to (a // b) * b + (a % b) != a, which is clearly a bug.

LLVM does not provide an instruction that directly corresponds to Python's floored mod for floats. Since __floordiv__ is already implemented as floor(a / b), computing __mod__ via the identity (a // b) * b + (a % b) == a, i.e. a - (a // b) * b, is a better and more correct approach.

Accordingly, __divmod__ which relied on the frem-based __mod__ logic is also updated to match.

Affected types: float, float32, float16, bfloat16, float128.

Fixing this issue #775

The current __mod__ (using LLVM frem) and __floordiv__ implementations
have mismatched semantics. When dealing with negative numbers, this
leads to (a // b) * b + (a % b) != a, which is clearly a bug.

LLVM does not provide an instruction that directly corresponds to
Python's floored mod for floats. Since __floordiv__ is already
implemented as floor(a / b), computing __mod__ via the identity
(a // b) * b + (a % b) == a, i.e. a - (a // b) * b, is a better
and more correct approach.

Accordingly, __divmod__ which relied on the frem-based __mod__ logic
is also updated to match.

Affected types: float, float32, float16, bfloat16, float128.
… type

Delegate to float(self) % other and float(self) // other instead of
using LLVM instructions directly, ensuring consistent semantics with
the corrected float.__mod__ and float.__floordiv__ implementations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant