Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
afad8272
Commit
afad8272
authored
Oct 31, 2008
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- div and mod (arbitrary precision)
- more tests - some global renames R=r OCL=18219 CL=18219
parent
12a34358
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
15 deletions
+74
-15
bignum.go
usr/gri/bignum/bignum.go
+0
-0
bignum_test.go
usr/gri/bignum/bignum_test.go
+74
-15
No files found.
usr/gri/bignum/bignum.go
View file @
afad8272
This diff is collapsed.
Click to expand it.
usr/gri/bignum/bignum_test.go
View file @
afad8272
...
...
@@ -4,7 +4,7 @@
package
main
import
Big
num
"bignum"
import
Big
"bignum"
const
(
sa
=
"991"
;
...
...
@@ -14,25 +14,35 @@ const (
var
(
a
=
Big
num
.
NatFromString
(
sa
,
10
);
b
=
Big
num
.
NatFromString
(
sb
,
10
);
c
=
Big
num
.
NatFromString
(
sc
,
10
);
a
=
Big
.
NatFromString
(
sa
,
10
);
b
=
Big
.
NatFromString
(
sb
,
10
);
c
=
Big
.
NatFromString
(
sc
,
10
);
)
var
test_msg
string
;
func
TEST
(
n
int
,
b
bool
)
{
func
TEST
(
n
u
int
,
b
bool
)
{
if
!
b
{
panic
(
"TEST failed: "
,
test_msg
,
"("
,
n
,
")
\n
"
);
}
}
func
TEST_EQ
(
n
uint
,
x
,
y
*
Big
.
Natural
)
{
if
x
.
Cmp
(
y
)
!=
0
{
println
(
"TEST failed: "
,
test_msg
,
"("
,
n
,
")
\n
"
);
println
(
"x = "
,
x
.
String
(
10
));
println
(
"y = "
,
y
.
String
(
10
));
panic
();
}
}
func
TestConv
()
{
test_msg
=
"TestConv"
;
TEST
(
0
,
a
.
Cmp
(
Big
num
.
New
Nat
(
991
))
==
0
);
TEST
(
1
,
b
.
Cmp
(
Big
num
.
Fact
(
20
))
==
0
);
TEST
(
2
,
c
.
Cmp
(
Big
num
.
Fact
(
100
))
==
0
);
TEST
(
0
,
a
.
Cmp
(
Big
.
Nat
(
991
))
==
0
);
TEST
(
1
,
b
.
Cmp
(
Big
.
Fact
(
20
))
==
0
);
TEST
(
2
,
c
.
Cmp
(
Big
.
Fact
(
100
))
==
0
);
TEST
(
3
,
a
.
String
(
10
)
==
sa
);
TEST
(
4
,
b
.
String
(
10
)
==
sb
);
TEST
(
5
,
c
.
String
(
10
)
==
sc
);
...
...
@@ -49,32 +59,81 @@ func TestShift() {
TEST
(
1
,
c
.
Shr
(
1
)
.
Cmp
(
c
)
<
0
);
test_msg
=
"TestShift2"
;
for
i
:=
0
;
i
<
100
;
i
++
{
TEST
(
i
,
c
.
Shl
(
uint
(
i
))
.
Shr
(
uint
(
i
)
)
.
Cmp
(
c
)
==
0
);
for
i
:=
uint
(
0
)
;
i
<
100
;
i
++
{
TEST
(
i
,
c
.
Shl
(
i
)
.
Shr
(
i
)
.
Cmp
(
c
)
==
0
);
}
test_msg
=
"TestShift3L"
;
{
const
m
=
3
;
p
:=
b
;
f
:=
Big
num
.
New
Nat
(
1
<<
m
);
for
i
:=
0
;
i
<
100
;
i
++
{
TEST
(
i
,
b
.
Shl
(
uint
(
i
*
m
))
.
Cmp
(
p
)
==
0
);
f
:=
Big
.
Nat
(
1
<<
m
);
for
i
:=
uint
(
0
)
;
i
<
100
;
i
++
{
TEST
_EQ
(
i
,
b
.
Shl
(
i
*
m
),
p
);
p
=
p
.
Mul
(
f
);
}
}
test_msg
=
"TestShift3R"
;
{
p
:=
c
;
for
i
:=
0
;
c
.
Cmp
(
Bignum
.
NatZero
)
==
0
;
i
++
{
TEST
(
i
,
c
.
Shr
(
uint
(
i
))
.
Cmp
(
p
)
==
0
);
for
i
:=
uint
(
0
);
c
.
Cmp
(
Big
.
NatZero
)
==
0
;
i
++
{
TEST
_EQ
(
i
,
c
.
Shr
(
i
),
p
);
p
=
p
.
Shr
(
1
);
}
}
}
func
TestMul
()
{
test_msg
=
"TestMulA"
;
TEST_EQ
(
0
,
b
.
Mul
(
Big
.
MulRange
(
0
,
100
)),
Big
.
Nat
(
0
));
TEST_EQ
(
0
,
b
.
Mul
(
Big
.
MulRange
(
21
,
100
)),
c
);
test_msg
=
"TestMulB"
;
const
n
=
100
;
p
:=
b
.
Mul
(
c
)
.
Shl
(
n
);
for
i
:=
uint
(
0
);
i
<
n
;
i
++
{
TEST_EQ
(
i
,
b
.
Shl
(
i
)
.
Mul
(
c
.
Shl
(
n
-
i
)),
p
);
}
}
func
TestDiv
()
{
test_msg
=
"TestDivA"
;
TEST_EQ
(
0
,
c
.
Div
(
Big
.
Nat
(
1
)),
c
);
TEST_EQ
(
1
,
c
.
Div
(
Big
.
Nat
(
100
)),
Big
.
Fact
(
99
));
TEST_EQ
(
2
,
b
.
Div
(
c
),
Big
.
Nat
(
0
));
TEST_EQ
(
4
,
Big
.
Nat
(
1
)
.
Shl
(
100
)
.
Div
(
Big
.
Nat
(
1
)
.
Shl
(
90
)),
Big
.
Nat
(
1
)
.
Shl
(
10
));
TEST_EQ
(
5
,
c
.
Div
(
b
),
Big
.
MulRange
(
21
,
100
));
test_msg
=
"TestDivB"
;
const
n
=
100
;
p
:=
Big
.
Fact
(
n
);
for
i
:=
uint
(
0
);
i
<
n
;
i
++
{
TEST_EQ
(
i
,
p
.
Div
(
Big
.
MulRange
(
1
,
uint64
(
i
))),
Big
.
MulRange
(
uint64
(
i
+
1
),
n
));
}
}
func
TestMod
()
{
test_msg
=
"TestModA"
;
for
i
:=
uint
(
0
);
;
i
++
{
d
:=
Big
.
Nat
(
1
)
.
Shl
(
i
);
if
d
.
Cmp
(
c
)
<
0
{
TEST_EQ
(
i
,
c
.
Add
(
d
)
.
Mod
(
c
),
d
);
}
else
{
TEST_EQ
(
i
,
c
.
Add
(
d
)
.
Div
(
c
),
Big
.
Nat
(
2
));
//TEST_EQ(i, c.Add(d).Mod(c), d.Sub(c));
break
;
}
}
}
func
main
()
{
TestConv
();
TestShift
();
TestMul
();
TestDiv
();
TestMod
();
print
(
"PASSED
\n
"
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment