From 7f319f8e23b911d482b813bb37432d6a64ae44e4 Mon Sep 17 00:00:00 2001 From: drkameleon Date: Tue, 13 Jun 2023 18:01:44 +0200 Subject: [PATCH 1/7] replace with `neg(x.rat)` --- src/vm/values/operators.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/values/operators.nim b/src/vm/values/operators.nim index c856dbfa95..43ea1fadb8 100644 --- a/src/vm/values/operators.nim +++ b/src/vm/values/operators.nim @@ -833,7 +833,7 @@ proc neg*(x: Value): Value = # TODO(VRational) add `neg` overload # it can be faster for BigRational values # labels: 3rd-party, enhancement, values - of Rational: return newRational(x.rat*(-1)) + of Rational: return newRational(neg(x.rat)) of Complex: return newComplex(x.z*(-1.0)) of Quantity: return newQuantity(x.q*(-1)) else: From 1690fbaaa5fe65084679a0bb70dbc0e3bcb38a9b Mon Sep 17 00:00:00 2001 From: drkameleon Date: Tue, 13 Jun 2023 18:01:50 +0200 Subject: [PATCH 2/7] build update --- version/build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version/build b/version/build index c5befbc75d..f6a32a9736 100644 --- a/version/build +++ b/version/build @@ -1 +1 @@ -856 \ No newline at end of file +857 \ No newline at end of file From 1d3d4760d5cc3acaa60b5e55fcdf6e4481b3b366 Mon Sep 17 00:00:00 2001 From: drkameleon Date: Tue, 13 Jun 2023 18:04:26 +0200 Subject: [PATCH 3/7] VM/values/custom/vrational: added `neg` overload --- src/vm/values/custom/vrational.nim | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/vm/values/custom/vrational.nim b/src/vm/values/custom/vrational.nim index 5324d37b38..2a2110b04c 100644 --- a/src/vm/values/custom/vrational.nim +++ b/src/vm/values/custom/vrational.nim @@ -999,6 +999,21 @@ func abs*(x: VRational): VRational = br: abs(x.br) ) +func neg*(x: VRational): VRational = + # negation of VRational + if x.rKind == NormalRational: + result = VRational( + rKind: NormalRational, + num: -x.num, + den: x.den + ) + else: + when not defined(NOGMP): + result = VRational( + rKind: BigRational, + br: -x.br + ) + func floorDiv*(x, y: VRational): int = # floor division between given VRationals if x.rKind == NormalRational: From 238e2cf4a4ca86a5a0fb13fbcd163ba813b4bc93 Mon Sep 17 00:00:00 2001 From: drkameleon Date: Tue, 13 Jun 2023 18:07:07 +0200 Subject: [PATCH 4/7] helpers/bignums: added proper `neg` overload for GMP-based Rat values (bignum rationals) --- src/helpers/bignums.nim | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/helpers/bignums.nim b/src/helpers/bignums.nim index 103936fb7f..29a01ef14d 100644 --- a/src/helpers/bignums.nim +++ b/src/helpers/bignums.nim @@ -630,9 +630,6 @@ func `*=`*(z: Int, x: int | culong | Int) = func `*=`*(x, y: Rat) = discard x.mul(x, y) -func neg*(x: Rat): Rat = - mpq_neg(result[], x[]) - func inv*(x: Rat): Rat = mpq_inv(result[], x[]) @@ -1080,6 +1077,13 @@ func neg*(x: Int): Int = func negI*(x: Int) = mpz_neg(x[], x[]) +func neg*(z, x: Rat): Rat = + result = z + mpq_neg(result[], x[]) + +func neg*(x: Rat): Rat = + newRat().neg(x) + func gcd*(z, x, y: Int): Int = result = z mpz_gcd(z[], x[], y[]) From 54ea50ef010971a8018fc1e244600d43c0e7a42d Mon Sep 17 00:00:00 2001 From: drkameleon Date: Tue, 13 Jun 2023 18:07:32 +0200 Subject: [PATCH 5/7] use our new `neg` for GMP rationals --- src/vm/values/custom/vrational.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/values/custom/vrational.nim b/src/vm/values/custom/vrational.nim index 2a2110b04c..955f725fd1 100644 --- a/src/vm/values/custom/vrational.nim +++ b/src/vm/values/custom/vrational.nim @@ -1011,7 +1011,7 @@ func neg*(x: VRational): VRational = when not defined(NOGMP): result = VRational( rKind: BigRational, - br: -x.br + br: neg(x.br) ) func floorDiv*(x, y: VRational): int = From 6c669f9d9041126e1df18a0987a003251e54036b Mon Sep 17 00:00:00 2001 From: drkameleon Date: Tue, 13 Jun 2023 18:08:02 +0200 Subject: [PATCH 6/7] removed TODO --- src/vm/values/operators.nim | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/vm/values/operators.nim b/src/vm/values/operators.nim index 43ea1fadb8..992c9eaca0 100644 --- a/src/vm/values/operators.nim +++ b/src/vm/values/operators.nim @@ -830,9 +830,6 @@ proc neg*(x: Value): Value = if x.iKind==NormalInteger: return normalIntegerNeg(x.i) else: (when GMP: return newInteger(neg(x.bi))) of Floating: return newFloating(x.f*(-1.0)) - # TODO(VRational) add `neg` overload - # it can be faster for BigRational values - # labels: 3rd-party, enhancement, values of Rational: return newRational(neg(x.rat)) of Complex: return newComplex(x.z*(-1.0)) of Quantity: return newQuantity(x.q*(-1)) From 4507eef46b22c5f8eece2cd96fa94ae43eda4007 Mon Sep 17 00:00:00 2001 From: drkameleon Date: Tue, 13 Jun 2023 18:08:06 +0200 Subject: [PATCH 7/7] build update --- version/build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version/build b/version/build index f6a32a9736..c99bf43de1 100644 --- a/version/build +++ b/version/build @@ -1 +1 @@ -857 \ No newline at end of file +858 \ No newline at end of file