math.big, docs: document the behaviour of % for negative numbers; in V: -10 % 7 == -3 (#24604)

This commit is contained in:
Delyan Angelov 2025-05-30 05:49:19 +03:00 committed by GitHub
parent ebde7bc85e
commit 3ecffe68ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 0 deletions

View File

@ -8293,6 +8293,9 @@ Assignment Operators
&&= ||=
```
Note: in V, `assert -10 % 7 == -3` passes. In programming, the sign of the remainder
depends upon the signs of divisor and dividend.
## Other online resources
### [V contributing guide](https://github.com/vlang/v/blob/master/CONTRIBUTING.md)

View File

@ -470,6 +470,8 @@ pub fn (dividend Integer) / (divisor Integer) Integer {
//
// WARNING: this method will panic if `divisor == 0`. For a modular division method that
// returns a Result refer to `mod_checked`.
// Note: in V, `assert big.integer_from_i64(-10) % big.integer_from_i64(7) == big.integer_from_i64(-3)` passes.
// In other words, the result is negative 3, and is NOT positive 4.
@[inline]
pub fn (dividend Integer) % (divisor Integer) Integer {
if dividend.signum == -1 {