-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Open
Description
Number has a #divmod method, which returns the floored quotient (#//) and the modulus (#%) as a 2-tuple:
Lines 217 to 219 in 791b0e4
| def divmod(number) | |
| {(self // number).floor, self % number} | |
| end |
There is no counterpart for the truncated quotient (#tdiv) and remainder (#remainder). Two examples in the standard library are:
Lines 667 to 668 in 791b0e4
| ary << num.remainder(base).to_i | |
| num = num.tdiv(base) |
Lines 68 to 69 in 791b0e4
| @seconds = seconds.to_i64 + nanoseconds.tdiv(NANOSECONDS_PER_SECOND).to_i64 | |
| @nanoseconds = nanoseconds.remainder(NANOSECONDS_PER_SECOND).to_i32 |
BigInt also has a specialized function for computing both results together, similar to its current #divmod override. I think we should add such a method for completeness. In other languages:
- C/C++:
div,ldiv,lldiv,imaxdiv(there is no#divmodequivalent) - C#:
System.Math.DivRem(there is no#divmodequivalent) - Java:
java.math.BigInteger.divideAndRemainder(there is no#divmodequivalent) - Haskell:
quotRem - Python, Ruby: only
divmodis present
If we want to emphasize the similarity with the floored version, #tdivmod is also a possible candidate.
straight-shoota