diff options
author | flywind <43030857+xflywind@users.noreply.github.com> | 2020-12-23 05:04:38 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-23 12:04:38 +0100 |
commit | b1c3dab2089bb4327627c2152a1148b299f1a930 (patch) | |
tree | 0369cb3b3b003a7ea9b954e3048cd9016a4b6d2a /lib | |
parent | 417c2509c42367edc4130a8761fae80d529d8574 (diff) | |
download | Nim-b1c3dab2089bb4327627c2152a1148b299f1a930.tar.gz |
add `euclDiv` and `euclMod` to `math` (#16414)
* add `euclDiv` and `euclMod` to `math` * use abs * Update lib/pure/math.nim Co-authored-by: Clyybber <darkmine956@gmail.com>
Diffstat (limited to 'lib')
-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>".} |