Skip to content

Commit c3dc78f

Browse files
committed
math/big: replace Float.NewInf with Float.SetInf for more consistent API
Change-Id: I2a60ea4a196eef1af5d2aae6cc239c64bddb6fb2 Reviewed-on: https://go-review.googlesource.com/6301 Reviewed-by: Alan Donovan <[email protected]>
1 parent 612dd6c commit c3dc78f

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

src/math/big/float.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ const (
8282
MaxPrec = math.MaxUint32 // largest (theoretically) supported precision; likely memory-limited
8383
)
8484

85-
// NewInf returns a new infinite Float value with value +Inf (sign >= 0),
86-
// or -Inf (sign < 0).
87-
func NewInf(sign int) *Float {
88-
return &Float{neg: sign < 0, exp: infExp}
89-
}
90-
9185
// Accuracy describes the rounding error produced by the most recent
9286
// operation that generated a Float value, relative to the exact value:
9387
//
@@ -633,6 +627,17 @@ func (z *Float) SetRat(x *Rat) *Float {
633627
return z.Quo(&a, &b)
634628
}
635629

630+
// SetInf sets z to the infinite Float +Inf for sign >= 0,
631+
// or -Inf for sign < 0, and returns z. The precision of
632+
// z is unchanged and the result is always Exact.
633+
func (z *Float) SetInf(sign int) *Float {
634+
z.acc = Exact
635+
z.neg = sign < 0
636+
z.mant = z.mant[:0]
637+
z.exp = infExp
638+
return z
639+
}
640+
636641
// Set sets z to the (possibly rounded) value of x and returns z.
637642
// If z's precision is 0, it is changed to the precision of x
638643
// before setting z (and rounding will have no effect).

src/math/big/float_test.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ func TestFloatZeroValue(t *testing.T) {
9090
}
9191

9292
func makeFloat(s string) *Float {
93+
var x Float
9394
if s == "Inf" || s == "+Inf" {
94-
return NewInf(+1)
95+
return x.SetInf(+1)
9596
}
9697
if s == "-Inf" {
97-
return NewInf(-1)
98+
return x.SetInf(-1)
9899
}
99-
var x Float
100100
x.SetPrec(1000)
101101
if _, ok := x.SetString(s); !ok {
102102
panic(fmt.Sprintf("%q is not a valid float", s))
@@ -694,6 +694,27 @@ func TestFloatSetRat(t *testing.T) {
694694
}
695695
}
696696

697+
func TestFloatSetInf(t *testing.T) {
698+
var f Float
699+
for _, test := range []struct {
700+
sign int
701+
prec uint
702+
want string
703+
}{
704+
{0, 0, "+Inf"},
705+
{100, 0, "+Inf"},
706+
{-1, 0, "-Inf"},
707+
{0, 10, "+Inf"},
708+
{100, 20, "+Inf"},
709+
{-1, 30, "-Inf"},
710+
} {
711+
x := f.SetPrec(test.prec).SetInf(test.sign)
712+
if got := x.String(); got != test.want || x.Prec() != test.prec {
713+
t.Errorf("SetInf(%d) = %s (prec = %d); want %s (prec = %d)", test.sign, got, x.Prec(), test.want, test.prec)
714+
}
715+
}
716+
}
717+
697718
func TestFloatUint64(t *testing.T) {
698719
for _, test := range []struct {
699720
x string

0 commit comments

Comments
 (0)