Skip to content

improve remainder and modulo exact---inexact combinations#1010

Merged
mflatt merged 1 commit intocisco:mainfrom
mflatt:remainder
Jan 28, 2026
Merged

improve remainder and modulo exact---inexact combinations#1010
mflatt merged 1 commit intocisco:mainfrom
mflatt:remainder

Conversation

@mflatt
Copy link
Contributor

@mflatt mflatt commented Jan 25, 2026

Intended to fix #1008:

  • When the second argument is 1 or -1 and the first argument is inexact, return inexact 0.0.

  • When the first argument is exact and the second argument is inexact, compute as exact and then convert to inexact.

A calculation like (remainder #e1e1000 3.0) is about 10 times slower. Interestingly, (remainder #e1e100 3.0) is about 10 times faster, and (remainder #e1e10 3.0) is about twice as fast.

@burgerrg
Copy link
Contributor

For the case of remainder with exact 1 or -1, why would the answer be inexact? The remainder of any real number and exact 1 is exactly 0.

The update to the exact/inexact case produces the closest correctly rounded double, which makes a lot of sense to me.

@mflatt
Copy link
Contributor Author

mflatt commented Jan 28, 2026

For the case of remainder with exact 1 or -1, why would the answer be inexact? The remainder of any real number and exact 1 is exactly 0.

I don't think that's the way remainder and modulo are usually generalized to rationals. For example, 1.2 % 1 in Python is close to 0.2. That works out differently than, say, multiplying by exact 0, where the result does always end up being 0.

We're not generalizing to rationals, but an exact 0 result still seems right to me on the grounds that something like 7.0 is not exactly an integer.

@burgerrg
Copy link
Contributor

Thanks, I now understand the rationale for making the remainder of an inexact integer with 1 or -1 to be inexact 0.0. That makes sense.

When the second argument is `1` or `-1` and the first argument is
ineact, return inexact `0`.

When the first argument is exact and the second argument is inexact,
compute as exact and then convert to inexact.
@mflatt mflatt merged commit 4580875 into cisco:main Jan 28, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Result of (remainder 7.0 1) and (modulo 7.0 1)

2 participants