diff options
Diffstat (limited to 'libgo/go/big')
-rw-r--r-- | libgo/go/big/int.go | 31 | ||||
-rw-r--r-- | libgo/go/big/int_test.go | 11 | ||||
-rw-r--r-- | libgo/go/big/nat.go | 13 | ||||
-rw-r--r-- | libgo/go/big/nat_test.go | 20 | ||||
-rw-r--r-- | libgo/go/big/rat.go | 201 | ||||
-rw-r--r-- | libgo/go/big/rat_test.go | 124 |
6 files changed, 277 insertions, 123 deletions
diff --git a/libgo/go/big/int.go b/libgo/go/big/int.go index 9e1d1ae1318..b0dde1e6e37 100644 --- a/libgo/go/big/int.go +++ b/libgo/go/big/int.go @@ -58,22 +58,24 @@ func NewInt(x int64) *Int { // Set sets z to x and returns z. func (z *Int) Set(x *Int) *Int { - z.abs = z.abs.set(x.abs) - z.neg = x.neg + if z != x { + z.abs = z.abs.set(x.abs) + z.neg = x.neg + } return z } // Abs sets z to |x| (the absolute value of x) and returns z. func (z *Int) Abs(x *Int) *Int { - z.abs = z.abs.set(x.abs) + z.Set(x) z.neg = false return z } // Neg sets z to -x and returns z. func (z *Int) Neg(x *Int) *Int { - z.abs = z.abs.set(x.abs) - z.neg = len(z.abs) > 0 && !x.neg // 0 has no sign + z.Set(x) + z.neg = len(z.abs) > 0 && !z.neg // 0 has no sign return z } @@ -174,7 +176,7 @@ func (z *Int) Quo(x, y *Int) *Int { // If y == 0, a division-by-zero run-time panic occurs. // Rem implements truncated modulus (like Go); see QuoRem for more details. func (z *Int) Rem(x, y *Int) *Int { - _, z.abs = nat(nil).div(z.abs, x.abs, y.abs) + _, z.abs = nat{}.div(z.abs, x.abs, y.abs) z.neg = len(z.abs) > 0 && x.neg // 0 has no sign return z } @@ -422,8 +424,8 @@ func (x *Int) Format(s fmt.State, ch int) { // scan sets z to the integer value corresponding to the longest possible prefix // read from r representing a signed integer number in a given conversion base. // It returns z, the actual conversion base used, and an error, if any. In the -// error case, the value of z is undefined. The syntax follows the syntax of -// integer literals in Go. +// error case, the value of z is undefined but the returned value is nil. The +// syntax follows the syntax of integer literals in Go. // // The base argument must be 0 or a value from 2 through MaxBase. If the base // is 0, the string prefix determines the actual conversion base. A prefix of @@ -434,7 +436,7 @@ func (z *Int) scan(r io.RuneScanner, base int) (*Int, int, os.Error) { // determine sign ch, _, err := r.ReadRune() if err != nil { - return z, 0, err + return nil, 0, err } neg := false switch ch { @@ -448,7 +450,7 @@ func (z *Int) scan(r io.RuneScanner, base int) (*Int, int, os.Error) { // determine mantissa z.abs, base, err = z.abs.scan(r, base) if err != nil { - return z, base, err + return nil, base, err } z.neg = len(z.abs) > 0 && neg // 0 has no sign @@ -497,7 +499,7 @@ func (x *Int) Int64() int64 { // SetString sets z to the value of s, interpreted in the given base, // and returns z and a boolean indicating success. If SetString fails, -// the value of z is undefined. +// the value of z is undefined but the returned value is nil. // // The base argument must be 0 or a value from 2 through MaxBase. If the base // is 0, the string prefix determines the actual conversion base. A prefix of @@ -508,10 +510,13 @@ func (z *Int) SetString(s string, base int) (*Int, bool) { r := strings.NewReader(s) _, _, err := z.scan(r, base) if err != nil { - return z, false + return nil, false } _, _, err = r.ReadRune() - return z, err == os.EOF // err == os.EOF => scan consumed all of s + if err != os.EOF { + return nil, false + } + return z, true // err == os.EOF => scan consumed all of s } // SetBytes interprets buf as the bytes of a big-endian unsigned diff --git a/libgo/go/big/int_test.go b/libgo/go/big/int_test.go index b2e16921799..fde19c23b72 100644 --- a/libgo/go/big/int_test.go +++ b/libgo/go/big/int_test.go @@ -311,7 +311,16 @@ func TestSetString(t *testing.T) { t.Errorf("#%d (input '%s') ok incorrect (should be %t)", i, test.in, test.ok) continue } - if !ok1 || !ok2 { + if !ok1 { + if n1 != nil { + t.Errorf("#%d (input '%s') n1 != nil", i, test.in) + } + continue + } + if !ok2 { + if n2 != nil { + t.Errorf("#%d (input '%s') n2 != nil", i, test.in) + } continue } diff --git a/libgo/go/big/nat.go b/libgo/go/big/nat.go index 33d6bb16ffd..c0769d88a91 100644 --- a/libgo/go/big/nat.go +++ b/libgo/go/big/nat.go @@ -35,7 +35,7 @@ import ( // During arithmetic operations, denormalized values may occur but are // always normalized before returning the final result. The normalized // representation of 0 is the empty or nil slice (length = 0). - +// type nat []Word var ( @@ -447,10 +447,10 @@ func (z nat) mulRange(a, b uint64) nat { case a == b: return z.setUint64(a) case a+1 == b: - return z.mul(nat(nil).setUint64(a), nat(nil).setUint64(b)) + return z.mul(nat{}.setUint64(a), nat{}.setUint64(b)) } m := (a + b) / 2 - return z.mul(nat(nil).mulRange(a, m), nat(nil).mulRange(m+1, b)) + return z.mul(nat{}.mulRange(a, m), nat{}.mulRange(m+1, b)) } // q = (x-r)/y, with 0 <= r < y @@ -589,7 +589,6 @@ func (x nat) bitLen() int { // MaxBase is the largest number base accepted for string conversions. const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1 - func hexValue(ch int) Word { d := MaxBase + 1 // illegal base switch { @@ -786,7 +785,7 @@ func (x nat) string(charset string) string { } // preserve x, create local copy for use in repeated divisions - q := nat(nil).set(x) + q := nat{}.set(x) var r Word // convert @@ -1192,11 +1191,11 @@ func (n nat) probablyPrime(reps int) bool { return false } - nm1 := nat(nil).sub(n, natOne) + nm1 := nat{}.sub(n, natOne) // 1<<k * q = nm1; q, k := nm1.powersOfTwoDecompose() - nm3 := nat(nil).sub(nm1, natTwo) + nm3 := nat{}.sub(nm1, natTwo) rand := rand.New(rand.NewSource(int64(n[0]))) var x, y, quotient nat diff --git a/libgo/go/big/nat_test.go b/libgo/go/big/nat_test.go index 71d0860878c..4f5732824c5 100644 --- a/libgo/go/big/nat_test.go +++ b/libgo/go/big/nat_test.go @@ -67,7 +67,7 @@ var prodNN = []argNN{ func TestSet(t *testing.T) { for _, a := range sumNN { - z := nat(nil).set(a.z) + z := nat{}.set(a.z) if z.cmp(a.z) != 0 { t.Errorf("got z = %v; want %v", z, a.z) } @@ -129,7 +129,7 @@ var mulRangesN = []struct { func TestMulRangeN(t *testing.T) { for i, r := range mulRangesN { - prod := nat(nil).mulRange(r.a, r.b).decimalString() + prod := nat{}.mulRange(r.a, r.b).decimalString() if prod != r.prod { t.Errorf("#%d: got %s; want %s", i, prod, r.prod) } @@ -175,7 +175,7 @@ func toString(x nat, charset string) string { s := make([]byte, i) // don't destroy x - q := nat(nil).set(x) + q := nat{}.set(x) // convert for len(q) > 0 { @@ -212,7 +212,7 @@ func TestString(t *testing.T) { t.Errorf("string%+v\n\tgot s = %s; want %s", a, s, a.s) } - x, b, err := nat(nil).scan(strings.NewReader(a.s), len(a.c)) + x, b, err := nat{}.scan(strings.NewReader(a.s), len(a.c)) if x.cmp(a.x) != 0 { t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x) } @@ -271,7 +271,7 @@ var natScanTests = []struct { func TestScanBase(t *testing.T) { for _, a := range natScanTests { r := strings.NewReader(a.s) - x, b, err := nat(nil).scan(r, a.base) + x, b, err := nat{}.scan(r, a.base) if err == nil && !a.ok { t.Errorf("scan%+v\n\texpected error", a) } @@ -651,17 +651,17 @@ var expNNTests = []struct { func TestExpNN(t *testing.T) { for i, test := range expNNTests { - x, _, _ := nat(nil).scan(strings.NewReader(test.x), 0) - y, _, _ := nat(nil).scan(strings.NewReader(test.y), 0) - out, _, _ := nat(nil).scan(strings.NewReader(test.out), 0) + x, _, _ := nat{}.scan(strings.NewReader(test.x), 0) + y, _, _ := nat{}.scan(strings.NewReader(test.y), 0) + out, _, _ := nat{}.scan(strings.NewReader(test.out), 0) var m nat if len(test.m) > 0 { - m, _, _ = nat(nil).scan(strings.NewReader(test.m), 0) + m, _, _ = nat{}.scan(strings.NewReader(test.m), 0) } - z := nat(nil).expNN(x, y, m) + z := nat{}.expNN(x, y, m) if z.cmp(out) != 0 { t.Errorf("#%d got %v want %v", i, z, out) } diff --git a/libgo/go/big/rat.go b/libgo/go/big/rat.go index f435e637f19..6b860627206 100644 --- a/libgo/go/big/rat.go +++ b/libgo/go/big/rat.go @@ -13,11 +13,11 @@ import ( "strings" ) -// A Rat represents a quotient a/b of arbitrary precision. The zero value for -// a Rat, 0/0, is not a legal Rat. +// A Rat represents a quotient a/b of arbitrary precision. +// The zero value for a Rat represents the value 0. type Rat struct { a Int - b nat + b nat // len(b) == 0 acts like b == 1 } // NewRat creates a new Rat with numerator a and denominator b. @@ -29,8 +29,11 @@ func NewRat(a, b int64) *Rat { func (z *Rat) SetFrac(a, b *Int) *Rat { z.a.neg = a.neg != b.neg babs := b.abs + if len(babs) == 0 { + panic("division by zero") + } if &z.a == b || alias(z.a.abs, babs) { - babs = nat(nil).set(babs) // make a copy + babs = nat{}.set(babs) // make a copy } z.a.abs = z.a.abs.set(a.abs) z.b = z.b.set(babs) @@ -40,6 +43,9 @@ func (z *Rat) SetFrac(a, b *Int) *Rat { // SetFrac64 sets z to a/b and returns z. func (z *Rat) SetFrac64(a, b int64) *Rat { z.a.SetInt64(a) + if b == 0 { + panic("division by zero") + } if b < 0 { b = -b z.a.neg = !z.a.neg @@ -51,14 +57,55 @@ func (z *Rat) SetFrac64(a, b int64) *Rat { // SetInt sets z to x (by making a copy of x) and returns z. func (z *Rat) SetInt(x *Int) *Rat { z.a.Set(x) - z.b = z.b.setWord(1) + z.b = z.b.make(0) return z } // SetInt64 sets z to x and returns z. func (z *Rat) SetInt64(x int64) *Rat { z.a.SetInt64(x) - z.b = z.b.setWord(1) + z.b = z.b.make(0) + return z +} + +// Set sets z to x (by making a copy of x) and returns z. +func (z *Rat) Set(x *Rat) *Rat { + if z != x { + z.a.Set(&x.a) + z.b = z.b.set(x.b) + } + return z +} + +// Abs sets z to |x| (the absolute value of x) and returns z. +func (z *Rat) Abs(x *Rat) *Rat { + z.Set(x) + z.a.neg = false + return z +} + +// Neg sets z to -x and returns z. +func (z *Rat) Neg(x *Rat) *Rat { + z.Set(x) + z.a.neg = len(z.a.abs) > 0 && !z.a.neg // 0 has no sign + return z +} + +// Inv sets z to 1/x and returns z. +func (z *Rat) Inv(x *Rat) *Rat { + if len(x.a.abs) == 0 { + panic("division by zero") + } + z.Set(x) + a := z.b + if len(a) == 0 { + a = a.setWord(1) // materialize numerator + } + b := z.a.abs + if b.cmp(natOne) == 0 { + b = b.make(0) // normalize denominator + } + z.a.abs, z.b = a, b // sign doesn't change return z } @@ -74,21 +121,24 @@ func (x *Rat) Sign() int { // IsInt returns true if the denominator of x is 1. func (x *Rat) IsInt() bool { - return len(x.b) == 1 && x.b[0] == 1 + return len(x.b) == 0 || x.b.cmp(natOne) == 0 } -// Num returns the numerator of z; it may be <= 0. -// The result is a reference to z's numerator; it -// may change if a new value is assigned to z. -func (z *Rat) Num() *Int { - return &z.a +// Num returns the numerator of x; it may be <= 0. +// The result is a reference to x's numerator; it +// may change if a new value is assigned to x. +func (x *Rat) Num() *Int { + return &x.a } -// Denom returns the denominator of z; it is always > 0. -// The result is a reference to z's denominator; it -// may change if a new value is assigned to z. -func (z *Rat) Denom() *Int { - return &Int{false, z.b} +// Denom returns the denominator of x; it is always > 0. +// The result is a reference to x's denominator; it +// may change if a new value is assigned to x. +func (x *Rat) Denom() *Int { + if len(x.b) == 0 { + return &Int{abs: nat{1}} + } + return &Int{abs: x.b} } func gcd(x, y nat) nat { @@ -106,24 +156,47 @@ func gcd(x, y nat) nat { } func (z *Rat) norm() *Rat { - f := gcd(z.a.abs, z.b) - if len(z.a.abs) == 0 { - // z == 0 - z.a.neg = false // normalize sign - z.b = z.b.setWord(1) - return z - } - if f.cmp(natOne) != 0 { - z.a.abs, _ = z.a.abs.div(nil, z.a.abs, f) - z.b, _ = z.b.div(nil, z.b, f) + switch { + case len(z.a.abs) == 0: + // z == 0 - normalize sign and denominator + z.a.neg = false + z.b = z.b.make(0) + case len(z.b) == 0: + // z is normalized int - nothing to do + case z.b.cmp(natOne) == 0: + // z is int - normalize denominator + z.b = z.b.make(0) + default: + if f := gcd(z.a.abs, z.b); f.cmp(natOne) != 0 { + z.a.abs, _ = z.a.abs.div(nil, z.a.abs, f) + z.b, _ = z.b.div(nil, z.b, f) + } } return z } -func mulNat(x *Int, y nat) *Int { +// mulDenom sets z to the denominator product x*y (by taking into +// account that 0 values for x or y must be interpreted as 1) and +// returns z. +func mulDenom(z, x, y nat) nat { + switch { + case len(x) == 0: + return z.set(y) + case len(y) == 0: + return z.set(x) + } + return z.mul(x, y) +} + +// scaleDenom computes x*f. +// If f == 0 (zero value of denominator), the result is (a copy of) x. +func scaleDenom(x *Int, f nat) *Int { var z Int - z.abs = z.abs.mul(x.abs, y) - z.neg = len(z.abs) > 0 && x.neg + if len(f) == 0 { + return z.Set(x) + } + z.abs = z.abs.mul(x.abs, f) + z.neg = x.neg return &z } @@ -133,39 +206,32 @@ func mulNat(x *Int, y nat) *Int { // 0 if x == y // +1 if x > y // -func (x *Rat) Cmp(y *Rat) (r int) { - return mulNat(&x.a, y.b).Cmp(mulNat(&y.a, x.b)) -} - -// Abs sets z to |x| (the absolute value of x) and returns z. -func (z *Rat) Abs(x *Rat) *Rat { - z.a.Abs(&x.a) - z.b = z.b.set(x.b) - return z +func (x *Rat) Cmp(y *Rat) int { + return scaleDenom(&x.a, y.b).Cmp(scaleDenom(&y.a, x.b)) } // Add sets z to the sum x+y and returns z. func (z *Rat) Add(x, y *Rat) *Rat { - a1 := mulNat(&x.a, y.b) - a2 := mulNat(&y.a, x.b) + a1 := scaleDenom(&x.a, y.b) + a2 := scaleDenom(&y.a, x.b) z.a.Add(a1, a2) - z.b = z.b.mul(x.b, y.b) + z.b = mulDenom(z.b, x.b, y.b) return z.norm() } // Sub sets z to the difference x-y and returns z. func (z *Rat) Sub(x, y *Rat) *Rat { - a1 := mulNat(&x.a, y.b) - a2 := mulNat(&y.a, x.b) + a1 := scaleDenom(&x.a, y.b) + a2 := scaleDenom(&y.a, x.b) z.a.Sub(a1, a2) - z.b = z.b.mul(x.b, y.b) + z.b = mulDenom(z.b, x.b, y.b) return z.norm() } // Mul sets z to the product x*y and returns z. func (z *Rat) Mul(x, y *Rat) *Rat { z.a.Mul(&x.a, &y.a) - z.b = z.b.mul(x.b, y.b) + z.b = mulDenom(z.b, x.b, y.b) return z.norm() } @@ -175,28 +241,14 @@ func (z *Rat) Quo(x, y *Rat) *Rat { if len(y.a.abs) == 0 { panic("division by zero") } - a := mulNat(&x.a, y.b) - b := mulNat(&y.a, x.b) + a := scaleDenom(&x.a, y.b) + b := scaleDenom(&y.a, x.b) z.a.abs = a.abs z.b = b.abs z.a.neg = a.neg != b.neg return z.norm() } -// Neg sets z to -x (by making a copy of x if necessary) and returns z. -func (z *Rat) Neg(x *Rat) *Rat { - z.a.Neg(&x.a) - z.b = z.b.set(x.b) - return z -} - -// Set sets z to x (by making a copy of x if necessary) and returns z. -func (z *Rat) Set(x *Rat) *Rat { - z.a.Set(&x.a) - z.b = z.b.set(x.b) - return z -} - func ratTok(ch int) bool { return strings.IndexRune("+-/0123456789.eE", ch) >= 0 } @@ -219,23 +271,23 @@ func (z *Rat) Scan(s fmt.ScanState, ch int) os.Error { // SetString sets z to the value of s and returns z and a boolean indicating // success. s can be given as a fraction "a/b" or as a floating-point number -// optionally followed by an exponent. If the operation failed, the value of z -// is undefined. +// optionally followed by an exponent. If the operation failed, the value of +// z is undefined but the returned value is nil. func (z *Rat) SetString(s string) (*Rat, bool) { if len(s) == 0 { - return z, false + return nil, false } // check for a quotient sep := strings.Index(s, "/") if sep >= 0 { if _, ok := z.a.SetString(s[0:sep], 10); !ok { - return z, false + return nil, false } s = s[sep+1:] var err os.Error if z.b, _, err = z.b.scan(strings.NewReader(s), 10); err != nil { - return z, false + return nil, false } return z.norm(), true } @@ -248,10 +300,10 @@ func (z *Rat) SetString(s string) (*Rat, bool) { if e >= 0 { if e < sep { // The E must come after the decimal point. - return z, false + return nil, false } if _, ok := exp.SetString(s[e+1:], 10); !ok { - return z, false + return nil, false } s = s[0:e] } @@ -261,7 +313,7 @@ func (z *Rat) SetString(s string) (*Rat, bool) { } if _, ok := z.a.SetString(s, 10); !ok { - return z, false + return nil, false } powTen := nat{}.expNN(natTen, exp.abs, nil) if exp.neg { @@ -269,7 +321,7 @@ func (z *Rat) SetString(s string) (*Rat, bool) { z.norm() } else { z.a.abs = z.a.abs.mul(z.a.abs, powTen) - z.b = z.b.setWord(1) + z.b = z.b.make(0) } return z, true @@ -277,7 +329,11 @@ func (z *Rat) SetString(s string) (*Rat, bool) { // String returns a string representation of z in the form "a/b" (even if b == 1). func (z *Rat) String() string { - return z.a.String() + "/" + z.b.decimalString() + s := "/1" + if len(z.b) != 0 { + s = "/" + z.b.decimalString() + } + return z.a.String() + s } // RatString returns a string representation of z in the form "a/b" if b != 1, @@ -299,6 +355,7 @@ func (z *Rat) FloatString(prec int) string { } return s } + // z.b != 0 q, r := nat{}.div(nat{}, z.a.abs, z.b) diff --git a/libgo/go/big/rat_test.go b/libgo/go/big/rat_test.go index a2b905525ee..a95e5fea3a5 100644 --- a/libgo/go/big/rat_test.go +++ b/libgo/go/big/rat_test.go @@ -11,6 +11,46 @@ import ( "testing" ) +func TestZeroRat(t *testing.T) { + var x, y, z Rat + y.SetFrac64(0, 42) + + if x.Cmp(&y) != 0 { + t.Errorf("x and y should be both equal and zero") + } + + if s := x.String(); s != "0/1" { + t.Errorf("got x = %s, want 0/1", s) + } + + if s := x.RatString(); s != "0" { + t.Errorf("got x = %s, want 0", s) + } + + z.Add(&x, &y) + if s := z.RatString(); s != "0" { + t.Errorf("got x+y = %s, want 0", s) + } + + z.Sub(&x, &y) + if s := z.RatString(); s != "0" { + t.Errorf("got x-y = %s, want 0", s) + } + + z.Mul(&x, &y) + if s := z.RatString(); s != "0" { + t.Errorf("got x*y = %s, want 0", s) + } + + // check for division by zero + defer func() { + if s := recover(); s == nil || s.(string) != "division by zero" { + panic(s) + } + }() + z.Quo(&x, &y) +} + var setStringTests = []struct { in, out string ok bool @@ -50,8 +90,14 @@ func TestRatSetString(t *testing.T) { for i, test := range setStringTests { x, ok := new(Rat).SetString(test.in) - if ok != test.ok || ok && x.RatString() != test.out { - t.Errorf("#%d got %s want %s", i, x.RatString(), test.out) + if ok { + if !test.ok { + t.Errorf("#%d SetString(%q) expected failure", i, test.in) + } else if x.RatString() != test.out { + t.Errorf("#%d SetString(%q) got %s want %s", i, test.in, x.RatString(), test.out) + } + } else if x != nil { + t.Errorf("#%d SetString(%q) got %p want nil", i, test.in, x) } } } @@ -113,8 +159,10 @@ func TestFloatString(t *testing.T) { func TestRatSign(t *testing.T) { zero := NewRat(0, 1) for _, a := range setStringTests { - var x Rat - x.SetString(a.in) + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } s := x.Sign() e := x.Cmp(zero) if s != e { @@ -153,29 +201,65 @@ func TestRatCmp(t *testing.T) { func TestIsInt(t *testing.T) { one := NewInt(1) for _, a := range setStringTests { - var x Rat - x.SetString(a.in) + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } i := x.IsInt() e := x.Denom().Cmp(one) == 0 if i != e { - t.Errorf("got %v; want %v for z = %v", i, e, &x) + t.Errorf("got IsInt(%v) == %v; want %v", x, i, e) } } } func TestRatAbs(t *testing.T) { - zero := NewRat(0, 1) + zero := new(Rat) for _, a := range setStringTests { - var z Rat - z.SetString(a.in) - var e Rat - e.Set(&z) + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } + e := new(Rat).Set(x) if e.Cmp(zero) < 0 { - e.Sub(zero, &e) + e.Sub(zero, e) + } + z := new(Rat).Abs(x) + if z.Cmp(e) != 0 { + t.Errorf("got Abs(%v) = %v; want %v", x, z, e) + } + } +} + +func TestRatNeg(t *testing.T) { + zero := new(Rat) + for _, a := range setStringTests { + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } + e := new(Rat).Sub(zero, x) + z := new(Rat).Neg(x) + if z.Cmp(e) != 0 { + t.Errorf("got Neg(%v) = %v; want %v", x, z, e) + } + } +} + +func TestRatInv(t *testing.T) { + zero := new(Rat) + for _, a := range setStringTests { + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } + if x.Cmp(zero) == 0 { + continue // avoid division by zero } - z.Abs(&z) - if z.Cmp(&e) != 0 { - t.Errorf("got z = %v; want %v", &z, &e) + e := new(Rat).SetFrac(x.Denom(), x.Num()) + z := new(Rat).Inv(x) + if z.Cmp(e) != 0 { + t.Errorf("got Inv(%v) = %v; want %v", x, z, e) } } } @@ -186,10 +270,10 @@ type ratBinArg struct { } func testRatBin(t *testing.T, i int, name string, f ratBinFun, a ratBinArg) { - x, _ := NewRat(0, 1).SetString(a.x) - y, _ := NewRat(0, 1).SetString(a.y) - z, _ := NewRat(0, 1).SetString(a.z) - out := f(NewRat(0, 1), x, y) + x, _ := new(Rat).SetString(a.x) + y, _ := new(Rat).SetString(a.y) + z, _ := new(Rat).SetString(a.z) + out := f(new(Rat), x, y) if out.Cmp(z) != 0 { t.Errorf("%s #%d got %s want %s", name, i, out, z) |