return z
}
+type cmpResult struct {
+ acc Accuracy
+}
+
// Cmp compares x and y and returns:
//
// Below if x < y
// Above if x > y
// Undef if any of x, y is NaN
//
-func (x *Float) Cmp(y *Float) Accuracy {
+func (x *Float) Cmp(y *Float) cmpResult {
if debugFloat {
x.validate()
y.validate()
}
if x.form == nan || y.form == nan {
- return Undef
+ return cmpResult{Undef}
}
mx := x.ord()
my := y.ord()
switch {
case mx < my:
- return Below
+ return cmpResult{Below}
case mx > my:
- return Above
+ return cmpResult{Above}
}
// mx == my
// only if |mx| == 1 we have to compare the mantissae
switch mx {
case -1:
- return y.ucmp(x)
+ return cmpResult{y.ucmp(x)}
case +1:
- return x.ucmp(y)
+ return cmpResult{x.ucmp(y)}
}
- return Exact
+ return cmpResult{Exact}
}
// The following accessors simplify testing of Cmp results.
-func (acc Accuracy) Eql() bool { return acc == Exact }
-func (acc Accuracy) Neq() bool { return acc != Exact }
-func (acc Accuracy) Lss() bool { return acc == Below }
-func (acc Accuracy) Leq() bool { return acc&Above == 0 }
-func (acc Accuracy) Gtr() bool { return acc == Above }
-func (acc Accuracy) Geq() bool { return acc&Below == 0 }
+func (res cmpResult) Acc() Accuracy { return res.acc }
+func (res cmpResult) Eql() bool { return res.acc == Exact }
+func (res cmpResult) Neq() bool { return res.acc != Exact }
+func (res cmpResult) Lss() bool { return res.acc == Below }
+func (res cmpResult) Leq() bool { return res.acc&Above == 0 }
+func (res cmpResult) Gtr() bool { return res.acc == Above }
+func (res cmpResult) Geq() bool { return res.acc&Below == 0 }
// ord classifies x and returns:
//
if x.IsNaN() || y.IsNaN() {
return x.IsNaN() && y.IsNaN()
}
- return x.Cmp(y) == 0 && x.IsNeg() == y.IsNeg()
+ return x.Cmp(y).Eql() && x.IsNeg() == y.IsNeg()
}
func TestFloatMantExp(t *testing.T) {
// inverse conversion
if res != nil {
got := new(Float).SetPrec(64).SetRat(res)
- if got.Cmp(x) != 0 {
+ if got.Cmp(x).Neq() {
t.Errorf("%s: got %s; want %s", test.x, got, x)
}
}
for i := 0; i < n; i++ {
x.Add(&x, &one)
}
- if x.Cmp(new(Float).SetInt64(n)) != 0 {
+ if x.Cmp(new(Float).SetInt64(n)).Neq() {
t.Errorf("prec = %d: got %s; want %d", prec, &x, n)
}
}
got := new(Float).SetPrec(prec).SetMode(mode)
got.Add(x, y)
want := zbits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t+ %s %v\n\t= %s\n\twant %s",
i, prec, mode, x, xbits, y, ybits, got, want)
}
got.Sub(z, x)
want = ybits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t- %s %v\n\t= %s\n\twant %s",
i, prec, mode, z, zbits, x, xbits, got, want)
}
got := new(Float).SetPrec(prec).SetMode(mode)
got.Mul(x, y)
want := zbits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t* %s %v\n\t= %s\n\twant %s",
i, prec, mode, x, xbits, y, ybits, got, want)
}
}
got.Quo(z, x)
want = ybits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t/ %s %v\n\t= %s\n\twant %s",
i, prec, mode, z, zbits, x, xbits, got, want)
}
p.Mul(p, psix)
z2.Sub(two, p)
- if z1.Cmp(z2) != 0 {
+ if z1.Cmp(z2).Neq() {
t.Fatalf("prec %d: got z1 = %s != z2 = %s; want z1 == z2\n", prec, z1, z2)
}
if z1.Sign() != 0 {
prec := uint(preci + d)
got := new(Float).SetPrec(prec).SetMode(mode).Quo(x, y)
want := bits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s\n\t/ %s\n\t= %s\n\twant %s",
i, prec, mode, x, y, got, want)
}
}
for _, y := range args {
yy.SetFloat64(y)
- got := xx.Cmp(yy)
+ got := xx.Cmp(yy).Acc()
want := Undef
switch {
case x < y: