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[]) diff --git a/src/vm/values/custom/vrational.nim b/src/vm/values/custom/vrational.nim index 5324d37b38..955f725fd1 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: neg(x.br) + ) + func floorDiv*(x, y: VRational): int = # floor division between given VRationals if x.rKind == NormalRational: diff --git a/src/vm/values/operators.nim b/src/vm/values/operators.nim index c856dbfa95..992c9eaca0 100644 --- a/src/vm/values/operators.nim +++ b/src/vm/values/operators.nim @@ -830,10 +830,7 @@ 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(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: diff --git a/version/build b/version/build index c5befbc75d..c99bf43de1 100644 --- a/version/build +++ b/version/build @@ -1 +1 @@ -856 \ No newline at end of file +858 \ No newline at end of file