Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncated quotient and remainder #14948

Open
HertzDevil opened this issue Aug 27, 2024 · 0 comments
Open

Truncated quotient and remainder #14948

HertzDevil opened this issue Aug 27, 2024 · 0 comments

Comments

@HertzDevil
Copy link
Contributor

Number has a #divmod method, which returns the floored quotient (#//) and the modulus (#%) as a 2-tuple:

crystal/src/number.cr

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:

crystal/src/int.cr

Lines 667 to 668 in 791b0e4

ary << num.remainder(base).to_i
num = num.tdiv(base)

crystal/src/time/span.cr

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:

If we want to emphasize the similarity with the floored version, #tdivmod is also a possible candidate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant