]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: replace Float.NewInf with Float.SetInf for more consistent API
authorRobert Griesemer <gri@golang.org>
Wed, 25 Feb 2015 23:38:15 +0000 (15:38 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 2 Mar 2015 20:35:49 +0000 (20:35 +0000)
Change-Id: I2a60ea4a196eef1af5d2aae6cc239c64bddb6fb2
Reviewed-on: https://go-review.googlesource.com/6301
Reviewed-by: Alan Donovan <adonovan@google.com>
src/math/big/float.go
src/math/big/float_test.go

index c1a197917c64db9e4d8669d196f140a9a007ee90..c4ae2ffd2a2948133ca6fd7572299efe485b4d46 100644 (file)
@@ -82,12 +82,6 @@ const (
        MaxPrec = math.MaxUint32 // largest (theoretically) supported precision; likely memory-limited
 )
 
-// NewInf returns a new infinite Float value with value +Inf (sign >= 0),
-// or -Inf (sign < 0).
-func NewInf(sign int) *Float {
-       return &Float{neg: sign < 0, exp: infExp}
-}
-
 // Accuracy describes the rounding error produced by the most recent
 // operation that generated a Float value, relative to the exact value:
 //
@@ -633,6 +627,17 @@ func (z *Float) SetRat(x *Rat) *Float {
        return z.Quo(&a, &b)
 }
 
+// SetInf sets z to the infinite Float +Inf for sign >= 0,
+// or -Inf for sign < 0, and returns z. The precision of
+// z is unchanged and the result is always Exact.
+func (z *Float) SetInf(sign int) *Float {
+       z.acc = Exact
+       z.neg = sign < 0
+       z.mant = z.mant[:0]
+       z.exp = infExp
+       return z
+}
+
 // Set sets z to the (possibly rounded) value of x and returns z.
 // If z's precision is 0, it is changed to the precision of x
 // before setting z (and rounding will have no effect).
index cc58d966e34ebd93c05062ef13b527901e18e0d8..c7486b1330b52d1aa9178ce6ace9412a9c8fdca8 100644 (file)
@@ -90,13 +90,13 @@ func TestFloatZeroValue(t *testing.T) {
 }
 
 func makeFloat(s string) *Float {
+       var x Float
        if s == "Inf" || s == "+Inf" {
-               return NewInf(+1)
+               return x.SetInf(+1)
        }
        if s == "-Inf" {
-               return NewInf(-1)
+               return x.SetInf(-1)
        }
-       var x Float
        x.SetPrec(1000)
        if _, ok := x.SetString(s); !ok {
                panic(fmt.Sprintf("%q is not a valid float", s))
@@ -694,6 +694,27 @@ func TestFloatSetRat(t *testing.T) {
        }
 }
 
+func TestFloatSetInf(t *testing.T) {
+       var f Float
+       for _, test := range []struct {
+               sign int
+               prec uint
+               want string
+       }{
+               {0, 0, "+Inf"},
+               {100, 0, "+Inf"},
+               {-1, 0, "-Inf"},
+               {0, 10, "+Inf"},
+               {100, 20, "+Inf"},
+               {-1, 30, "-Inf"},
+       } {
+               x := f.SetPrec(test.prec).SetInf(test.sign)
+               if got := x.String(); got != test.want || x.Prec() != test.prec {
+                       t.Errorf("SetInf(%d) = %s (prec = %d); want %s (prec = %d)", test.sign, got, x.Prec(), test.want, test.prec)
+               }
+       }
+}
+
 func TestFloatUint64(t *testing.T) {
        for _, test := range []struct {
                x   string