X Tutup
Skip to content

Commit ea1fafb

Browse files
committed
math/big: modified MantExp semantics to enable fast exponent access
Change-Id: I9a6ebb747d5b9756c214bdeb19f60820602d7a24 Reviewed-on: https://go-review.googlesource.com/6340 Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent e053883 commit ea1fafb

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

src/math/big/float.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,31 +231,34 @@ func (x *Float) Sign() int {
231231
return 1
232232
}
233233

234-
// MantExp breaks x into its mantissa and exponent components.
235-
// It returns mant and exp satisfying x == mant × 2**exp, with
236-
// the absolute value of mant satisfying 0.5 <= |mant| < 1.0.
237-
// mant has the same precision and rounding mode as x.
238-
// If a non-nil *Float argument z is provided, MantExp stores
239-
// the result mant in z instead of allocating a new Float.
234+
// MantExp breaks x into its mantissa and exponent components
235+
// and returns the exponent. If a non-nil mant argument is
236+
// provided its value is set to the mantissa of x, with the
237+
// same precision and rounding mode as x. The components
238+
// satisfy x == mant × 2**exp, with 0.5 <= |mant| < 1.0.
239+
// Calling MantExp with a nil argument is an efficient way to
240+
// get the exponent of the receiver.
240241
//
241242
// Special cases are:
242243
//
243-
// ( ±0).MantExp() = ±0, 0
244-
// (±Inf).MantExp() = ±Inf, 0
245-
// ( NaN).MantExp() = NaN, 0
244+
// ( ±0).MantExp(mant) = 0, with mant set to ±0
245+
// (±Inf).MantExp(mant) = 0, with mant set to ±Inf
246+
// ( NaN).MantExp(mant) = 0, with mant set to NaN
246247
//
247-
// MantExp does not modify x; the result mant is a new Float.
248-
func (x *Float) MantExp(z *Float) (mant *Float, exp int) {
248+
// x and mant may be the same in which case x is set to its
249+
// mantissa value.
250+
func (x *Float) MantExp(mant *Float) (exp int) {
249251
if debugFloat {
250252
validate(x)
251253
}
252-
if z == nil {
253-
z = new(Float)
254-
}
255-
mant = z.Copy(x)
256-
if len(z.mant) != 0 {
254+
if len(x.mant) != 0 {
257255
exp = int(x.exp)
258-
mant.exp = 0 // after reading x.exp (x and mant may be aliases)
256+
}
257+
if mant != nil {
258+
mant.Copy(x)
259+
if x.exp >= MinExp {
260+
mant.exp = 0
261+
}
259262
}
260263
return
261264
}
@@ -265,7 +268,8 @@ func (x *Float) MantExp(z *Float) (mant *Float, exp int) {
265268
// as mant. SetMantExp is an inverse of MantExp but does
266269
// not require 0.5 <= |mant| < 1.0. Specifically:
267270
//
268-
// new(Float).SetMantExp(x.MantExp()).Cmp(x) == 0
271+
// mant := new(Float)
272+
// new(Float).SetMantExp(mant, x.SetMantExp(mant)).Cmp(x) == 0
269273
//
270274
// Special cases are:
271275
//

src/math/big/float_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func feq(x, y *Float) bool {
216216
func TestFloatMantExp(t *testing.T) {
217217
for _, test := range []struct {
218218
x string
219-
frac string
219+
mant string
220220
exp int
221221
}{
222222
{"0", "0", 0},
@@ -231,23 +231,23 @@ func TestFloatMantExp(t *testing.T) {
231231
{"-0.125", "-0.5", -2},
232232
} {
233233
x := makeFloat(test.x)
234-
frac := makeFloat(test.frac)
235-
f, e := x.MantExp(nil)
236-
if !feq(f, frac) || e != test.exp {
237-
t.Errorf("%s.MantExp(nil) = %s, %d; want %s, %d", test.x, f.Format('g', 10), e, test.frac, test.exp)
234+
mant := makeFloat(test.mant)
235+
m := new(Float)
236+
e := x.MantExp(m)
237+
if !feq(m, mant) || e != test.exp {
238+
t.Errorf("%s.MantExp() = %s, %d; want %s, %d", test.x, m.Format('g', 10), e, test.mant, test.exp)
238239
}
239240
}
240241
}
241242

242243
func TestFloatMantExpAliasing(t *testing.T) {
243244
x := makeFloat("0.5p10")
244-
z := new(Float)
245-
if m, _ := x.MantExp(z); m != z {
246-
t.Fatalf("Float.MantExp didn't use supplied *Float")
247-
}
248-
if _, e := x.MantExp(x); e != 10 {
245+
if e := x.MantExp(x); e != 10 {
249246
t.Fatalf("Float.MantExp aliasing error: got %d; want 10", e)
250247
}
248+
if want := makeFloat("0.5"); !feq(x, want) {
249+
t.Fatalf("Float.MantExp aliasing error: got %s; want %s", x.Format('g', 10), want.Format('g', 10))
250+
}
251251
}
252252

253253
func TestFloatSetMantExp(t *testing.T) {
@@ -281,7 +281,8 @@ func TestFloatSetMantExp(t *testing.T) {
281281
t.Errorf("SetMantExp(%s, %d) = %s; want %s", test.frac, test.exp, z.Format('g', 10), test.z)
282282
}
283283
// test inverse property
284-
if z.SetMantExp(want.MantExp(nil)).Cmp(want) != 0 {
284+
mant := new(Float)
285+
if z.SetMantExp(mant, want.MantExp(mant)).Cmp(want) != 0 {
285286
t.Errorf("Inverse property not satisfied: got %s; want %s", z.Format('g', 10), test.z)
286287
}
287288
}

0 commit comments

Comments
 (0)
X Tutup