I was reading about new features in Julia 1.11 and happened upon this new invmod
capability: invmod(n::BitInteger): efficient native modular inverses by StefanKarpinski · Pull Request #52180 · JuliaLang/julia · GitHub .
As someone more accustomed to math than bit-twiddling, I didn’t realize at first that this is doing modular inverses in base 2^N
(determined by the max values of the type T
) and thus the argument must be odd to have a well-defined inverse. It would be great to add that requirement to the docs, and document what happens when the argument is even (a DomainError
exception appears to be the result).
Also, for consistency with other ares where an inverse doesn’t exist (e.g. computing 1/0
and getting Inf
), I wonder whether throwing an exception might not be the best option. Is there a suitable non-value in Julia for this situation? That might help callers who’d want to do invmod.(array)
on a bunch of values at once, for example.
Finally, I noticed that the unit tests don’t seem to limit themselves to odd values, but they don’t seem to worry about catching the DomainError
exception either - how does this work?