Commit 66c6b13b authored by Robert Griesemer's avatar Robert Griesemer

- implemented String() and Format functionality in Bignum

- added a test

R=r
OCL=18687
CL=18687
parent 3a2c0a96
......@@ -11,6 +11,7 @@ package Bignum
// - Integer signed integer numbers
// - Rational rational numbers
import Fmt "fmt"
// ----------------------------------------------------------------------------
// Internal representation
......@@ -675,7 +676,7 @@ func DivMod1(x *Natural, d Digit) (*Natural, Digit) {
}
func (x *Natural) String(base uint) string {
func (x *Natural) ToString(base uint) string {
if len(x) == 0 {
return "0";
}
......@@ -702,6 +703,27 @@ func (x *Natural) String(base uint) string {
}
func (x *Natural) String() string {
return x.ToString(10);
}
func FmtBase(c int) uint {
switch c {
case 'b': return 2;
case 'o': return 8;
case 'x': return 16;
}
return 10;
}
func (x *Natural) Format(h Fmt.Formatter, c int) {
t := x.ToString(FmtBase(c)); // BUG in 6g
Fmt.fprintf(h, "%s", t);
}
func HexValue(ch byte) uint {
d := uint(1 << LogH);
switch {
......@@ -1092,7 +1114,7 @@ func (x *Integer) Cmp(y *Integer) int {
}
func (x *Integer) String(base uint) string {
func (x *Integer) ToString(base uint) string {
if x.mant.IsZero() {
return "0";
}
......@@ -1100,10 +1122,21 @@ func (x *Integer) String(base uint) string {
if x.sign {
s = "-";
}
return s + x.mant.String(base);
return s + x.mant.ToString(base);
}
func (x *Integer) String() string {
return x.ToString(10);
}
func (x *Integer) Format(h Fmt.Formatter, c int) {
t := x.ToString(FmtBase(c)); // BUG in 6g
Fmt.fprintf(h, "%s", t);
}
// Determines base (octal, decimal, hexadecimal) if base == 0.
// Returns the number and base.
export func IntFromString(s string, base uint, slen *int) (*Integer, uint) {
......@@ -1215,15 +1248,26 @@ func (x *Rational) Cmp(y *Rational) int {
}
func (x *Rational) String(base uint) string {
s := x.a.String(base);
func (x *Rational) ToString(base uint) string {
s := x.a.ToString(base);
if !x.IsInt() {
s += "/" + x.b.String(base);
s += "/" + x.b.ToString(base);
}
return s;
}
func (x *Rational) String() string {
return x.ToString(10);
}
func (x *Rational) Format(h Fmt.Formatter, c int) {
t := x.ToString(FmtBase(c)); // BUG in 6g
Fmt.fprintf(h, "%s", t);
}
// Determines base (octal, decimal, hexadecimal) if base == 0.
// Returns the number and base of the nominator.
export func RatFromString(s string, base uint, slen *int) (*Rational, uint) {
......
......@@ -7,6 +7,7 @@
package main
import Big "bignum"
import Fmt "fmt"
const (
sa = "991";
......@@ -71,8 +72,8 @@ func TEST(n uint, b bool) {
func NAT_EQ(n uint, x, y *Big.Natural) {
if x.Cmp(y) != 0 {
println("TEST failed:", test_msg, "(", n, ")");
println("x =", x.String(10));
println("y =", y.String(10));
println("x =", x.String());
println("y =", y.String());
panic();
}
}
......@@ -81,8 +82,8 @@ func NAT_EQ(n uint, x, y *Big.Natural) {
func INT_EQ(n uint, x, y *Big.Integer) {
if x.Cmp(y) != 0 {
println("TEST failed:", test_msg, "(", n, ")");
println("x =", x.String(10));
println("y =", y.String(10));
println("x =", x.String());
println("y =", y.String());
panic();
}
}
......@@ -91,8 +92,8 @@ func INT_EQ(n uint, x, y *Big.Integer) {
func RAT_EQ(n uint, x, y *Big.Rational) {
if x.Cmp(y) != 0 {
println("TEST failed:", test_msg, "(", n, ")");
println("x =", x.String(10));
println("y =", y.String(10));
println("x =", x.String());
println("y =", y.String());
panic();
}
}
......@@ -103,9 +104,9 @@ func NatConv() {
NAT_EQ(0, a, Big.Nat(991));
NAT_EQ(1, b, Big.Fact(20));
NAT_EQ(2, c, Big.Fact(100));
TEST(3, a.String(10) == sa);
TEST(4, b.String(10) == sb);
TEST(5, c.String(10) == sc);
TEST(3, a.String() == sa);
TEST(4, b.String() == sb);
TEST(5, c.String() == sc);
test_msg = "NatConvB";
var slen int;
......@@ -119,8 +120,13 @@ func NatConv() {
test_msg = "NatConvC";
t := c.Mul(c);
for base := uint(2); base <= 16; base++ {
NAT_EQ(base, NatFromString(t.String(base), base, nil), t);
NAT_EQ(base, NatFromString(t.ToString(base), base, nil), t);
}
test_msg = "NatConvD";
x := Big.Nat(100);
y, b := Big.NatFromString(Fmt.sprintf("%b", x), 2, nil);
NAT_EQ(0, y, x);
}
......@@ -162,8 +168,8 @@ func Add(x, y *Big.Natural) *Big.Natural {
z2 := y.Add(x);
if z1.Cmp(z2) != 0 {
println("addition not symmetric");
println("x =", x.String(10));
println("y =", y.String(10));
println("x =", x.String());
println("y =", y.String());
panic();
}
return z1;
......@@ -197,20 +203,20 @@ func Mul(x, y *Big.Natural) *Big.Natural {
z2 := y.Mul(x);
if z1.Cmp(z2) != 0 {
println("multiplication not symmetric");
println("x =", x.String(10));
println("y =", y.String(10));
println("x =", x.String());
println("y =", y.String());
panic();
}
if !x.IsZero() && z1.Div(x).Cmp(y) != 0 {
println("multiplication/division not inverse (A)");
println("x =", x.String(10));
println("y =", y.String(10));
println("x =", x.String());
println("y =", y.String());
panic();
}
if !y.IsZero() && z1.Div(y).Cmp(x) != 0 {
println("multiplication/division not inverse (B)");
println("x =", x.String(10));
println("y =", y.String(10));
println("x =", x.String());
println("y =", y.String());
panic();
}
return z1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment