Skip to content

R consistent integer division and modulo operation#250

Merged
MichaelChirico merged 3 commits intomainfrom
modulo
Jan 25, 2026
Merged

R consistent integer division and modulo operation#250
MichaelChirico merged 3 commits intomainfrom
modulo

Conversation

@hcirellu
Copy link
Copy Markdown
Collaborator

The integer division is changed to meet the behavior of base R for integer. I couldn't find any "option" to change the behaviour for long long int, so I added logic.

Closes #248

@hcirellu hcirellu marked this pull request as ready for review January 19, 2026 13:10
Comment thread NEWS.md Outdated

1. `as.integer64.integer64` returns a plain `integer64` vector stripped of any attributes. This is consistent with R like behavior, e.g. `as.integer.integer`.

1. `%/%` does not truncate towards zero any more. It now matches R behavior of truncating towards negative infinity. For example, `as.integer64(-10L) %/% as.integer64(7L)` now gives `-2L`, not `-1L`. This is consistent with `-10L %/% 7L` in base R. Consequently, `%%` is also affected, e.g. `as.integer64(-10L) %% as.integer64(7L)` now gives `4L`, not `-3L`, consistent with `-10L %% 7L` in base R.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ?"%/%" this is described as Knuth's floor() behavior:

x %/% y := floor(x / y)

I think it will be helpful to use that terminology to anchor more definitively to the base rule.

Copy link
Copy Markdown
Collaborator

@MichaelChirico MichaelChirico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the previous behavior is IMO incorrect, I think of this more as a bug, hence less concerned about back-compatibility.

Still, I'll keep an eye on it aroud release time when doing reverse dependency checks -- if it is a recurring source of downstream breaking, we may guard this with a flag, too.

@MichaelChirico MichaelChirico merged commit 60c9c3f into main Jan 25, 2026
8 checks passed
hcirellu added a commit that referenced this pull request Jan 26, 2026
* r consistent integer division

* tweak NEWS

---------

Co-authored-by: Michael Chirico <chiricom@google.com>
@MichaelChirico MichaelChirico deleted the modulo branch March 2, 2026 17:24
MichaelChirico added a commit that referenced this pull request Mar 13, 2026
* extend c, cbind, and rbind

* consistent use of Rboolean (#251)

* R consistent integer division and modulo operation (#250)

* r consistent integer division

* tweak NEWS

---------

Co-authored-by: Michael Chirico <chiricom@google.com>

* fix test ancient

* add origin for as.{Date,POSIXct,POSIXlt} for ancient

* `stringsAsFactors=FALSE` for ancient

* fix ancient

* skip a test for ancient

* fix cbind tests for ancient

* skip cbind tests for ancient

* skip rbind tests for ancient

* fix target_class_and_sample_value for missing .bit64.suppressPromoteInteger64ToCharacterMessage

* merge target_class_and_sample_value in  target_class

* touch-ups to c()

* touch-ups to rbind,cbind

* missing ')'

* inherits() instead of class()

Co-authored-by: Michael Chirico <chiricom@google.com>

* remove eval(str2lang())

* another round of touch-up

* vestigial ')'

* missing '{'

* simplify (?) replacement helper for cbind

* more expect_same_error

* switch branch order

* fix updated logical condition for unnested branches

* revert unrelated switch to expect_same_error

* replace rbind helper too

* fix helper argument name

Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>

* refactoring, add comments, add news

* extract replace_dimnames

* tweak NEWS

* fix deparse.level problems

* skip ancient test

* more canonical patrick (avoid expand.grid), don't test 4

* raw cases

* missing symmetric cases for difftime, factor, ordered

* explicitly ignore coercion warning before error

---------

Co-authored-by: Michael Chirico <chiricom@google.com>
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
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.

Modulo operation with negative numbers is inconsistent to base R

2 participants