From 2c07c2a58cd7b7754509509320d3a2b950b2078e Mon Sep 17 00:00:00 2001 From: Ishaan Bhardwaj <59454579+SashLilac@users.noreply.github.com> Date: Tue, 16 Feb 2021 13:03:53 -0500 Subject: [PATCH] BigInt changes, read extended description Disabled strict type checking, can be re-enabled in bleeding edge. (This is done so bigint ops run faster) Added a negation method and updated the corresponding metamethod to use it. --- libs/bigint/bigint.lua | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/libs/bigint/bigint.lua b/libs/bigint/bigint.lua index 286e70e..d86031c 100644 --- a/libs/bigint/bigint.lua +++ b/libs/bigint/bigint.lua @@ -2,7 +2,7 @@ -- If this variable is true, then strict type checking is performed for all -- operations. This may result in slower code, but it will allow you to catch -- errors and bugs earlier. -local strict = true +local strict = false -------------------------------------------------------------------------------- @@ -33,12 +33,7 @@ function bigint.new(num) return bigint.add(lhs, rhs) end, __unm = function() - if (self.sign == "+") then - self.sign = "-" - else - self.sign = "+" - end - return self + return bigint.negate(self) end, __sub = function(lhs, rhs) return bigint.subtract(lhs, rhs) @@ -98,6 +93,18 @@ function bigint.abs(big) return result end +-- Return a new big with the same digits but the opposite sign (negation) +function bigint.negate(big) + bigint.check(big) + local result = big:clone() + if (result.sign == "+") then + result.sign = "-" + else + result.sign = "+" + end + return result +end + -- Return the number of digits in the big function bigint.digits(big) bigint.check(big)