diff options
author | Brian Kessler <brian.m.kessler@gmail.com> | 2017-08-17 23:44:34 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2017-08-18 12:47:46 +0000 |
commit | fe08ebaebb09cb0c45e3728a96d494f7f9f5c1b3 (patch) | |
tree | 89a180b485bd2af4d88a1ec504e5b2c5b6cc28b8 /src/math | |
parent | 66a1d37bf78b75f4b3f20e67ecf34be5c996f61e (diff) | |
download | go-git-fe08ebaebb09cb0c45e3728a96d494f7f9f5c1b3.tar.gz |
math/big: use internal square for Rat
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>
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/big/rat.go | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/math/big/rat.go b/src/math/big/rat.go index 56ce33d882..f0f436e452 100644 --- a/src/math/big/rat.go +++ b/src/math/big/rat.go @@ -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() |