Skip to content

Commit

Permalink
fix modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
pthomalla committed Nov 25, 2018
1 parent d228ce9 commit d7f80c6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/math/math.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ defmodule ExthCrypto.Math do
iex> ExthCrypto.Math.mod(0, 1337)
0
iex> ExthCrypto.Math.mod(-75, 32)
21
"""
def mod(x, n) when x > 0, do: rem x, n
def mod(x, n) when x < 0, do: rem n + x, n
def mod(0, _n), do: 0
def mod(x, n) do
remainder = rem(x, n)

if remainder < 0,
do: n + remainder,
else: remainder
end

@doc """
Simple wrapper function to convert a hex string to a binary.
Expand Down Expand Up @@ -108,4 +115,4 @@ defmodule ExthCrypto.Math do
:crypto.exor(a, b)
end

end
end

0 comments on commit d7f80c6

Please sign in to comment.