Skip to content

float.__mod__ is unstable: if n % k == k then n % k % k == 0 #135859

Closed as not planned
@cls-oi

Description

@cls-oi

Bug report

Bug description:

As an example:

>>> import math
>>> n = math.atan2(-1.0, 4503599627370496.0)
>>> k = 2 * math.pi
>>> n
-2.220446049250313e-16
>>> k
6.283185307179586
>>> n % k
6.283185307179586
>>> n % k % k
0.0
>>>

This looks to be due to:

mod = fmod(vx, wx);
if (mod) {
/* ensure the remainder has the same sign as the denominator */
if ((wx < 0) != (mod < 0)) {
mod += wx;
}
}

On the first modulo we have mod < 0 but mod + wx == wx, so n % k == k, and then on the second modulo we have fmod(wx, wx) == 0, so n % k % k == 0.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions