Commit ea1fafbc authored by Robert Griesemer's avatar Robert Griesemer

math/big: modified MantExp semantics to enable fast exponent access

Change-Id: I9a6ebb747d5b9756c214bdeb19f60820602d7a24
Reviewed-on: https://go-review.googlesource.com/6340Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent e0538833
...@@ -231,31 +231,34 @@ func (x *Float) Sign() int { ...@@ -231,31 +231,34 @@ func (x *Float) Sign() int {
return 1 return 1
} }
// MantExp breaks x into its mantissa and exponent components. // MantExp breaks x into its mantissa and exponent components
// It returns mant and exp satisfying x == mant × 2**exp, with // and returns the exponent. If a non-nil mant argument is
// the absolute value of mant satisfying 0.5 <= |mant| < 1.0. // provided its value is set to the mantissa of x, with the
// mant has the same precision and rounding mode as x. // same precision and rounding mode as x. The components
// If a non-nil *Float argument z is provided, MantExp stores // satisfy x == mant × 2**exp, with 0.5 <= |mant| < 1.0.
// the result mant in z instead of allocating a new Float. // Calling MantExp with a nil argument is an efficient way to
// get the exponent of the receiver.
// //
// Special cases are: // Special cases are:
// //
// ( ±0).MantExp() = ±0, 0 // ( ±0).MantExp(mant) = 0, with mant set to ±0
// (±Inf).MantExp() = ±Inf, 0 // (±Inf).MantExp(mant) = 0, with mant set to ±Inf
// ( NaN).MantExp() = NaN, 0 // ( NaN).MantExp(mant) = 0, with mant set to NaN
// //
// MantExp does not modify x; the result mant is a new Float. // x and mant may be the same in which case x is set to its
func (x *Float) MantExp(z *Float) (mant *Float, exp int) { // mantissa value.
func (x *Float) MantExp(mant *Float) (exp int) {
if debugFloat { if debugFloat {
validate(x) validate(x)
} }
if z == nil { if len(x.mant) != 0 {
z = new(Float)
}
mant = z.Copy(x)
if len(z.mant) != 0 {
exp = int(x.exp) exp = int(x.exp)
mant.exp = 0 // after reading x.exp (x and mant may be aliases) }
if mant != nil {
mant.Copy(x)
if x.exp >= MinExp {
mant.exp = 0
}
} }
return return
} }
...@@ -265,7 +268,8 @@ func (x *Float) MantExp(z *Float) (mant *Float, exp int) { ...@@ -265,7 +268,8 @@ func (x *Float) MantExp(z *Float) (mant *Float, exp int) {
// as mant. SetMantExp is an inverse of MantExp but does // as mant. SetMantExp is an inverse of MantExp but does
// not require 0.5 <= |mant| < 1.0. Specifically: // not require 0.5 <= |mant| < 1.0. Specifically:
// //
// new(Float).SetMantExp(x.MantExp()).Cmp(x) == 0 // mant := new(Float)
// new(Float).SetMantExp(mant, x.SetMantExp(mant)).Cmp(x) == 0
// //
// Special cases are: // Special cases are:
// //
......
...@@ -216,7 +216,7 @@ func feq(x, y *Float) bool { ...@@ -216,7 +216,7 @@ func feq(x, y *Float) bool {
func TestFloatMantExp(t *testing.T) { func TestFloatMantExp(t *testing.T) {
for _, test := range []struct { for _, test := range []struct {
x string x string
frac string mant string
exp int exp int
}{ }{
{"0", "0", 0}, {"0", "0", 0},
...@@ -231,23 +231,23 @@ func TestFloatMantExp(t *testing.T) { ...@@ -231,23 +231,23 @@ func TestFloatMantExp(t *testing.T) {
{"-0.125", "-0.5", -2}, {"-0.125", "-0.5", -2},
} { } {
x := makeFloat(test.x) x := makeFloat(test.x)
frac := makeFloat(test.frac) mant := makeFloat(test.mant)
f, e := x.MantExp(nil) m := new(Float)
if !feq(f, frac) || e != test.exp { e := x.MantExp(m)
t.Errorf("%s.MantExp(nil) = %s, %d; want %s, %d", test.x, f.Format('g', 10), e, test.frac, test.exp) if !feq(m, mant) || e != test.exp {
t.Errorf("%s.MantExp() = %s, %d; want %s, %d", test.x, m.Format('g', 10), e, test.mant, test.exp)
} }
} }
} }
func TestFloatMantExpAliasing(t *testing.T) { func TestFloatMantExpAliasing(t *testing.T) {
x := makeFloat("0.5p10") x := makeFloat("0.5p10")
z := new(Float) if e := x.MantExp(x); e != 10 {
if m, _ := x.MantExp(z); m != z {
t.Fatalf("Float.MantExp didn't use supplied *Float")
}
if _, e := x.MantExp(x); e != 10 {
t.Fatalf("Float.MantExp aliasing error: got %d; want 10", e) t.Fatalf("Float.MantExp aliasing error: got %d; want 10", e)
} }
if want := makeFloat("0.5"); !feq(x, want) {
t.Fatalf("Float.MantExp aliasing error: got %s; want %s", x.Format('g', 10), want.Format('g', 10))
}
} }
func TestFloatSetMantExp(t *testing.T) { func TestFloatSetMantExp(t *testing.T) {
...@@ -281,7 +281,8 @@ func TestFloatSetMantExp(t *testing.T) { ...@@ -281,7 +281,8 @@ func TestFloatSetMantExp(t *testing.T) {
t.Errorf("SetMantExp(%s, %d) = %s; want %s", test.frac, test.exp, z.Format('g', 10), test.z) t.Errorf("SetMantExp(%s, %d) = %s; want %s", test.frac, test.exp, z.Format('g', 10), test.z)
} }
// test inverse property // test inverse property
if z.SetMantExp(want.MantExp(nil)).Cmp(want) != 0 { mant := new(Float)
if z.SetMantExp(mant, want.MantExp(mant)).Cmp(want) != 0 {
t.Errorf("Inverse property not satisfied: got %s; want %s", z.Format('g', 10), test.z) t.Errorf("Inverse property not satisfied: got %s; want %s", z.Format('g', 10), test.z)
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment