]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: permit passing of (possibly nil) *Float to MantExp to avoid allocation
authorRobert Griesemer <gri@golang.org>
Wed, 25 Feb 2015 00:42:39 +0000 (16:42 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 25 Feb 2015 00:46:27 +0000 (00:46 +0000)
Change-Id: Ia92eea833283f8b16fa09d4ca1c9cb3bc0eb18a2
Reviewed-on: https://go-review.googlesource.com/5870
Reviewed-by: Rob Pike <r@golang.org>
src/math/big/float.go
src/math/big/float_test.go

index 015f1645b61b1d153c7a3dab38e6cb28a0ed4af6..60e9a2d46db4edb574472a6575429f184246be7d 100644 (file)
@@ -203,6 +203,8 @@ func (x *Float) Sign() int {
 // It returns mant and exp satisfying x == mant × 2**exp, with
 // the absolute value of mant satisfying 0.5 <= |mant| < 1.0.
 // mant has the same precision and rounding mode as x.
+// If a non-nil *Float argument z is provided it is used to
+// store the result mant; otherwise a new Float is allocated.
 //
 // Special cases are:
 //
@@ -210,11 +212,14 @@ func (x *Float) Sign() int {
 //     (±Inf).MantExp() = ±Inf, 0
 //
 // MantExp does not modify x; the result mant is a new Float.
-func (x *Float) MantExp() (mant *Float, exp int) {
-       mant = new(Float).Copy(x)
+func (x *Float) MantExp(z *Float) (mant *Float, exp int) {
+       if z == nil {
+               z = new(Float)
+       }
+       mant = z.Copy(x)
        if x.exp != infExp {
-               mant.exp = 0
                exp = int(x.exp)
+               mant.exp = 0 // after reading x.exp (x and mant may be aliases)
        }
        return
 }
index 00bb3099080a75f390d1a257c0f16421ebd4babe..f7c243e71aa7ce5c554ccd4296a78fb8ebe4d990 100644 (file)
@@ -147,13 +147,24 @@ func TestFloatMantExp(t *testing.T) {
        } {
                x := makeFloat(test.x)
                frac := makeFloat(test.frac)
-               f, e := x.MantExp()
+               f, e := x.MantExp(nil)
                if !feq(f, frac) || e != test.exp {
                        t.Errorf("%s.MantExp() = %s, %d; want %s, %d", test.x, f.Format('g', 10), e, test.frac, test.exp)
                }
        }
 }
 
+func TestFloatMantExpAliasing(t *testing.T) {
+       x := makeFloat("0.5p10")
+       z := new(Float)
+       if m, _ := x.MantExp(z); m != z {
+               t.Fatalf("MantExp didn't use supplied *Float")
+       }
+       if _, e := x.MantExp(x); e != 10 {
+               t.Fatalf("MantExp aliasing error: got %d; want 10", e)
+       }
+}
+
 func TestFloatSetMantExp(t *testing.T) {
        for _, test := range []struct {
                frac string
@@ -185,7 +196,7 @@ func TestFloatSetMantExp(t *testing.T) {
                        t.Errorf("SetMantExp(%s, %d) = %s; want %s", test.frac, test.exp, z.Format('g', 10), test.z)
                }
                // test inverse property
-               if z.SetMantExp(want.MantExp()).Cmp(want) != 0 {
+               if z.SetMantExp(want.MantExp(nil)).Cmp(want) != 0 {
                        t.Errorf("Inverse property not satisfied: got %s; want %s", z.Format('g', 10), test.z)
                }
        }