diff options
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/math.nim | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index c05e47545..5b19a8ec0 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -882,6 +882,32 @@ func floorMod*[T: SomeNumber](x, y: T): T = result = x mod y if (result > 0 and y < 0) or (result < 0 and y > 0): result += y +func euclDiv*[T: SomeInteger](x, y: T): T {.since: (1, 5, 1).} = + ## Returns euclidean division of `x` by `y`. + runnableExamples: + assert euclDiv(13, 3) == 4 + assert euclDiv(-13, 3) == -5 + assert euclDiv(13, -3) == -4 + assert euclDiv(-13, -3) == 5 + result = x div y + if x mod y < 0: + if y > 0: + dec result + else: + inc result + +func euclMod*[T: SomeNumber](x, y: T): T {.since: (1, 5, 1).} = + ## Returns euclidean modulo of `x` by `y`. + ## `euclMod(x, y)` is non-negative. + runnableExamples: + assert euclMod(13, 3) == 1 + assert euclMod(-13, 3) == 2 + assert euclMod(13, -3) == 1 + assert euclMod(-13, -3) == 2 + result = x mod y + if result < 0: + result += abs(y) + when not defined(js): func c_frexp*(x: float32, exponent: var int32): float32 {. importc: "frexp", header: "<math.h>".} |