The PR can be found here, by the way: https://github.com/elixir-lang/elixir/pull/5112
On Monday, August 8, 2016 at 10:24:12 AM UTC+2, Wiebe-Marten Wijnja wrote: > > Elixir has `rem(dividend, divisor)` to calculate the remainder of an > integer division. However, as you probably know, there are different ways > to handle remainders with negative numbers: > > `rem` will take the remainder, but use the sign of the dividend, which > means that the following function definition: > > def is_odd(n) > rem(n, 2) == 1 > end > > will fail for -1, -3, etc, as `rem(-3, 2)`, for instance, has as answer > *-1*. > > It is also possible to make a *modulo* function, which uses the sign of > the divisor. This has the advantage over the remainder function that it > maps the whole domain of integers unto a smaller, cyclic, range of numbers. > This is highly useful in many algorithms dealing with numbers or indices. > > Many programming languages both have a remainder and a modulo function. I > have seen multiple people roll their own definitions of `mod` in their > Elixir libraries, because they needed it. > > I propose the addition of the modulo function, `mod(a, n)` to Elixir. > > A possible implementation is as follows: > > defmacro mod(a, n) do > quote do > unquote(a) - (unquote(n) * div(unquote(a), unquote(n)) > end > end > > This implementation is guard-safe, so it can be used anywhere where `div` > and `rem` are also allowed. > > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/41b2c2ec-73d9-423b-9e59-0781b68c56ed%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
