// ±Inf are not considered integers.
func (x *Float) IsInt() bool {
if debugFloat {
- x.validate()
+ validate(x)
}
// pick off easy cases
if x.exp <= 0 {
}
// debugging support
-func (x *Float) validate() {
- const msb = 1 << (_W - 1)
- m := len(x.mant)
- if m == 0 {
- // 0.0 or Inf
- if x.exp != 0 && x.exp != infExp {
- panic(fmt.Sprintf("empty matissa with invalid exponent %d", x.exp))
+func validate(args ...*Float) {
+ for i, x := range args {
+ const msb = 1 << (_W - 1)
+ m := len(x.mant)
+ if m == 0 {
+ // 0.0 or Inf
+ if x.exp != 0 && x.exp != infExp {
+ panic(fmt.Sprintf("#%d: %empty matissa with invalid exponent %d", i, x.exp))
+ }
+ continue
+ }
+ if x.mant[m-1]&msb == 0 {
+ panic(fmt.Sprintf("#%d: msb not set in last word %#x of %s", i, x.mant[m-1], x.Format('p', 0)))
+ }
+ if x.prec <= 0 {
+ panic(fmt.Sprintf("#%d: invalid precision %d", i, x.prec))
}
- return
- }
- if x.mant[m-1]&msb == 0 {
- panic(fmt.Sprintf("msb not set in last word %#x of %s", x.mant[m-1], x.Format('p', 0)))
- }
- if x.prec <= 0 {
- panic(fmt.Sprintf("invalid precision %d", x.prec))
}
}
// z.prec > 0
if debugFloat {
- z.validate()
+ validate(z)
}
bits := m * _W // available mantissa bits
}
if debugFloat {
- z.validate()
+ validate(z)
}
return
// for x > math.MaxUint64.
func (x *Float) Uint64() (uint64, Accuracy) {
if debugFloat {
- x.validate()
+ validate(x)
}
switch x.ord() {
case -2, -1:
// (math.MaxInt64, Below) for x > math.MaxInt64.
func (x *Float) Int64() (int64, Accuracy) {
if debugFloat {
- x.validate()
+ validate(x)
}
switch x.ord() {
// otherwise it is Below for x > 0, and Above for x < 0.
func (x *Float) Int() (res *Int, acc Accuracy) {
if debugFloat {
- x.validate()
+ validate(x)
}
// accuracy for inexact results
acc = Below // truncation
// result error relative to the exact (not rounded)
// result.
func (z *Float) Add(x, y *Float) *Float {
+ if debugFloat {
+ validate(x, y)
+ }
+
if z.prec == 0 {
z.prec = umax(x.prec, y.prec)
}
// Sub sets z to the rounded difference x-y and returns z.
// Precision, rounding, and accuracy reporting are as for Add.
func (z *Float) Sub(x, y *Float) *Float {
+ if debugFloat {
+ validate(x, y)
+ }
+
if z.prec == 0 {
z.prec = umax(x.prec, y.prec)
}
// Mul sets z to the rounded product x*y and returns z.
// Precision, rounding, and accuracy reporting are as for Add.
func (z *Float) Mul(x, y *Float) *Float {
+ if debugFloat {
+ validate(x, y)
+ }
+
if z.prec == 0 {
z.prec = umax(x.prec, y.prec)
}
// Quo sets z to the rounded quotient x/y and returns z.
// Precision, rounding, and accuracy reporting are as for Add.
func (z *Float) Quo(x, y *Float) *Float {
+ if debugFloat {
+ validate(x, y)
+ }
+
if z.prec == 0 {
z.prec = umax(x.prec, y.prec)
}
// and rounding mode; and z's accuracy reports the
// result error relative to the exact (not rounded)
// result.
-func (z *Float) Lsh(x *Float, s uint, mode RoundingMode) *Float {
+// BUG(gri) Lsh is not tested and may not work correctly.
+func (z *Float) Lsh(x *Float, s uint) *Float {
+ if debugFloat {
+ validate(x)
+ }
+
if z.prec == 0 {
z.prec = x.prec
}
// TODO(gri) handle Inf
- z.Round(x, z.prec, mode)
+ z.round(0)
z.setExp(int64(z.exp) + int64(s))
return z
}
// Rsh sets z to the rounded x / (1<<s) and returns z.
// Precision, rounding, and accuracy reporting are as for Lsh.
-func (z *Float) Rsh(x *Float, s uint, mode RoundingMode) *Float {
+// BUG(gri) Rsh is not tested and may not work correctly.
+func (z *Float) Rsh(x *Float, s uint) *Float {
+ if debugFloat {
+ validate(x)
+ }
+
if z.prec == 0 {
z.prec = x.prec
}
// TODO(gri) handle Inf
- z.Round(x, z.prec, mode)
+ z.round(0)
z.setExp(int64(z.exp) - int64(s))
return z
}
// Infinities with matching sign are equal.
func (x *Float) Cmp(y *Float) int {
if debugFloat {
- x.validate()
- y.validate()
+ validate(x, y)
}
mx := x.ord()
}
}
+func TestFloatInc(t *testing.T) {
+ var x, one Float
+ // x.prec = 256 TODO(gri) This doesn't work at the moment
+ one.SetInt64(1)
+ for i := 0; i < 10; i++ {
+ x.Add(&x, &one)
+ }
+ if s := x.Format('g', 10); s != "10" {
+ t.Errorf("got %s; want 10", s)
+ }
+}
+
// Selected precisions with which to run various tests.
var precList = [...]uint{1, 2, 5, 8, 10, 16, 23, 24, 32, 50, 53, 64, 100, 128, 500, 511, 512, 513, 1000, 10000}