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:
//
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).
}
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))
}
}
+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