diff options
Diffstat (limited to 'src/pkg/bignum')
-rw-r--r-- | src/pkg/bignum/arith.go | 72 | ||||
-rw-r--r-- | src/pkg/bignum/bignum.go | 486 | ||||
-rw-r--r-- | src/pkg/bignum/bignum_test.go | 616 | ||||
-rw-r--r-- | src/pkg/bignum/integer.go | 150 | ||||
-rw-r--r-- | src/pkg/bignum/nrdiv_test.go | 100 | ||||
-rw-r--r-- | src/pkg/bignum/rational.go | 82 |
6 files changed, 753 insertions, 753 deletions
diff --git a/src/pkg/bignum/arith.go b/src/pkg/bignum/arith.go index 243e34b9c..aa65dbd7a 100644 --- a/src/pkg/bignum/arith.go +++ b/src/pkg/bignum/arith.go @@ -18,10 +18,10 @@ func Mul128(x, y uint64) (z1, z0 uint64) { // and return the product as 2 words. const ( - W = uint(unsafe.Sizeof(x)) * 8; - W2 = W / 2; - B2 = 1 << W2; - M2 = B2 - 1; + W = uint(unsafe.Sizeof(x)) * 8 + W2 = W / 2 + B2 = 1 << W2 + M2 = B2 - 1 ) if x < y { @@ -32,44 +32,44 @@ func Mul128(x, y uint64) (z1, z0 uint64) { // y < B2 because y <= x // sub-digits of x and y are (0, x) and (0, y) // z = z[0] = x*y - z0 = x * y; - return; + z0 = x * y + return } if y < B2 { // sub-digits of x and y are (x1, x0) and (0, y) // x = (x1*B2 + x0) // y = (y1*B2 + y0) - x1, x0 := x>>W2, x&M2; + x1, x0 := x>>W2, x&M2 // x*y = t2*B2*B2 + t1*B2 + t0 - t0 := x0 * y; - t1 := x1 * y; + t0 := x0 * y + t1 := x1 * y // compute result digits but avoid overflow // z = z[1]*B + z[0] = x*y - z0 = t1<<W2 + t0; - z1 = (t1 + t0>>W2) >> W2; - return; + z0 = t1<<W2 + t0 + z1 = (t1 + t0>>W2) >> W2 + return } // general case // sub-digits of x and y are (x1, x0) and (y1, y0) // x = (x1*B2 + x0) // y = (y1*B2 + y0) - x1, x0 := x>>W2, x&M2; - y1, y0 := y>>W2, y&M2; + x1, x0 := x>>W2, x&M2 + y1, y0 := y>>W2, y&M2 // x*y = t2*B2*B2 + t1*B2 + t0 - t0 := x0 * y0; - t1 := x1*y0 + x0*y1; - t2 := x1 * y1; + t0 := x0 * y0 + t1 := x1*y0 + x0*y1 + t2 := x1 * y1 // compute result digits but avoid overflow // z = z[1]*B + z[0] = x*y - z0 = t1<<W2 + t0; - z1 = t2 + (t1+t0>>W2)>>W2; - return; + z0 = t1<<W2 + t0 + z1 = t2 + (t1+t0>>W2)>>W2 + return } @@ -80,10 +80,10 @@ func MulAdd128(x, y, c uint64) (z1, z0 uint64) { // and return the product as 2 words. const ( - W = uint(unsafe.Sizeof(x)) * 8; - W2 = W / 2; - B2 = 1 << W2; - M2 = B2 - 1; + W = uint(unsafe.Sizeof(x)) * 8 + W2 = W / 2 + B2 = 1 << W2 + M2 = B2 - 1 ) // TODO(gri) Should implement special cases for faster execution. @@ -92,30 +92,30 @@ func MulAdd128(x, y, c uint64) (z1, z0 uint64) { // sub-digits of x, y, and c are (x1, x0), (y1, y0), (c1, c0) // x = (x1*B2 + x0) // y = (y1*B2 + y0) - x1, x0 := x>>W2, x&M2; - y1, y0 := y>>W2, y&M2; - c1, c0 := c>>W2, c&M2; + x1, x0 := x>>W2, x&M2 + y1, y0 := y>>W2, y&M2 + c1, c0 := c>>W2, c&M2 // x*y + c = t2*B2*B2 + t1*B2 + t0 - t0 := x0*y0 + c0; - t1 := x1*y0 + x0*y1 + c1; - t2 := x1 * y1; + t0 := x0*y0 + c0 + t1 := x1*y0 + x0*y1 + c1 + t2 := x1 * y1 // compute result digits but avoid overflow // z = z[1]*B + z[0] = x*y - z0 = t1<<W2 + t0; - z1 = t2 + (t1+t0>>W2)>>W2; - return; + z0 = t1<<W2 + t0 + z1 = t2 + (t1+t0>>W2)>>W2 + return } // q = (x1<<64 + x0)/y + r func Div128(x1, x0, y uint64) (q, r uint64) { if x1 == 0 { - q, r = x0/y, x0%y; - return; + q, r = x0/y, x0%y + return } // TODO(gri) Implement general case. - panic("Div128 not implemented for x > 1<<64-1"); + panic("Div128 not implemented for x > 1<<64-1") } diff --git a/src/pkg/bignum/bignum.go b/src/pkg/bignum/bignum.go index 8106a2664..ee7d45ba6 100644 --- a/src/pkg/bignum/bignum.go +++ b/src/pkg/bignum/bignum.go @@ -16,7 +16,7 @@ package bignum import ( - "fmt"; + "fmt" ) // TODO(gri) Complete the set of in-place operations. @@ -60,25 +60,25 @@ import ( // in bits must be even. type ( - digit uint64; - digit2 uint32; // half-digits for division + digit uint64 + digit2 uint32 // half-digits for division ) const ( - logW = 64; // word width - logH = 4; // bits for a hex digit (= small number) - logB = logW - logH; // largest bit-width available + logW = 64 // word width + logH = 4 // bits for a hex digit (= small number) + logB = logW - logH // largest bit-width available // half-digits - _W2 = logB / 2; // width - _B2 = 1 << _W2; // base - _M2 = _B2 - 1; // mask + _W2 = logB / 2 // width + _B2 = 1 << _W2 // base + _M2 = _B2 - 1 // mask // full digits - _W = _W2 * 2; // width - _B = 1 << _W; // base - _M = _B - 1; // mask + _W = _W2 * 2 // width + _B = 1 << _W // base + _M = _B - 1 // mask ) @@ -92,7 +92,7 @@ func assert(p bool) { } -func isSmall(x digit) bool { return x < 1<<logH } +func isSmall(x digit) bool { return x < 1<<logH } // For debugging. Keep around. @@ -119,7 +119,7 @@ type Natural []digit // func Nat(x uint64) Natural { if x == 0 { - return nil // len == 0 + return nil // len == 0 } // single-digit values @@ -132,19 +132,19 @@ func Nat(x uint64) Natural { // compute number of digits required to represent x // (this is usually 1 or 2, but the algorithm works // for any base) - n := 0; + n := 0 for t := x; t > 0; t >>= _W { n++ } // split x into digits - z := make(Natural, n); + z := make(Natural, n) for i := 0; i < n; i++ { - z[i] = digit(x & _M); - x >>= _W; + z[i] = digit(x & _M) + x >>= _W } - return z; + return z } @@ -152,7 +152,7 @@ func Nat(x uint64) Natural { // func (x Natural) Value() uint64 { // single-digit values - n := len(x); + n := len(x) switch n { case 0: return 0 @@ -163,14 +163,14 @@ func (x Natural) Value() uint64 { // multi-digit values // (this is usually 1 or 2, but the algorithm works // for any base) - z := uint64(0); - s := uint(0); + z := uint64(0) + s := uint(0) for i := 0; i < n && s < 64; i++ { - z += uint64(x[i]) << s; - s += _W; + z += uint64(x[i]) << s + s += _W } - return z; + return z } @@ -178,17 +178,17 @@ func (x Natural) Value() uint64 { // IsEven returns true iff x is divisible by 2. // -func (x Natural) IsEven() bool { return len(x) == 0 || x[0]&1 == 0 } +func (x Natural) IsEven() bool { return len(x) == 0 || x[0]&1 == 0 } // IsOdd returns true iff x is not divisible by 2. // -func (x Natural) IsOdd() bool { return len(x) > 0 && x[0]&1 != 0 } +func (x Natural) IsOdd() bool { return len(x) > 0 && x[0]&1 != 0 } // IsZero returns true iff x == 0. // -func (x Natural) IsZero() bool { return len(x) == 0 } +func (x Natural) IsZero() bool { return len(x) == 0 } // Operations @@ -201,11 +201,11 @@ func (x Natural) IsZero() bool { return len(x) == 0 } // n, m len(x), len(y) func normalize(x Natural) Natural { - n := len(x); + n := len(x) for n > 0 && x[n-1] == 0 { n-- } - return x[0:n]; + return x[0:n] } @@ -214,14 +214,14 @@ func normalize(x Natural) Natural { // Natural is allocated. // func nalloc(z Natural, n int) Natural { - size := n; + size := n if size <= 0 { size = 4 } if size <= cap(z) { return z[0:n] } - return make(Natural, n, size); + return make(Natural, n, size) } @@ -229,40 +229,40 @@ func nalloc(z Natural, n int) Natural { // *zp may be x or y. // func Nadd(zp *Natural, x, y Natural) { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n < m { - Nadd(zp, y, x); - return; + Nadd(zp, y, x) + return } - z := nalloc(*zp, n+1); - c := digit(0); - i := 0; + z := nalloc(*zp, n+1) + c := digit(0) + i := 0 for i < m { - t := c + x[i] + y[i]; - c, z[i] = t>>_W, t&_M; - i++; + t := c + x[i] + y[i] + c, z[i] = t>>_W, t&_M + i++ } for i < n { - t := c + x[i]; - c, z[i] = t>>_W, t&_M; - i++; + t := c + x[i] + c, z[i] = t>>_W, t&_M + i++ } if c != 0 { - z[i] = c; - i++; + z[i] = c + i++ } - *zp = z[0:i]; + *zp = z[0:i] } // Add returns the sum z = x + y. // func (x Natural) Add(y Natural) Natural { - var z Natural; - Nadd(&z, x, y); - return z; + var z Natural + Nadd(&z, x, y) + return z } @@ -271,29 +271,29 @@ func (x Natural) Add(y Natural) Natural { // *zp may be x or y. // func Nsub(zp *Natural, x, y Natural) { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n < m { panic("underflow") } - z := nalloc(*zp, n); - c := digit(0); - i := 0; + z := nalloc(*zp, n) + c := digit(0) + i := 0 for i < m { - t := c + x[i] - y[i]; - c, z[i] = digit(int64(t)>>_W), t&_M; // requires arithmetic shift! - i++; + t := c + x[i] - y[i] + c, z[i] = digit(int64(t)>>_W), t&_M // requires arithmetic shift! + i++ } for i < n { - t := c + x[i]; - c, z[i] = digit(int64(t)>>_W), t&_M; // requires arithmetic shift! - i++; + t := c + x[i] + c, z[i] = digit(int64(t)>>_W), t&_M // requires arithmetic shift! + i++ } if int64(c) < 0 { panic("underflow") } - *zp = normalize(z); + *zp = normalize(z) } @@ -301,17 +301,17 @@ func Nsub(zp *Natural, x, y Natural) { // If x < y, an underflow run-time error occurs (use Cmp to test if x >= y). // func (x Natural) Sub(y Natural) Natural { - var z Natural; - Nsub(&z, x, y); - return z; + var z Natural + Nsub(&z, x, y) + return z } // Returns z1 = (x*y + c) div B, z0 = (x*y + c) mod B. // func muladd11(x, y, c digit) (digit, digit) { - z1, z0 := MulAdd128(uint64(x), uint64(y), uint64(c)); - return digit(z1<<(64-logB) | z0>>logB), digit(z0 & _M); + z1, z0 := MulAdd128(uint64(x), uint64(y), uint64(c)) + return digit(z1<<(64-logB) | z0>>logB), digit(z0 & _M) } @@ -319,7 +319,7 @@ func mul1(z, x Natural, y digit) (c digit) { for i := 0; i < len(x); i++ { c, z[i] = muladd11(x[i], y, c) } - return; + return } @@ -328,29 +328,29 @@ func mul1(z, x Natural, y digit) (c digit) { func Nscale(z *Natural, d uint64) { switch { case d == 0: - *z = Nat(0); - return; + *z = Nat(0) + return case d == 1: return case d >= _B: - *z = z.Mul1(d); - return; + *z = z.Mul1(d) + return } - c := mul1(*z, *z, digit(d)); + c := mul1(*z, *z, digit(d)) if c != 0 { - n := len(*z); + n := len(*z) if n >= cap(*z) { - zz := make(Natural, n+1); + zz := make(Natural, n+1) for i, d := range *z { zz[i] = d } - *z = zz; + *z = zz } else { *z = (*z)[0 : n+1] } - (*z)[n] = c; + (*z)[n] = c } } @@ -358,17 +358,17 @@ func Nscale(z *Natural, d uint64) { // Computes x = x*d + c for small d's. // func muladd1(x Natural, d, c digit) Natural { - assert(isSmall(d-1) && isSmall(c)); - n := len(x); - z := make(Natural, n+1); + assert(isSmall(d-1) && isSmall(c)) + n := len(x) + z := make(Natural, n+1) for i := 0; i < n; i++ { - t := c + x[i]*d; - c, z[i] = t>>_W, t&_M; + t := c + x[i]*d + c, z[i] = t>>_W, t&_M } - z[n] = c; + z[n] = c - return normalize(z); + return normalize(z) } @@ -386,18 +386,18 @@ func (x Natural) Mul1(d uint64) Natural { return x.Mul(Nat(d)) } - z := make(Natural, len(x)+1); - c := mul1(z, x, digit(d)); - z[len(x)] = c; - return normalize(z); + z := make(Natural, len(x)+1) + c := mul1(z, x, digit(d)) + z[len(x)] = c + return normalize(z) } // Mul returns the product x * y. // func (x Natural) Mul(y Natural) Natural { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n < m { return y.Mul(x) } @@ -410,19 +410,19 @@ func (x Natural) Mul(y Natural) Natural { return x.Mul1(uint64(y[0])) } - z := make(Natural, n+m); + z := make(Natural, n+m) for j := 0; j < m; j++ { - d := y[j]; + d := y[j] if d != 0 { - c := digit(0); + c := digit(0) for i := 0; i < n; i++ { c, z[i+j] = muladd11(x[i], d, z[i+j]+c) } - z[n+j] = c; + z[n+j] = c } } - return normalize(z); + return normalize(z) } @@ -432,57 +432,57 @@ func (x Natural) Mul(y Natural) Natural { // DivMod, and then pack the results again. func unpack(x Natural) []digit2 { - n := len(x); - z := make([]digit2, n*2+1); // add space for extra digit (used by DivMod) + n := len(x) + z := make([]digit2, n*2+1) // add space for extra digit (used by DivMod) for i := 0; i < n; i++ { - t := x[i]; - z[i*2] = digit2(t & _M2); - z[i*2+1] = digit2(t >> _W2 & _M2); + t := x[i] + z[i*2] = digit2(t & _M2) + z[i*2+1] = digit2(t >> _W2 & _M2) } // normalize result - k := 2 * n; + k := 2 * n for k > 0 && z[k-1] == 0 { k-- } - return z[0:k]; // trim leading 0's + return z[0:k] // trim leading 0's } func pack(x []digit2) Natural { - n := (len(x) + 1) / 2; - z := make(Natural, n); + n := (len(x) + 1) / 2 + z := make(Natural, n) if len(x)&1 == 1 { // handle odd len(x) - n--; - z[n] = digit(x[n*2]); + n-- + z[n] = digit(x[n*2]) } for i := 0; i < n; i++ { z[i] = digit(x[i*2+1])<<_W2 | digit(x[i*2]) } - return normalize(z); + return normalize(z) } func mul21(z, x []digit2, y digit2) digit2 { - c := digit(0); - f := digit(y); + c := digit(0) + f := digit(y) for i := 0; i < len(x); i++ { - t := c + digit(x[i])*f; - c, z[i] = t>>_W2, digit2(t&_M2); + t := c + digit(x[i])*f + c, z[i] = t>>_W2, digit2(t&_M2) } - return digit2(c); + return digit2(c) } func div21(z, x []digit2, y digit2) digit2 { - c := digit(0); - d := digit(y); + c := digit(0) + d := digit(y) for i := len(x) - 1; i >= 0; i-- { - t := c<<_W2 + digit(x[i]); - c, z[i] = t%d, digit2(t/d); + t := c<<_W2 + digit(x[i]) + c, z[i] = t%d, digit2(t/d) } - return digit2(c); + return digit2(c) } @@ -507,14 +507,14 @@ func div21(z, x []digit2, y digit2) digit2 { // 579-601. John Wiley & Sons, Ltd. func divmod(x, y []digit2) ([]digit2, []digit2) { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if m == 0 { panic("division by zero") } - assert(n+1 <= cap(x)); // space for one extra digit - x = x[0 : n+1]; - assert(x[n] == 0); + assert(n+1 <= cap(x)) // space for one extra digit + x = x[0 : n+1] + assert(x[n] == 0) if m == 1 { // division by single digit @@ -528,27 +528,27 @@ func divmod(x, y []digit2) ([]digit2, []digit2) { } else { // general case - assert(2 <= m && m <= n); + assert(2 <= m && m <= n) // normalize x and y // TODO Instead of multiplying, it would be sufficient to // shift y such that the normalization condition is // satisfied (as done in Hacker's Delight). - f := _B2 / (digit(y[m-1]) + 1); + f := _B2 / (digit(y[m-1]) + 1) if f != 1 { - mul21(x, x, digit2(f)); - mul21(y, y, digit2(f)); + mul21(x, x, digit2(f)) + mul21(y, y, digit2(f)) } - assert(_B2/2 <= y[m-1] && y[m-1] < _B2); // incorrect scaling + assert(_B2/2 <= y[m-1] && y[m-1] < _B2) // incorrect scaling - y1, y2 := digit(y[m-1]), digit(y[m-2]); + y1, y2 := digit(y[m-1]), digit(y[m-2]) for i := n - m; i >= 0; i-- { - k := i + m; + k := i + m // compute trial digit (Knuth) - var q digit; + var q digit { - x0, x1, x2 := digit(x[k]), digit(x[k-1]), digit(x[k-2]); + x0, x1, x2 := digit(x[k]), digit(x[k-1]), digit(x[k-2]) if x0 != y1 { q = (x0<<_W2 + x1) / y1 } else { @@ -560,36 +560,36 @@ func divmod(x, y []digit2) ([]digit2, []digit2) { } // subtract y*q - c := digit(0); + c := digit(0) for j := 0; j < m; j++ { - t := c + digit(x[i+j]) - digit(y[j])*q; - c, x[i+j] = digit(int64(t)>>_W2), digit2(t&_M2); // requires arithmetic shift! + t := c + digit(x[i+j]) - digit(y[j])*q + c, x[i+j] = digit(int64(t)>>_W2), digit2(t&_M2) // requires arithmetic shift! } // correct if trial digit was too large if c+digit(x[k]) != 0 { // add y - c := digit(0); + c := digit(0) for j := 0; j < m; j++ { - t := c + digit(x[i+j]) + digit(y[j]); - c, x[i+j] = t>>_W2, digit2(t&_M2); + t := c + digit(x[i+j]) + digit(y[j]) + c, x[i+j] = t>>_W2, digit2(t&_M2) } - assert(c+digit(x[k]) == 0); + assert(c+digit(x[k]) == 0) // correct trial digit - q--; + q-- } - x[k] = digit2(q); + x[k] = digit2(q) } // undo normalization for remainder if f != 1 { - c := div21(x[0:m], x[0:m], digit2(f)); - assert(c == 0); + c := div21(x[0:m], x[0:m], digit2(f)) + assert(c == 0) } } - return x[m : n+1], x[0:m]; + return x[m : n+1], x[0:m] } @@ -598,8 +598,8 @@ func divmod(x, y []digit2) ([]digit2, []digit2) { // If y == 0, a division-by-zero run-time error occurs. // func (x Natural) Div(y Natural) Natural { - q, _ := divmod(unpack(x), unpack(y)); - return pack(q); + q, _ := divmod(unpack(x), unpack(y)) + return pack(q) } @@ -608,8 +608,8 @@ func (x Natural) Div(y Natural) Natural { // If y == 0, a division-by-zero run-time error occurs. // func (x Natural) Mod(y Natural) Natural { - _, r := divmod(unpack(x), unpack(y)); - return pack(r); + _, r := divmod(unpack(x), unpack(y)) + return pack(r) } @@ -617,78 +617,78 @@ func (x Natural) Mod(y Natural) Natural { // If y == 0, a division-by-zero run-time error occurs. // func (x Natural) DivMod(y Natural) (Natural, Natural) { - q, r := divmod(unpack(x), unpack(y)); - return pack(q), pack(r); + q, r := divmod(unpack(x), unpack(y)) + return pack(q), pack(r) } func shl(z, x Natural, s uint) digit { - assert(s <= _W); - n := len(x); - c := digit(0); + assert(s <= _W) + n := len(x) + c := digit(0) for i := 0; i < n; i++ { c, z[i] = x[i]>>(_W-s), x[i]<<s&_M|c } - return c; + return c } // Shl implements ``shift left'' x << s. It returns x * 2^s. // func (x Natural) Shl(s uint) Natural { - n := uint(len(x)); - m := n + s/_W; - z := make(Natural, m+1); + n := uint(len(x)) + m := n + s/_W + z := make(Natural, m+1) - z[m] = shl(z[m-n:m], x, s%_W); + z[m] = shl(z[m-n:m], x, s%_W) - return normalize(z); + return normalize(z) } func shr(z, x Natural, s uint) digit { - assert(s <= _W); - n := len(x); - c := digit(0); + assert(s <= _W) + n := len(x) + c := digit(0) for i := n - 1; i >= 0; i-- { c, z[i] = x[i]<<(_W-s)&_M, x[i]>>s|c } - return c; + return c } // Shr implements ``shift right'' x >> s. It returns x / 2^s. // func (x Natural) Shr(s uint) Natural { - n := uint(len(x)); - m := n - s/_W; - if m > n { // check for underflow + n := uint(len(x)) + m := n - s/_W + if m > n { // check for underflow m = 0 } - z := make(Natural, m); + z := make(Natural, m) - shr(z, x[n-m:n], s%_W); + shr(z, x[n-m:n], s%_W) - return normalize(z); + return normalize(z) } // And returns the ``bitwise and'' x & y for the 2's-complement representation of x and y. // func (x Natural) And(y Natural) Natural { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n < m { return y.And(x) } - z := make(Natural, m); + z := make(Natural, m) for i := 0; i < m; i++ { z[i] = x[i] & y[i] } // upper bits are 0 - return normalize(z); + return normalize(z) } @@ -702,57 +702,57 @@ func copy(z, x Natural) { // AndNot returns the ``bitwise clear'' x &^ y for the 2's-complement representation of x and y. // func (x Natural) AndNot(y Natural) Natural { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n < m { m = n } - z := make(Natural, n); + z := make(Natural, n) for i := 0; i < m; i++ { z[i] = x[i] &^ y[i] } - copy(z[m:n], x[m:n]); + copy(z[m:n], x[m:n]) - return normalize(z); + return normalize(z) } // Or returns the ``bitwise or'' x | y for the 2's-complement representation of x and y. // func (x Natural) Or(y Natural) Natural { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n < m { return y.Or(x) } - z := make(Natural, n); + z := make(Natural, n) for i := 0; i < m; i++ { z[i] = x[i] | y[i] } - copy(z[m:n], x[m:n]); + copy(z[m:n], x[m:n]) - return z; + return z } // Xor returns the ``bitwise exclusive or'' x ^ y for the 2's-complement representation of x and y. // func (x Natural) Xor(y Natural) Natural { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n < m { return y.Xor(x) } - z := make(Natural, n); + z := make(Natural, n) for i := 0; i < m; i++ { z[i] = x[i] ^ y[i] } - copy(z[m:n], x[m:n]); + copy(z[m:n], x[m:n]) - return normalize(z); + return normalize(z) } @@ -763,19 +763,19 @@ func (x Natural) Xor(y Natural) Natural { // > 0 if x > y // func (x Natural) Cmp(y Natural) int { - n := len(x); - m := len(y); + n := len(x) + m := len(y) if n != m || n == 0 { return n - m } - i := n - 1; + i := n - 1 for i > 0 && x[i] == y[i] { i-- } - d := 0; + d := 0 switch { case x[i] < y[i]: d = -1 @@ -783,7 +783,7 @@ func (x Natural) Cmp(y Natural) int { d = 1 } - return d; + return d } @@ -792,13 +792,13 @@ func (x Natural) Cmp(y Natural) int { // If x == 0 a run-time error occurs. // func log2(x uint64) uint { - assert(x > 0); - n := uint(0); + assert(x > 0) + n := uint(0) for x > 0 { - x >>= 1; - n++; + x >>= 1 + n++ } - return n - 1; + return n - 1 } @@ -807,11 +807,11 @@ func log2(x uint64) uint { // If x == 0 a run-time error occurs. // func (x Natural) Log2() uint { - n := len(x); + n := len(x) if n > 0 { return (uint(n)-1)*_W + log2(uint64(x[n-1])) } - panic("Log2(0)"); + panic("Log2(0)") } @@ -819,15 +819,15 @@ func (x Natural) Log2() uint { // Returns updated x and x mod d. // func divmod1(x Natural, d digit) (Natural, digit) { - assert(0 < d && isSmall(d-1)); + assert(0 < d && isSmall(d-1)) - c := digit(0); + c := digit(0) for i := len(x) - 1; i >= 0; i-- { - t := c<<_W + x[i]; - c, x[i] = t%d, t/d; + t := c<<_W + x[i] + c, x[i] = t%d, t/d } - return normalize(x), c; + return normalize(x), c } @@ -839,31 +839,31 @@ func (x Natural) ToString(base uint) string { } // allocate buffer for conversion - assert(2 <= base && base <= 16); - n := (x.Log2()+1)/log2(uint64(base)) + 1; // +1: round up - s := make([]byte, n); + assert(2 <= base && base <= 16) + n := (x.Log2()+1)/log2(uint64(base)) + 1 // +1: round up + s := make([]byte, n) // don't destroy x - t := make(Natural, len(x)); - copy(t, x); + t := make(Natural, len(x)) + copy(t, x) // convert - i := n; + i := n for !t.IsZero() { - i--; - var d digit; - t, d = divmod1(t, digit(base)); - s[i] = "0123456789abcdef"[d]; + i-- + var d digit + t, d = divmod1(t, digit(base)) + s[i] = "0123456789abcdef"[d] } - return string(s[i:n]); + return string(s[i:n]) } // String converts x to its decimal string representation. // x.String() is the same as x.ToString(10). // -func (x Natural) String() string { return x.ToString(10) } +func (x Natural) String() string { return x.ToString(10) } func fmtbase(c int) uint { @@ -875,18 +875,18 @@ func fmtbase(c int) uint { case 'x': return 16 } - return 10; + return 10 } // Format is a support routine for fmt.Formatter. It accepts // the formats 'b' (binary), 'o' (octal), and 'x' (hexadecimal). // -func (x Natural) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString(fmtbase(c))) } +func (x Natural) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString(fmtbase(c))) } func hexvalue(ch byte) uint { - d := uint(1 << logH); + d := uint(1 << logH) switch { case '0' <= ch && ch <= '9': d = uint(ch - '0') @@ -895,7 +895,7 @@ func hexvalue(ch byte) uint { case 'A' <= ch && ch <= 'F': d = uint(ch-'A') + 10 } - return d; + return d } @@ -911,9 +911,9 @@ func hexvalue(ch byte) uint { // func NatFromString(s string, base uint) (Natural, uint, int) { // determine base if necessary - i, n := 0, len(s); + i, n := 0, len(s) if base == 0 { - base = 10; + base = 10 if n > 0 && s[0] == '0' { if n > 1 && (s[1] == 'x' || s[1] == 'X') { base, i = 16, 2 @@ -924,10 +924,10 @@ func NatFromString(s string, base uint) (Natural, uint, int) { } // convert string - assert(2 <= base && base <= 16); - x := Nat(0); + assert(2 <= base && base <= 16) + x := Nat(0) for ; i < n; i++ { - d := hexvalue(s[i]); + d := hexvalue(s[i]) if d < base { x = muladd1(x, digit(base), digit(d)) } else { @@ -935,46 +935,46 @@ func NatFromString(s string, base uint) (Natural, uint, int) { } } - return x, base, i; + return x, base, i } // Natural number functions func pop1(x digit) uint { - n := uint(0); + n := uint(0) for x != 0 { - x &= x - 1; - n++; + x &= x - 1 + n++ } - return n; + return n } // Pop computes the ``population count'' of (the number of 1 bits in) x. // func (x Natural) Pop() uint { - n := uint(0); + n := uint(0) for i := len(x) - 1; i >= 0; i-- { n += pop1(x[i]) } - return n; + return n } // Pow computes x to the power of n. // func (xp Natural) Pow(n uint) Natural { - z := Nat(1); - x := xp; + z := Nat(1) + x := xp for n > 0 { // z * x^n == x^n0 if n&1 == 1 { z = z.Mul(x) } - x, n = x.Mul(x), n/2; + x, n = x.Mul(x), n/2 } - return z; + return z } @@ -990,9 +990,9 @@ func MulRange(a, b uint) Natural { case a+1 == b: return Nat(uint64(a)).Mul(Nat(uint64(b))) } - m := (a + b) >> 1; - assert(a <= m && m < b); - return MulRange(a, m).Mul(MulRange(m+1, b)); + m := (a + b) >> 1 + assert(a <= m && m < b) + return MulRange(a, m).Mul(MulRange(m+1, b)) } @@ -1007,16 +1007,16 @@ func Fact(n uint) Natural { // Binomial computes the binomial coefficient of (n, k). // -func Binomial(n, k uint) Natural { return MulRange(n-k+1, n).Div(MulRange(1, k)) } +func Binomial(n, k uint) Natural { return MulRange(n-k+1, n).Div(MulRange(1, k)) } // Gcd computes the gcd of x and y. // func (x Natural) Gcd(y Natural) Natural { // Euclidean algorithm. - a, b := x, y; + a, b := x, y for !b.IsZero() { a, b = b, a.Mod(b) } - return a; + return a } diff --git a/src/pkg/bignum/bignum_test.go b/src/pkg/bignum/bignum_test.go index 73edc9345..532fc9740 100644 --- a/src/pkg/bignum/bignum_test.go +++ b/src/pkg/bignum/bignum_test.go @@ -5,62 +5,62 @@ package bignum import ( - "fmt"; - "testing"; + "fmt" + "testing" ) const ( - sa = "991"; - sb = "2432902008176640000"; // 20! - sc = "933262154439441526816992388562667004907159682643816214685929" + + sa = "991" + sb = "2432902008176640000" // 20! + sc = "933262154439441526816992388562667004907159682643816214685929" + "638952175999932299156089414639761565182862536979208272237582" + - "51185210916864000000000000000000000000"; // 100! - sp = "170141183460469231731687303715884105727"; // prime + "51185210916864000000000000000000000000" // 100! + sp = "170141183460469231731687303715884105727" // prime ) func natFromString(s string, base uint, slen *int) Natural { - x, _, len := NatFromString(s, base); + x, _, len := NatFromString(s, base) if slen != nil { *slen = len } - return x; + return x } func intFromString(s string, base uint, slen *int) *Integer { - x, _, len := IntFromString(s, base); + x, _, len := IntFromString(s, base) if slen != nil { *slen = len } - return x; + return x } func ratFromString(s string, base uint, slen *int) *Rational { - x, _, len := RatFromString(s, base); + x, _, len := RatFromString(s, base) if slen != nil { *slen = len } - return x; + return x } var ( - nat_zero = Nat(0); - nat_one = Nat(1); - nat_two = Nat(2); - a = natFromString(sa, 10, nil); - b = natFromString(sb, 10, nil); - c = natFromString(sc, 10, nil); - p = natFromString(sp, 10, nil); - int_zero = Int(0); - int_one = Int(1); - int_two = Int(2); - ip = intFromString(sp, 10, nil); - rat_zero = Rat(0, 1); - rat_half = Rat(1, 2); - rat_one = Rat(1, 1); - rat_two = Rat(2, 1); + nat_zero = Nat(0) + nat_one = Nat(1) + nat_two = Nat(2) + a = natFromString(sa, 10, nil) + b = natFromString(sb, 10, nil) + c = natFromString(sc, 10, nil) + p = natFromString(sp, 10, nil) + int_zero = Int(0) + int_one = Int(1) + int_two = Int(2) + ip = intFromString(sp, 10, nil) + rat_zero = Rat(0, 1) + rat_half = Rat(1, 2) + rat_one = Rat(1, 1) + rat_two = Rat(2, 1) ) @@ -96,11 +96,11 @@ func rat_eq(n uint, x, y *Rational) { func TestNatConv(t *testing.T) { - tester = t; - test_msg = "NatConvA"; + tester = t + test_msg = "NatConvA" type entry1 struct { - x uint64; - s string; + x uint64 + s string } tab := []entry1{ entry1{0, "0"}, @@ -108,51 +108,51 @@ func TestNatConv(t *testing.T) { entry1{65535, "65535"}, entry1{4294967295, "4294967295"}, entry1{18446744073709551615, "18446744073709551615"}, - }; + } for i, e := range tab { - test(100+uint(i), Nat(e.x).String() == e.s); - test(200+uint(i), natFromString(e.s, 0, nil).Value() == e.x); + test(100+uint(i), Nat(e.x).String() == e.s) + test(200+uint(i), natFromString(e.s, 0, nil).Value() == e.x) } - test_msg = "NatConvB"; + test_msg = "NatConvB" for i := uint(0); i < 100; i++ { test(i, Nat(uint64(i)).String() == fmt.Sprintf("%d", i)) } - test_msg = "NatConvC"; - z := uint64(7); + test_msg = "NatConvC" + z := uint64(7) for i := uint(0); i <= 64; i++ { - test(i, Nat(z).Value() == z); - z <<= 1; - } - - test_msg = "NatConvD"; - nat_eq(0, a, Nat(991)); - nat_eq(1, b, Fact(20)); - nat_eq(2, c, Fact(100)); - test(3, a.String() == sa); - test(4, b.String() == sb); - test(5, c.String() == sc); - - test_msg = "NatConvE"; - var slen int; - nat_eq(10, natFromString("0", 0, nil), nat_zero); - nat_eq(11, natFromString("123", 0, nil), Nat(123)); - nat_eq(12, natFromString("077", 0, nil), Nat(7*8+7)); - nat_eq(13, natFromString("0x1f", 0, nil), Nat(1*16+15)); - nat_eq(14, natFromString("0x1fg", 0, &slen), Nat(1*16+15)); - test(4, slen == 4); - - test_msg = "NatConvF"; - tmp := c.Mul(c); + test(i, Nat(z).Value() == z) + z <<= 1 + } + + test_msg = "NatConvD" + nat_eq(0, a, Nat(991)) + nat_eq(1, b, Fact(20)) + nat_eq(2, c, Fact(100)) + test(3, a.String() == sa) + test(4, b.String() == sb) + test(5, c.String() == sc) + + test_msg = "NatConvE" + var slen int + nat_eq(10, natFromString("0", 0, nil), nat_zero) + nat_eq(11, natFromString("123", 0, nil), Nat(123)) + nat_eq(12, natFromString("077", 0, nil), Nat(7*8+7)) + nat_eq(13, natFromString("0x1f", 0, nil), Nat(1*16+15)) + nat_eq(14, natFromString("0x1fg", 0, &slen), Nat(1*16+15)) + test(4, slen == 4) + + test_msg = "NatConvF" + tmp := c.Mul(c) for base := uint(2); base <= 16; base++ { nat_eq(base, natFromString(tmp.ToString(base), base, nil), tmp) } - test_msg = "NatConvG"; - x := Nat(100); - y, _, _ := NatFromString(fmt.Sprintf("%b", &x), 2); - nat_eq(100, y, x); + test_msg = "NatConvG" + x := Nat(100) + y, _, _ := NatFromString(fmt.Sprintf("%b", &x), 2) + nat_eq(100, y, x) } @@ -160,16 +160,16 @@ func abs(x int64) uint64 { if x < 0 { x = -x } - return uint64(x); + return uint64(x) } func TestIntConv(t *testing.T) { - tester = t; - test_msg = "IntConvA"; + tester = t + test_msg = "IntConvA" type entry2 struct { - x int64; - s string; + x int64 + s string } tab := []entry2{ entry2{0, "0"}, @@ -181,92 +181,92 @@ func TestIntConv(t *testing.T) { entry2{2147483647, "2147483647"}, entry2{-9223372036854775808, "-9223372036854775808"}, entry2{9223372036854775807, "9223372036854775807"}, - }; + } for i, e := range tab { - test(100+uint(i), Int(e.x).String() == e.s); - test(200+uint(i), intFromString(e.s, 0, nil).Value() == e.x); - test(300+uint(i), Int(e.x).Abs().Value() == abs(e.x)); - } - - test_msg = "IntConvB"; - var slen int; - int_eq(0, intFromString("0", 0, nil), int_zero); - int_eq(1, intFromString("-0", 0, nil), int_zero); - int_eq(2, intFromString("123", 0, nil), Int(123)); - int_eq(3, intFromString("-123", 0, nil), Int(-123)); - int_eq(4, intFromString("077", 0, nil), Int(7*8+7)); - int_eq(5, intFromString("-077", 0, nil), Int(-(7*8 + 7))); - int_eq(6, intFromString("0x1f", 0, nil), Int(1*16+15)); - int_eq(7, intFromString("-0x1f", 0, &slen), Int(-(1*16 + 15))); - test(7, slen == 5); - int_eq(8, intFromString("+0x1f", 0, &slen), Int(+(1*16 + 15))); - test(8, slen == 5); - int_eq(9, intFromString("0x1fg", 0, &slen), Int(1*16+15)); - test(9, slen == 4); - int_eq(10, intFromString("-0x1fg", 0, &slen), Int(-(1*16 + 15))); - test(10, slen == 5); + test(100+uint(i), Int(e.x).String() == e.s) + test(200+uint(i), intFromString(e.s, 0, nil).Value() == e.x) + test(300+uint(i), Int(e.x).Abs().Value() == abs(e.x)) + } + + test_msg = "IntConvB" + var slen int + int_eq(0, intFromString("0", 0, nil), int_zero) + int_eq(1, intFromString("-0", 0, nil), int_zero) + int_eq(2, intFromString("123", 0, nil), Int(123)) + int_eq(3, intFromString("-123", 0, nil), Int(-123)) + int_eq(4, intFromString("077", 0, nil), Int(7*8+7)) + int_eq(5, intFromString("-077", 0, nil), Int(-(7*8 + 7))) + int_eq(6, intFromString("0x1f", 0, nil), Int(1*16+15)) + int_eq(7, intFromString("-0x1f", 0, &slen), Int(-(1*16 + 15))) + test(7, slen == 5) + int_eq(8, intFromString("+0x1f", 0, &slen), Int(+(1*16 + 15))) + test(8, slen == 5) + int_eq(9, intFromString("0x1fg", 0, &slen), Int(1*16+15)) + test(9, slen == 4) + int_eq(10, intFromString("-0x1fg", 0, &slen), Int(-(1*16 + 15))) + test(10, slen == 5) } func TestRatConv(t *testing.T) { - tester = t; - test_msg = "RatConv"; - var slen int; - rat_eq(0, ratFromString("0", 0, nil), rat_zero); - rat_eq(1, ratFromString("0/1", 0, nil), rat_zero); - rat_eq(2, ratFromString("0/01", 0, nil), rat_zero); - rat_eq(3, ratFromString("0x14/10", 0, &slen), rat_two); - test(4, slen == 7); - rat_eq(5, ratFromString("0.", 0, nil), rat_zero); - rat_eq(6, ratFromString("0.001f", 10, nil), Rat(1, 1000)); - rat_eq(7, ratFromString(".1", 0, nil), Rat(1, 10)); - rat_eq(8, ratFromString("10101.0101", 2, nil), Rat(0x155, 1<<4)); - rat_eq(9, ratFromString("-0003.145926", 10, &slen), Rat(-3145926, 1000000)); - test(10, slen == 12); - rat_eq(11, ratFromString("1e2", 0, nil), Rat(100, 1)); - rat_eq(12, ratFromString("1e-2", 0, nil), Rat(1, 100)); - rat_eq(13, ratFromString("1.1e2", 0, nil), Rat(110, 1)); - rat_eq(14, ratFromString(".1e2x", 0, &slen), Rat(10, 1)); - test(15, slen == 4); + tester = t + test_msg = "RatConv" + var slen int + rat_eq(0, ratFromString("0", 0, nil), rat_zero) + rat_eq(1, ratFromString("0/1", 0, nil), rat_zero) + rat_eq(2, ratFromString("0/01", 0, nil), rat_zero) + rat_eq(3, ratFromString("0x14/10", 0, &slen), rat_two) + test(4, slen == 7) + rat_eq(5, ratFromString("0.", 0, nil), rat_zero) + rat_eq(6, ratFromString("0.001f", 10, nil), Rat(1, 1000)) + rat_eq(7, ratFromString(".1", 0, nil), Rat(1, 10)) + rat_eq(8, ratFromString("10101.0101", 2, nil), Rat(0x155, 1<<4)) + rat_eq(9, ratFromString("-0003.145926", 10, &slen), Rat(-3145926, 1000000)) + test(10, slen == 12) + rat_eq(11, ratFromString("1e2", 0, nil), Rat(100, 1)) + rat_eq(12, ratFromString("1e-2", 0, nil), Rat(1, 100)) + rat_eq(13, ratFromString("1.1e2", 0, nil), Rat(110, 1)) + rat_eq(14, ratFromString(".1e2x", 0, &slen), Rat(10, 1)) + test(15, slen == 4) } func add(x, y Natural) Natural { - z1 := x.Add(y); - z2 := y.Add(x); + z1 := x.Add(y) + z2 := y.Add(x) if z1.Cmp(z2) != 0 { tester.Fatalf("addition not symmetric:\n\tx = %v\n\ty = %t", x, y) } - return z1; + return z1 } func sum(n uint64, scale Natural) Natural { - s := nat_zero; + s := nat_zero for ; n > 0; n-- { s = add(s, Nat(n).Mul(scale)) } - return s; + return s } func TestNatAdd(t *testing.T) { - tester = t; - test_msg = "NatAddA"; - nat_eq(0, add(nat_zero, nat_zero), nat_zero); - nat_eq(1, add(nat_zero, c), c); + tester = t + test_msg = "NatAddA" + nat_eq(0, add(nat_zero, nat_zero), nat_zero) + nat_eq(1, add(nat_zero, c), c) - test_msg = "NatAddB"; + test_msg = "NatAddB" for i := uint64(0); i < 100; i++ { - t := Nat(i); - nat_eq(uint(i), sum(i, c), t.Mul(t).Add(t).Shr(1).Mul(c)); + t := Nat(i) + nat_eq(uint(i), sum(i, c), t.Mul(t).Add(t).Shr(1).Mul(c)) } } func mul(x, y Natural) Natural { - z1 := x.Mul(y); - z2 := y.Mul(x); + z1 := x.Mul(y) + z2 := y.Mul(x) if z1.Cmp(z2) != 0 { tester.Fatalf("multiplication not symmetric:\n\tx = %v\n\ty = %t", x, y) } @@ -276,40 +276,40 @@ func mul(x, y Natural) Natural { if !y.IsZero() && z1.Div(y).Cmp(x) != 0 { tester.Fatalf("multiplication/division not inverse (B):\n\tx = %v\n\ty = %t", x, y) } - return z1; + return z1 } func TestNatSub(t *testing.T) { - tester = t; - test_msg = "NatSubA"; - nat_eq(0, nat_zero.Sub(nat_zero), nat_zero); - nat_eq(1, c.Sub(nat_zero), c); + tester = t + test_msg = "NatSubA" + nat_eq(0, nat_zero.Sub(nat_zero), nat_zero) + nat_eq(1, c.Sub(nat_zero), c) - test_msg = "NatSubB"; + test_msg = "NatSubB" for i := uint64(0); i < 100; i++ { - t := sum(i, c); + t := sum(i, c) for j := uint64(0); j <= i; j++ { t = t.Sub(mul(Nat(j), c)) } - nat_eq(uint(i), t, nat_zero); + nat_eq(uint(i), t, nat_zero) } } func TestNatMul(t *testing.T) { - tester = t; - test_msg = "NatMulA"; - nat_eq(0, mul(c, nat_zero), nat_zero); - nat_eq(1, mul(c, nat_one), c); - - test_msg = "NatMulB"; - nat_eq(0, b.Mul(MulRange(0, 100)), nat_zero); - nat_eq(1, b.Mul(MulRange(21, 100)), c); - - test_msg = "NatMulC"; - const n = 100; - p := b.Mul(c).Shl(n); + tester = t + test_msg = "NatMulA" + nat_eq(0, mul(c, nat_zero), nat_zero) + nat_eq(1, mul(c, nat_one), c) + + test_msg = "NatMulB" + nat_eq(0, b.Mul(MulRange(0, 100)), nat_zero) + nat_eq(1, b.Mul(MulRange(21, 100)), c) + + test_msg = "NatMulC" + const n = 100 + p := b.Mul(c).Shl(n) for i := uint(0); i < n; i++ { nat_eq(i, mul(b.Shl(i), c.Shl(n-i)), p) } @@ -317,17 +317,17 @@ func TestNatMul(t *testing.T) { func TestNatDiv(t *testing.T) { - tester = t; - test_msg = "NatDivA"; - nat_eq(0, c.Div(nat_one), c); - nat_eq(1, c.Div(Nat(100)), Fact(99)); - nat_eq(2, b.Div(c), nat_zero); - nat_eq(4, nat_one.Shl(100).Div(nat_one.Shl(90)), nat_one.Shl(10)); - nat_eq(5, c.Div(b), MulRange(21, 100)); - - test_msg = "NatDivB"; - const n = 100; - p := Fact(n); + tester = t + test_msg = "NatDivA" + nat_eq(0, c.Div(nat_one), c) + nat_eq(1, c.Div(Nat(100)), Fact(99)) + nat_eq(2, b.Div(c), nat_zero) + nat_eq(4, nat_one.Shl(100).Div(nat_one.Shl(90)), nat_one.Shl(10)) + nat_eq(5, c.Div(b), MulRange(21, 100)) + + test_msg = "NatDivB" + const n = 100 + p := Fact(n) for i := uint(0); i < n; i++ { nat_eq(100+i, p.Div(MulRange(1, i)), MulRange(i+1, n)) } @@ -335,10 +335,10 @@ func TestNatDiv(t *testing.T) { func TestIntQuoRem(t *testing.T) { - tester = t; - test_msg = "IntQuoRem"; + tester = t + test_msg = "IntQuoRem" type T struct { - x, y, q, r int64; + x, y, q, r int64 } a := []T{ T{+8, +3, +2, +2}, @@ -349,25 +349,25 @@ func TestIntQuoRem(t *testing.T) { T{+1, -2, 0, +1}, T{-1, +2, 0, -1}, T{-1, -2, 0, -1}, - }; + } for i := uint(0); i < uint(len(a)); i++ { - e := &a[i]; - x, y := Int(e.x).Mul(ip), Int(e.y).Mul(ip); - q, r := Int(e.q), Int(e.r).Mul(ip); - qq, rr := x.QuoRem(y); - int_eq(4*i+0, x.Quo(y), q); - int_eq(4*i+1, x.Rem(y), r); - int_eq(4*i+2, qq, q); - int_eq(4*i+3, rr, r); + e := &a[i] + x, y := Int(e.x).Mul(ip), Int(e.y).Mul(ip) + q, r := Int(e.q), Int(e.r).Mul(ip) + qq, rr := x.QuoRem(y) + int_eq(4*i+0, x.Quo(y), q) + int_eq(4*i+1, x.Rem(y), r) + int_eq(4*i+2, qq, q) + int_eq(4*i+3, rr, r) } } func TestIntDivMod(t *testing.T) { - tester = t; - test_msg = "IntDivMod"; + tester = t + test_msg = "IntDivMod" type T struct { - x, y, q, r int64; + x, y, q, r int64 } a := []T{ T{+8, +3, +2, +2}, @@ -378,144 +378,144 @@ func TestIntDivMod(t *testing.T) { T{+1, -2, 0, +1}, T{-1, +2, -1, +1}, T{-1, -2, +1, +1}, - }; + } for i := uint(0); i < uint(len(a)); i++ { - e := &a[i]; - x, y := Int(e.x).Mul(ip), Int(e.y).Mul(ip); - q, r := Int(e.q), Int(e.r).Mul(ip); - qq, rr := x.DivMod(y); - int_eq(4*i+0, x.Div(y), q); - int_eq(4*i+1, x.Mod(y), r); - int_eq(4*i+2, qq, q); - int_eq(4*i+3, rr, r); + e := &a[i] + x, y := Int(e.x).Mul(ip), Int(e.y).Mul(ip) + q, r := Int(e.q), Int(e.r).Mul(ip) + qq, rr := x.DivMod(y) + int_eq(4*i+0, x.Div(y), q) + int_eq(4*i+1, x.Mod(y), r) + int_eq(4*i+2, qq, q) + int_eq(4*i+3, rr, r) } } func TestNatMod(t *testing.T) { - tester = t; - test_msg = "NatModA"; + tester = t + test_msg = "NatModA" for i := uint(0); ; i++ { - d := nat_one.Shl(i); + d := nat_one.Shl(i) if d.Cmp(c) < 0 { nat_eq(i, c.Add(d).Mod(c), d) } else { - nat_eq(i, c.Add(d).Div(c), nat_two); - nat_eq(i, c.Add(d).Mod(c), d.Sub(c)); - break; + nat_eq(i, c.Add(d).Div(c), nat_two) + nat_eq(i, c.Add(d).Mod(c), d.Sub(c)) + break } } } func TestNatShift(t *testing.T) { - tester = t; - test_msg = "NatShift1L"; - test(0, b.Shl(0).Cmp(b) == 0); - test(1, c.Shl(1).Cmp(c) > 0); + tester = t + test_msg = "NatShift1L" + test(0, b.Shl(0).Cmp(b) == 0) + test(1, c.Shl(1).Cmp(c) > 0) - test_msg = "NatShift1R"; - test(3, b.Shr(0).Cmp(b) == 0); - test(4, c.Shr(1).Cmp(c) < 0); + test_msg = "NatShift1R" + test(3, b.Shr(0).Cmp(b) == 0) + test(4, c.Shr(1).Cmp(c) < 0) - test_msg = "NatShift2"; + test_msg = "NatShift2" for i := uint(0); i < 100; i++ { test(i, c.Shl(i).Shr(i).Cmp(c) == 0) } - test_msg = "NatShift3L"; + test_msg = "NatShift3L" { - const m = 3; - p := b; - f := Nat(1 << m); + const m = 3 + p := b + f := Nat(1 << m) for i := uint(0); i < 100; i++ { - nat_eq(i, b.Shl(i*m), p); - p = mul(p, f); + nat_eq(i, b.Shl(i*m), p) + p = mul(p, f) } } - test_msg = "NatShift3R"; + test_msg = "NatShift3R" { - p := c; + p := c for i := uint(0); !p.IsZero(); i++ { - nat_eq(i, c.Shr(i), p); - p = p.Shr(1); + nat_eq(i, c.Shr(i), p) + p = p.Shr(1) } } } func TestIntShift(t *testing.T) { - tester = t; - test_msg = "IntShift1L"; - test(0, ip.Shl(0).Cmp(ip) == 0); - test(1, ip.Shl(1).Cmp(ip) > 0); + tester = t + test_msg = "IntShift1L" + test(0, ip.Shl(0).Cmp(ip) == 0) + test(1, ip.Shl(1).Cmp(ip) > 0) - test_msg = "IntShift1R"; - test(0, ip.Shr(0).Cmp(ip) == 0); - test(1, ip.Shr(1).Cmp(ip) < 0); + test_msg = "IntShift1R" + test(0, ip.Shr(0).Cmp(ip) == 0) + test(1, ip.Shr(1).Cmp(ip) < 0) - test_msg = "IntShift2"; + test_msg = "IntShift2" for i := uint(0); i < 100; i++ { test(i, ip.Shl(i).Shr(i).Cmp(ip) == 0) } - test_msg = "IntShift3L"; + test_msg = "IntShift3L" { - const m = 3; - p := ip; - f := Int(1 << m); + const m = 3 + p := ip + f := Int(1 << m) for i := uint(0); i < 100; i++ { - int_eq(i, ip.Shl(i*m), p); - p = p.Mul(f); + int_eq(i, ip.Shl(i*m), p) + p = p.Mul(f) } } - test_msg = "IntShift3R"; + test_msg = "IntShift3R" { - p := ip; + p := ip for i := uint(0); p.IsPos(); i++ { - int_eq(i, ip.Shr(i), p); - p = p.Shr(1); + int_eq(i, ip.Shr(i), p) + p = p.Shr(1) } } - test_msg = "IntShift4R"; - int_eq(0, Int(-43).Shr(1), Int(-43>>1)); - int_eq(0, Int(-1024).Shr(100), Int(-1)); - int_eq(1, ip.Neg().Shr(10), ip.Neg().Div(Int(1).Shl(10))); + test_msg = "IntShift4R" + int_eq(0, Int(-43).Shr(1), Int(-43>>1)) + int_eq(0, Int(-1024).Shr(100), Int(-1)) + int_eq(1, ip.Neg().Shr(10), ip.Neg().Div(Int(1).Shl(10))) } func TestNatBitOps(t *testing.T) { - tester = t; + tester = t - x := uint64(0xf08e6f56bd8c3941); - y := uint64(0x3984ef67834bc); + x := uint64(0xf08e6f56bd8c3941) + y := uint64(0x3984ef67834bc) - bx := Nat(x); - by := Nat(y); + bx := Nat(x) + by := Nat(y) - test_msg = "NatAnd"; - bz := Nat(x & y); + test_msg = "NatAnd" + bz := Nat(x & y) for i := uint(0); i < 100; i++ { nat_eq(i, bx.Shl(i).And(by.Shl(i)), bz.Shl(i)) } - test_msg = "NatAndNot"; - bz = Nat(x &^ y); + test_msg = "NatAndNot" + bz = Nat(x &^ y) for i := uint(0); i < 100; i++ { nat_eq(i, bx.Shl(i).AndNot(by.Shl(i)), bz.Shl(i)) } - test_msg = "NatOr"; - bz = Nat(x | y); + test_msg = "NatOr" + bz = Nat(x | y) for i := uint(0); i < 100; i++ { nat_eq(i, bx.Shl(i).Or(by.Shl(i)), bz.Shl(i)) } - test_msg = "NatXor"; - bz = Nat(x ^ y); + test_msg = "NatXor" + bz = Nat(x ^ y) for i := uint(0); i < 100; i++ { nat_eq(i, bx.Shl(i).Xor(by.Shl(i)), bz.Shl(i)) } @@ -523,77 +523,77 @@ func TestNatBitOps(t *testing.T) { func TestIntBitOps1(t *testing.T) { - tester = t; - test_msg = "IntBitOps1"; + tester = t + test_msg = "IntBitOps1" type T struct { - x, y int64; + x, y int64 } a := []T{ T{+7, +3}, T{+7, -3}, T{-7, +3}, T{-7, -3}, - }; + } for i := uint(0); i < uint(len(a)); i++ { - e := &a[i]; - int_eq(4*i+0, Int(e.x).And(Int(e.y)), Int(e.x&e.y)); - int_eq(4*i+1, Int(e.x).AndNot(Int(e.y)), Int(e.x&^e.y)); - int_eq(4*i+2, Int(e.x).Or(Int(e.y)), Int(e.x|e.y)); - int_eq(4*i+3, Int(e.x).Xor(Int(e.y)), Int(e.x^e.y)); + e := &a[i] + int_eq(4*i+0, Int(e.x).And(Int(e.y)), Int(e.x&e.y)) + int_eq(4*i+1, Int(e.x).AndNot(Int(e.y)), Int(e.x&^e.y)) + int_eq(4*i+2, Int(e.x).Or(Int(e.y)), Int(e.x|e.y)) + int_eq(4*i+3, Int(e.x).Xor(Int(e.y)), Int(e.x^e.y)) } } func TestIntBitOps2(t *testing.T) { - tester = t; + tester = t - test_msg = "IntNot"; - int_eq(0, Int(-2).Not(), Int(1)); - int_eq(0, Int(-1).Not(), Int(0)); - int_eq(0, Int(0).Not(), Int(-1)); - int_eq(0, Int(1).Not(), Int(-2)); - int_eq(0, Int(2).Not(), Int(-3)); + test_msg = "IntNot" + int_eq(0, Int(-2).Not(), Int(1)) + int_eq(0, Int(-1).Not(), Int(0)) + int_eq(0, Int(0).Not(), Int(-1)) + int_eq(0, Int(1).Not(), Int(-2)) + int_eq(0, Int(2).Not(), Int(-3)) - test_msg = "IntAnd"; + test_msg = "IntAnd" for x := int64(-15); x < 5; x++ { - bx := Int(x); + bx := Int(x) for y := int64(-5); y < 15; y++ { - by := Int(y); - for i := uint(50); i < 70; i++ { // shift across 64bit boundary + by := Int(y) + for i := uint(50); i < 70; i++ { // shift across 64bit boundary int_eq(i, bx.Shl(i).And(by.Shl(i)), Int(x&y).Shl(i)) } } } - test_msg = "IntAndNot"; + test_msg = "IntAndNot" for x := int64(-15); x < 5; x++ { - bx := Int(x); + bx := Int(x) for y := int64(-5); y < 15; y++ { - by := Int(y); - for i := uint(50); i < 70; i++ { // shift across 64bit boundary - int_eq(2*i+0, bx.Shl(i).AndNot(by.Shl(i)), Int(x&^y).Shl(i)); - int_eq(2*i+1, bx.Shl(i).And(by.Shl(i).Not()), Int(x&^y).Shl(i)); + by := Int(y) + for i := uint(50); i < 70; i++ { // shift across 64bit boundary + int_eq(2*i+0, bx.Shl(i).AndNot(by.Shl(i)), Int(x&^y).Shl(i)) + int_eq(2*i+1, bx.Shl(i).And(by.Shl(i).Not()), Int(x&^y).Shl(i)) } } } - test_msg = "IntOr"; + test_msg = "IntOr" for x := int64(-15); x < 5; x++ { - bx := Int(x); + bx := Int(x) for y := int64(-5); y < 15; y++ { - by := Int(y); - for i := uint(50); i < 70; i++ { // shift across 64bit boundary + by := Int(y) + for i := uint(50); i < 70; i++ { // shift across 64bit boundary int_eq(i, bx.Shl(i).Or(by.Shl(i)), Int(x|y).Shl(i)) } } } - test_msg = "IntXor"; + test_msg = "IntXor" for x := int64(-15); x < 5; x++ { - bx := Int(x); + bx := Int(x) for y := int64(-5); y < 15; y++ { - by := Int(y); - for i := uint(50); i < 70; i++ { // shift across 64bit boundary + by := Int(y) + for i := uint(50); i < 70; i++ { // shift across 64bit boundary int_eq(i, bx.Shl(i).Xor(by.Shl(i)), Int(x^y).Shl(i)) } } @@ -602,27 +602,27 @@ func TestIntBitOps2(t *testing.T) { func TestNatCmp(t *testing.T) { - tester = t; - test_msg = "NatCmp"; - test(0, a.Cmp(a) == 0); - test(1, a.Cmp(b) < 0); - test(2, b.Cmp(a) > 0); - test(3, a.Cmp(c) < 0); - d := c.Add(b); - test(4, c.Cmp(d) < 0); - test(5, d.Cmp(c) > 0); + tester = t + test_msg = "NatCmp" + test(0, a.Cmp(a) == 0) + test(1, a.Cmp(b) < 0) + test(2, b.Cmp(a) > 0) + test(3, a.Cmp(c) < 0) + d := c.Add(b) + test(4, c.Cmp(d) < 0) + test(5, d.Cmp(c) > 0) } func TestNatLog2(t *testing.T) { - tester = t; - test_msg = "NatLog2A"; - test(0, nat_one.Log2() == 0); - test(1, nat_two.Log2() == 1); - test(2, Nat(3).Log2() == 1); - test(3, Nat(4).Log2() == 2); - - test_msg = "NatLog2B"; + tester = t + test_msg = "NatLog2A" + test(0, nat_one.Log2() == 0) + test(1, nat_two.Log2() == 1) + test(2, Nat(3).Log2() == 1) + test(3, Nat(4).Log2() == 2) + + test_msg = "NatLog2B" for i := uint(0); i < 100; i++ { test(i, nat_one.Shl(i).Log2() == i) } @@ -630,19 +630,19 @@ func TestNatLog2(t *testing.T) { func TestNatGcd(t *testing.T) { - tester = t; - test_msg = "NatGcdA"; - f := Nat(99991); - nat_eq(0, b.Mul(f).Gcd(c.Mul(f)), MulRange(1, 20).Mul(f)); + tester = t + test_msg = "NatGcdA" + f := Nat(99991) + nat_eq(0, b.Mul(f).Gcd(c.Mul(f)), MulRange(1, 20).Mul(f)) } func TestNatPow(t *testing.T) { - tester = t; - test_msg = "NatPowA"; - nat_eq(0, nat_two.Pow(0), nat_one); + tester = t + test_msg = "NatPowA" + nat_eq(0, nat_two.Pow(0), nat_one) - test_msg = "NatPowB"; + test_msg = "NatPowB" for i := uint(0); i < 100; i++ { nat_eq(i, nat_two.Pow(i), nat_one.Shl(i)) } @@ -650,15 +650,15 @@ func TestNatPow(t *testing.T) { func TestNatPop(t *testing.T) { - tester = t; - test_msg = "NatPopA"; - test(0, nat_zero.Pop() == 0); - test(1, nat_one.Pop() == 1); - test(2, Nat(10).Pop() == 2); - test(3, Nat(30).Pop() == 4); - test(4, Nat(0x1248f).Shl(33).Pop() == 8); - - test_msg = "NatPopB"; + tester = t + test_msg = "NatPopA" + test(0, nat_zero.Pop() == 0) + test(1, nat_one.Pop() == 1) + test(2, Nat(10).Pop() == 2) + test(3, Nat(30).Pop() == 4) + test(4, Nat(0x1248f).Shl(33).Pop() == 8) + + test_msg = "NatPopB" for i := uint(0); i < 100; i++ { test(i, nat_one.Shl(i).Sub(nat_one).Pop() == i) } diff --git a/src/pkg/bignum/integer.go b/src/pkg/bignum/integer.go index 3d382473e..873b2664a 100644 --- a/src/pkg/bignum/integer.go +++ b/src/pkg/bignum/integer.go @@ -10,7 +10,7 @@ package bignum import ( - "fmt"; + "fmt" ) // TODO(gri) Complete the set of in-place operations. @@ -18,8 +18,8 @@ import ( // Integer represents a signed integer value of arbitrary precision. // type Integer struct { - sign bool; - mant Natural; + sign bool + mant Natural } @@ -29,16 +29,16 @@ type Integer struct { // func MakeInt(sign bool, mant Natural) *Integer { if mant.IsZero() { - sign = false // normalize + sign = false // normalize } - return &Integer{sign, mant}; + return &Integer{sign, mant} } // Int creates a small integer with value x. // func Int(x int64) *Integer { - var ux uint64; + var ux uint64 if x < 0 { // For the most negative x, -x == x, and // the bit pattern has the correct value. @@ -46,7 +46,7 @@ func Int(x int64) *Integer { } else { ux = uint64(x) } - return MakeInt(x < 0, Nat(ux)); + return MakeInt(x < 0, Nat(ux)) } @@ -54,51 +54,51 @@ func Int(x int64) *Integer { // otherwise the result is undefined. // func (x *Integer) Value() int64 { - z := int64(x.mant.Value()); + z := int64(x.mant.Value()) if x.sign { z = -z } - return z; + return z } // Abs returns the absolute value of x. // -func (x *Integer) Abs() Natural { return x.mant } +func (x *Integer) Abs() Natural { return x.mant } // Predicates // IsEven returns true iff x is divisible by 2. // -func (x *Integer) IsEven() bool { return x.mant.IsEven() } +func (x *Integer) IsEven() bool { return x.mant.IsEven() } // IsOdd returns true iff x is not divisible by 2. // -func (x *Integer) IsOdd() bool { return x.mant.IsOdd() } +func (x *Integer) IsOdd() bool { return x.mant.IsOdd() } // IsZero returns true iff x == 0. // -func (x *Integer) IsZero() bool { return x.mant.IsZero() } +func (x *Integer) IsZero() bool { return x.mant.IsZero() } // IsNeg returns true iff x < 0. // -func (x *Integer) IsNeg() bool { return x.sign && !x.mant.IsZero() } +func (x *Integer) IsNeg() bool { return x.sign && !x.mant.IsZero() } // IsPos returns true iff x >= 0. // -func (x *Integer) IsPos() bool { return !x.sign && !x.mant.IsZero() } +func (x *Integer) IsPos() bool { return !x.sign && !x.mant.IsZero() } // Operations // Neg returns the negated value of x. // -func (x *Integer) Neg() *Integer { return MakeInt(!x.sign, x.mant) } +func (x *Integer) Neg() *Integer { return MakeInt(!x.sign, x.mant) } // Iadd sets z to the sum x + y. @@ -108,17 +108,17 @@ func Iadd(z, x, y *Integer) { if x.sign == y.sign { // x + y == x + y // (-x) + (-y) == -(x + y) - z.sign = x.sign; - Nadd(&z.mant, x.mant, y.mant); + z.sign = x.sign + Nadd(&z.mant, x.mant, y.mant) } else { // x + (-y) == x - y == -(y - x) // (-x) + y == y - x == -(x - y) if x.mant.Cmp(y.mant) >= 0 { - z.sign = x.sign; - Nsub(&z.mant, x.mant, y.mant); + z.sign = x.sign + Nsub(&z.mant, x.mant, y.mant) } else { - z.sign = !x.sign; - Nsub(&z.mant, y.mant, x.mant); + z.sign = !x.sign + Nsub(&z.mant, y.mant, x.mant) } } } @@ -127,9 +127,9 @@ func Iadd(z, x, y *Integer) { // Add returns the sum x + y. // func (x *Integer) Add(y *Integer) *Integer { - var z Integer; - Iadd(&z, x, y); - return &z; + var z Integer + Iadd(&z, x, y) + return &z } @@ -137,17 +137,17 @@ func Isub(z, x, y *Integer) { if x.sign != y.sign { // x - (-y) == x + y // (-x) - y == -(x + y) - z.sign = x.sign; - Nadd(&z.mant, x.mant, y.mant); + z.sign = x.sign + Nadd(&z.mant, x.mant, y.mant) } else { // x - y == x - y == -(y - x) // (-x) - (-y) == y - x == -(x - y) if x.mant.Cmp(y.mant) >= 0 { - z.sign = x.sign; - Nsub(&z.mant, x.mant, y.mant); + z.sign = x.sign + Nsub(&z.mant, x.mant, y.mant) } else { - z.sign = !x.sign; - Nsub(&z.mant, y.mant, x.mant); + z.sign = !x.sign + Nsub(&z.mant, y.mant, x.mant) } } } @@ -156,32 +156,32 @@ func Isub(z, x, y *Integer) { // Sub returns the difference x - y. // func (x *Integer) Sub(y *Integer) *Integer { - var z Integer; - Isub(&z, x, y); - return &z; + var z Integer + Isub(&z, x, y) + return &z } // Nscale sets *z to the scaled value (*z) * d. // func Iscale(z *Integer, d int64) { - f := uint64(d); + f := uint64(d) if d < 0 { f = uint64(-d) } - z.sign = z.sign != (d < 0); - Nscale(&z.mant, f); + z.sign = z.sign != (d < 0) + Nscale(&z.mant, f) } // Mul1 returns the product x * d. // func (x *Integer) Mul1(d int64) *Integer { - f := uint64(d); + f := uint64(d) if d < 0 { f = uint64(-d) } - return MakeInt(x.sign != (d < 0), x.mant.Mul1(f)); + return MakeInt(x.sign != (d < 0), x.mant.Mul1(f)) } @@ -242,8 +242,8 @@ func (x *Integer) Rem(y *Integer) *Integer { // If y == 0, a division-by-zero run-time error occurs. // func (x *Integer) QuoRem(y *Integer) (*Integer, *Integer) { - q, r := x.mant.DivMod(y.mant); - return MakeInt(x.sign != y.sign, q), MakeInt(x.sign, r); + q, r := x.mant.DivMod(y.mant) + return MakeInt(x.sign != y.sign, q), MakeInt(x.sign, r) } @@ -261,7 +261,7 @@ func (x *Integer) QuoRem(y *Integer) (*Integer, *Integer) { // ACM press.) // func (x *Integer) Div(y *Integer) *Integer { - q, r := x.QuoRem(y); + q, r := x.QuoRem(y) if r.IsNeg() { if y.IsPos() { q = q.Sub(Int(1)) @@ -269,7 +269,7 @@ func (x *Integer) Div(y *Integer) *Integer { q = q.Add(Int(1)) } } - return q; + return q } @@ -278,7 +278,7 @@ func (x *Integer) Div(y *Integer) *Integer { // If y == 0, a division-by-zero run-time error occurs. // func (x *Integer) Mod(y *Integer) *Integer { - r := x.Rem(y); + r := x.Rem(y) if r.IsNeg() { if y.IsPos() { r = r.Add(y) @@ -286,30 +286,30 @@ func (x *Integer) Mod(y *Integer) *Integer { r = r.Sub(y) } } - return r; + return r } // DivMod returns the pair (x.Div(y), x.Mod(y)). // func (x *Integer) DivMod(y *Integer) (*Integer, *Integer) { - q, r := x.QuoRem(y); + q, r := x.QuoRem(y) if r.IsNeg() { if y.IsPos() { - q = q.Sub(Int(1)); - r = r.Add(y); + q = q.Sub(Int(1)) + r = r.Add(y) } else { - q = q.Add(Int(1)); - r = r.Sub(y); + q = q.Add(Int(1)) + r = r.Sub(y) } } - return q, r; + return q, r } // Shl implements ``shift left'' x << s. It returns x * 2^s. // -func (x *Integer) Shl(s uint) *Integer { return MakeInt(x.sign, x.mant.Shl(s)) } +func (x *Integer) Shl(s uint) *Integer { return MakeInt(x.sign, x.mant.Shl(s)) } // The bitwise operations on integers are defined on the 2's-complement @@ -336,7 +336,7 @@ func (x *Integer) Shr(s uint) *Integer { return MakeInt(true, x.mant.Sub(Nat(1)).Shr(s).Add(Nat(1))) } - return MakeInt(false, x.mant.Shr(s)); + return MakeInt(false, x.mant.Shr(s)) } @@ -348,7 +348,7 @@ func (x *Integer) Not() *Integer { } // ^x == -x-1 == -(x+1) - return MakeInt(true, x.mant.Add(Nat(1))); + return MakeInt(true, x.mant.Add(Nat(1))) } @@ -362,16 +362,16 @@ func (x *Integer) And(y *Integer) *Integer { } // x & y == x & y - return MakeInt(false, x.mant.And(y.mant)); + return MakeInt(false, x.mant.And(y.mant)) } // x.sign != y.sign if x.sign { - x, y = y, x // & is symmetric + x, y = y, x // & is symmetric } // x & (-y) == x & ^(y-1) == x &^ (y-1) - return MakeInt(false, x.mant.AndNot(y.mant.Sub(Nat(1)))); + return MakeInt(false, x.mant.AndNot(y.mant.Sub(Nat(1)))) } @@ -385,7 +385,7 @@ func (x *Integer) AndNot(y *Integer) *Integer { } // x &^ y == x &^ y - return MakeInt(false, x.mant.AndNot(y.mant)); + return MakeInt(false, x.mant.AndNot(y.mant)) } if x.sign { @@ -394,7 +394,7 @@ func (x *Integer) AndNot(y *Integer) *Integer { } // x &^ (-y) == x &^ ^(y-1) == x & (y-1) - return MakeInt(false, x.mant.And(y.mant.Sub(Nat(1)))); + return MakeInt(false, x.mant.And(y.mant.Sub(Nat(1)))) } @@ -408,16 +408,16 @@ func (x *Integer) Or(y *Integer) *Integer { } // x | y == x | y - return MakeInt(false, x.mant.Or(y.mant)); + return MakeInt(false, x.mant.Or(y.mant)) } // x.sign != y.sign if x.sign { - x, y = y, x // | or symmetric + x, y = y, x // | or symmetric } // x | (-y) == x | ^(y-1) == ^((y-1) &^ x) == -(^((y-1) &^ x) + 1) - return MakeInt(true, y.mant.Sub(Nat(1)).AndNot(x.mant).Add(Nat(1))); + return MakeInt(true, y.mant.Sub(Nat(1)).AndNot(x.mant).Add(Nat(1))) } @@ -431,16 +431,16 @@ func (x *Integer) Xor(y *Integer) *Integer { } // x ^ y == x ^ y - return MakeInt(false, x.mant.Xor(y.mant)); + return MakeInt(false, x.mant.Xor(y.mant)) } // x.sign != y.sign if x.sign { - x, y = y, x // ^ is symmetric + x, y = y, x // ^ is symmetric } // x ^ (-y) == x ^ ^(y-1) == ^(x ^ (y-1)) == -((x ^ (y-1)) + 1) - return MakeInt(true, x.mant.Xor(y.mant.Sub(Nat(1))).Add(Nat(1))); + return MakeInt(true, x.mant.Xor(y.mant.Sub(Nat(1))).Add(Nat(1))) } @@ -455,10 +455,10 @@ func (x *Integer) Cmp(y *Integer) int { // x cmp (-y) == x // (-x) cmp y == y // (-x) cmp (-y) == -(x cmp y) - var r int; + var r int switch { case x.sign == y.sign: - r = x.mant.Cmp(y.mant); + r = x.mant.Cmp(y.mant) if x.sign { r = -r } @@ -467,7 +467,7 @@ func (x *Integer) Cmp(y *Integer) int { case y.sign: r = 1 } - return r; + return r } @@ -477,24 +477,24 @@ func (x *Integer) ToString(base uint) string { if x.mant.IsZero() { return "0" } - var s string; + var s string if x.sign { s = "-" } - return s + x.mant.ToString(base); + return s + x.mant.ToString(base) } // String converts x to its decimal string representation. // x.String() is the same as x.ToString(10). // -func (x *Integer) String() string { return x.ToString(10) } +func (x *Integer) String() string { return x.ToString(10) } // Format is a support routine for fmt.Formatter. It accepts // the formats 'b' (binary), 'o' (octal), and 'x' (hexadecimal). // -func (x *Integer) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString(fmtbase(c))) } +func (x *Integer) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString(fmtbase(c))) } // IntFromString returns the integer corresponding to the @@ -509,12 +509,12 @@ func (x *Integer) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString(f // func IntFromString(s string, base uint) (*Integer, uint, int) { // skip sign, if any - i0 := 0; + i0 := 0 if len(s) > 0 && (s[0] == '-' || s[0] == '+') { i0 = 1 } - mant, base, slen := NatFromString(s[i0:], base); + mant, base, slen := NatFromString(s[i0:], base) - return MakeInt(i0 > 0 && s[0] == '-', mant), base, i0 + slen; + return MakeInt(i0 > 0 && s[0] == '-', mant), base, i0 + slen } diff --git a/src/pkg/bignum/nrdiv_test.go b/src/pkg/bignum/nrdiv_test.go index 724eecec3..725b1acea 100644 --- a/src/pkg/bignum/nrdiv_test.go +++ b/src/pkg/bignum/nrdiv_test.go @@ -21,8 +21,8 @@ import "testing" // value of an fpNat x is x.m * 2^x.e . // type fpNat struct { - m Natural; - e int; + m Natural + e int } @@ -34,16 +34,16 @@ func (x fpNat) sub(y fpNat) fpNat { case d > 0: return fpNat{x.m.Shl(uint(d)).Sub(y.m), y.e} } - return fpNat{x.m.Sub(y.m), x.e}; + return fpNat{x.m.Sub(y.m), x.e} } // mul2 computes x*2. -func (x fpNat) mul2() fpNat { return fpNat{x.m, x.e + 1} } +func (x fpNat) mul2() fpNat { return fpNat{x.m, x.e + 1} } // mul computes x*y. -func (x fpNat) mul(y fpNat) fpNat { return fpNat{x.m.Mul(y.m), x.e + y.e} } +func (x fpNat) mul(y fpNat) fpNat { return fpNat{x.m.Mul(y.m), x.e + y.e} } // mant computes the (possibly truncated) Natural representation @@ -56,7 +56,7 @@ func (x fpNat) mant() Natural { case x.e < 0: return x.m.Shr(uint(-x.e)) } - return x.m; + return x.m } @@ -65,8 +65,8 @@ func (x fpNat) mant() Natural { // func nrDivEst(x0, y0 Natural) Natural { if y0.IsZero() { - panic("division by zero"); - return nil; + panic("division by zero") + return nil } // y0 > 0 @@ -84,78 +84,78 @@ func nrDivEst(x0, y0 Natural) Natural { // x0 > y0 > 1 // Determine maximum result length. - maxLen := int(x0.Log2() - y0.Log2() + 1); + maxLen := int(x0.Log2() - y0.Log2() + 1) // In the following, each number x is represented // as a mantissa x.m and an exponent x.e such that // x = xm * 2^x.e. - x := fpNat{x0, 0}; - y := fpNat{y0, 0}; + x := fpNat{x0, 0} + y := fpNat{y0, 0} // Determine a scale factor f = 2^e such that // 0.5 <= y/f == y*(2^-e) < 1.0 // and scale y accordingly. - e := int(y.m.Log2()) + 1; - y.e -= e; + e := int(y.m.Log2()) + 1 + y.e -= e // t1 - var c = 2.9142; - const n = 14; - t1 := fpNat{Nat(uint64(c * (1 << n))), -n}; + var c = 2.9142 + const n = 14 + t1 := fpNat{Nat(uint64(c * (1 << n))), -n} // Compute initial value r0 for the reciprocal of y/f. // r0 = t1 - 2*y - r := t1.sub(y.mul2()); - two := fpNat{Nat(2), 0}; + r := t1.sub(y.mul2()) + two := fpNat{Nat(2), 0} // Newton-Raphson iteration - p := Nat(0); + p := Nat(0) for i := 0; ; i++ { // check if we are done // TODO: Need to come up with a better test here // as it will reduce computation time significantly. // q = x*r/f - q := x.mul(r); - q.e -= e; - res := q.mant(); + q := x.mul(r) + q.e -= e + res := q.mant() if res.Cmp(p) == 0 { return res } - p = res; + p = res // r' = r*(2 - y*r) - r = r.mul(two.sub(y.mul(r))); + r = r.mul(two.sub(y.mul(r))) // reduce mantissa size // TODO: Find smaller bound as it will reduce // computation time massively. - d := int(r.m.Log2()+1) - maxLen; + d := int(r.m.Log2()+1) - maxLen if d > 0 { r = fpNat{r.m.Shr(uint(d)), r.e + d} } } - panic("unreachable"); - return nil; + panic("unreachable") + return nil } func nrdiv(x, y Natural) (q, r Natural) { - q = nrDivEst(x, y); - r = x.Sub(y.Mul(q)); + q = nrDivEst(x, y) + r = x.Sub(y.Mul(q)) // if r is too large, correct q and r // (usually one iteration) for r.Cmp(y) >= 0 { - q = q.Add(Nat(1)); - r = r.Sub(y); + q = q.Add(Nat(1)) + r = r.Sub(y) } - return; + return } func div(t *testing.T, x, y Natural) { - q, r := nrdiv(x, y); - qx, rx := x.DivMod(y); + q, r := nrdiv(x, y) + qx, rx := x.DivMod(y) if q.Cmp(qx) != 0 { t.Errorf("x = %s, y = %s, got q = %s, want q = %s", x, y, q, qx) } @@ -165,24 +165,24 @@ func div(t *testing.T, x, y Natural) { } -func idiv(t *testing.T, x0, y0 uint64) { div(t, Nat(x0), Nat(y0)) } +func idiv(t *testing.T, x0, y0 uint64) { div(t, Nat(x0), Nat(y0)) } func TestNRDiv(t *testing.T) { - idiv(t, 17, 18); - idiv(t, 17, 17); - idiv(t, 17, 1); - idiv(t, 17, 16); - idiv(t, 17, 10); - idiv(t, 17, 9); - idiv(t, 17, 8); - idiv(t, 17, 5); - idiv(t, 17, 3); - idiv(t, 1025, 512); - idiv(t, 7489595, 2); - idiv(t, 5404679459, 78495); - idiv(t, 7484890589595, 7484890589594); - div(t, Fact(100), Fact(91)); - div(t, Fact(1000), Fact(991)); + idiv(t, 17, 18) + idiv(t, 17, 17) + idiv(t, 17, 1) + idiv(t, 17, 16) + idiv(t, 17, 10) + idiv(t, 17, 9) + idiv(t, 17, 8) + idiv(t, 17, 5) + idiv(t, 17, 3) + idiv(t, 1025, 512) + idiv(t, 7489595, 2) + idiv(t, 5404679459, 78495) + idiv(t, 7484890589595, 7484890589594) + div(t, Fact(100), Fact(91)) + div(t, Fact(1000), Fact(991)) //div(t, Fact(10000), Fact(9991)); // takes too long - disabled for now } diff --git a/src/pkg/bignum/rational.go b/src/pkg/bignum/rational.go index 9e9c3a8e0..378585e5f 100644 --- a/src/pkg/bignum/rational.go +++ b/src/pkg/bignum/rational.go @@ -12,31 +12,31 @@ import "fmt" // Rational represents a quotient a/b of arbitrary precision. // type Rational struct { - a *Integer; // numerator - b Natural; // denominator + a *Integer // numerator + b Natural // denominator } // MakeRat makes a rational number given a numerator a and a denominator b. // func MakeRat(a *Integer, b Natural) *Rational { - f := a.mant.Gcd(b); // f > 0 + f := a.mant.Gcd(b) // f > 0 if f.Cmp(Nat(1)) != 0 { - a = MakeInt(a.sign, a.mant.Div(f)); - b = b.Div(f); + a = MakeInt(a.sign, a.mant.Div(f)) + b = b.Div(f) } - return &Rational{a, b}; + return &Rational{a, b} } // Rat creates a small rational number with value a0/b0. // func Rat(a0 int64, b0 int64) *Rational { - a, b := Int(a0), Int(b0); + a, b := Int(a0), Int(b0) if b.sign { a = a.Neg() } - return MakeRat(a, b.mant); + return MakeRat(a, b.mant) } @@ -51,30 +51,30 @@ func (x *Rational) Value() (numerator *Integer, denominator Natural) { // IsZero returns true iff x == 0. // -func (x *Rational) IsZero() bool { return x.a.IsZero() } +func (x *Rational) IsZero() bool { return x.a.IsZero() } // IsNeg returns true iff x < 0. // -func (x *Rational) IsNeg() bool { return x.a.IsNeg() } +func (x *Rational) IsNeg() bool { return x.a.IsNeg() } // IsPos returns true iff x > 0. // -func (x *Rational) IsPos() bool { return x.a.IsPos() } +func (x *Rational) IsPos() bool { return x.a.IsPos() } // IsInt returns true iff x can be written with a denominator 1 // in the form x == x'/1; i.e., if x is an integer value. // -func (x *Rational) IsInt() bool { return x.b.Cmp(Nat(1)) == 0 } +func (x *Rational) IsInt() bool { return x.b.Cmp(Nat(1)) == 0 } // Operations // Neg returns the negated value of x. // -func (x *Rational) Neg() *Rational { return MakeRat(x.a.Neg(), x.b) } +func (x *Rational) Neg() *Rational { return MakeRat(x.a.Neg(), x.b) } // Add returns the sum x + y. @@ -93,19 +93,19 @@ func (x *Rational) Sub(y *Rational) *Rational { // Mul returns the product x * y. // -func (x *Rational) Mul(y *Rational) *Rational { return MakeRat(x.a.Mul(y.a), x.b.Mul(y.b)) } +func (x *Rational) Mul(y *Rational) *Rational { return MakeRat(x.a.Mul(y.a), x.b.Mul(y.b)) } // Quo returns the quotient x / y for y != 0. // If y == 0, a division-by-zero run-time error occurs. // func (x *Rational) Quo(y *Rational) *Rational { - a := x.a.MulNat(y.b); - b := y.a.MulNat(x.b); + a := x.a.MulNat(y.b) + b := y.a.MulNat(x.b) if b.IsNeg() { a = a.Neg() } - return MakeRat(a, b.mant); + return MakeRat(a, b.mant) } @@ -115,7 +115,7 @@ func (x *Rational) Quo(y *Rational) *Rational { // == 0 if x == y // > 0 if x > y // -func (x *Rational) Cmp(y *Rational) int { return (x.a.MulNat(y.b)).Cmp(y.a.MulNat(x.b)) } +func (x *Rational) Cmp(y *Rational) int { return (x.a.MulNat(y.b)).Cmp(y.a.MulNat(x.b)) } // ToString converts x to a string for a given base, with 2 <= base <= 16. @@ -123,24 +123,24 @@ func (x *Rational) Cmp(y *Rational) int { return (x.a.MulNat(y.b)).Cmp(y.a.MulNa // it is of form "n/d". // func (x *Rational) ToString(base uint) string { - s := x.a.ToString(base); + s := x.a.ToString(base) if !x.IsInt() { s += "/" + x.b.ToString(base) } - return s; + return s } // String converts x to its decimal string representation. // x.String() is the same as x.ToString(10). // -func (x *Rational) String() string { return x.ToString(10) } +func (x *Rational) String() string { return x.ToString(10) } // Format is a support routine for fmt.Formatter. It accepts // the formats 'b' (binary), 'o' (octal), and 'x' (hexadecimal). // -func (x *Rational) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString(fmtbase(c))) } +func (x *Rational) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString(fmtbase(c))) } // RatFromString returns the rational number corresponding to the @@ -164,35 +164,35 @@ func (x *Rational) Format(h fmt.State, c int) { fmt.Fprintf(h, "%s", x.ToString( // func RatFromString(s string, base uint) (*Rational, uint, int) { // read numerator - a, abase, alen := IntFromString(s, base); - b := Nat(1); + a, abase, alen := IntFromString(s, base) + b := Nat(1) // read denominator or fraction, if any - var blen int; + var blen int if alen < len(s) { - ch := s[alen]; + ch := s[alen] if ch == '/' { - alen++; - b, base, blen = NatFromString(s[alen:], base); + alen++ + b, base, blen = NatFromString(s[alen:], base) } else if ch == '.' { - alen++; - b, base, blen = NatFromString(s[alen:], abase); - assert(base == abase); - f := Nat(uint64(base)).Pow(uint(blen)); - a = MakeInt(a.sign, a.mant.Mul(f).Add(b)); - b = f; + alen++ + b, base, blen = NatFromString(s[alen:], abase) + assert(base == abase) + f := Nat(uint64(base)).Pow(uint(blen)) + a = MakeInt(a.sign, a.mant.Mul(f).Add(b)) + b = f } } // read exponent, if any - rlen := alen + blen; + rlen := alen + blen if rlen < len(s) { - ch := s[rlen]; + ch := s[rlen] if ch == 'e' || ch == 'E' { - rlen++; - e, _, elen := IntFromString(s[rlen:], 10); - rlen += elen; - m := Nat(10).Pow(uint(e.mant.Value())); + rlen++ + e, _, elen := IntFromString(s[rlen:], 10) + rlen += elen + m := Nat(10).Pow(uint(e.mant.Value())) if e.sign { b = b.Mul(m) } else { @@ -201,5 +201,5 @@ func RatFromString(s string, base uint) (*Rational, uint, int) { } } - return MakeRat(a, b), base, rlen; + return MakeRat(a, b), base, rlen } |