]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: use internal square for Rat
authorBrian Kessler <brian.m.kessler@gmail.com>
Fri, 18 Aug 2017 06:44:34 +0000 (23:44 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 18 Aug 2017 12:47:46 +0000 (12:47 +0000)
updates #13745

A squared rational is always positive and can not
be reduced since the numerator and denominator had
no previous common factors.  The nat multiplication
can be performed using the internal sqr method.

Change-Id: I558f5b38e379bfd26ff163c9489006d7e5a9cfaa
Reviewed-on: https://go-review.googlesource.com/56776
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/math/big/rat.go

index 56ce33d8826bbe668bb2b04c4ca1827df13352ad..f0f436e452776a655088bddbc37e962b4d8640e8 100644 (file)
@@ -490,6 +490,13 @@ func (z *Rat) Sub(x, y *Rat) *Rat {
 
 // Mul sets z to the product x*y and returns z.
 func (z *Rat) Mul(x, y *Rat) *Rat {
+       if x == y {
+               // a squared Rat is positive and can't be reduced
+               z.a.neg = false
+               z.a.abs = z.a.abs.sqr(x.a.abs)
+               z.b.abs = z.b.abs.sqr(x.b.abs)
+               return z
+       }
        z.a.Mul(&x.a, &y.a)
        z.b.abs = mulDenom(z.b.abs, x.b.abs, y.b.abs)
        return z.norm()