From 7883c2e260accf31b0b1c46013ed7cae91167d55 Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Mon, 25 Jul 2022 18:44:07 -0500 Subject: [PATCH] Add signum function --- src/bigints.nim | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/bigints.nim b/src/bigints.nim index 9ef5abb..6981e41 100644 --- a/src/bigints.nim +++ b/src/bigints.nim @@ -178,6 +178,14 @@ func `==`(a: BigInt, b: int32): bool = cmp(a, b) == 0 func `<`(a: BigInt, b: int32): bool = cmp(a, b) < 0 func `<`(a: int32, b: BigInt): bool = cmp(a, b) < 0 +func sgn*(a: BigInt): BigInt = + # Return the signum of `a`. + runnableExamples: + assert sgn(42.initBigInt) == 1.initBigInt + assert sgn(-12.initBigInt) == -1.initBigInt + assert sgn(0.initBigInt) == 0.initBigInt + return sgn(cmp(a, 0)).initBigInt + template addParts(toAdd) = tmp += toAdd a.limbs[i] = uint32(tmp and uint32.high)