Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
c7cf56be
Commit
c7cf56be
authored
Oct 18, 2011
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
big: handle aliasing correctly for Rat.SetFrac.
Fixes #2379. R=r, rsc CC=golang-dev
https://golang.org/cl/5305043
parent
862179b0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
2 deletions
+46
-2
src/pkg/big/rat.go
src/pkg/big/rat.go
+6
-2
src/pkg/big/rat_test.go
src/pkg/big/rat_test.go
+40
-0
No files found.
src/pkg/big/rat.go
View file @
c7cf56be
...
...
@@ -27,9 +27,13 @@ func NewRat(a, b int64) *Rat {
// SetFrac sets z to a/b and returns z.
func
(
z
*
Rat
)
SetFrac
(
a
,
b
*
Int
)
*
Rat
{
z
.
a
.
Set
(
a
)
z
.
a
.
neg
=
a
.
neg
!=
b
.
neg
z
.
b
=
z
.
b
.
set
(
b
.
abs
)
babs
:=
b
.
abs
if
&
z
.
a
==
b
||
alias
(
z
.
a
.
abs
,
babs
)
{
babs
=
nat
(
nil
)
.
set
(
babs
)
// make a copy
}
z
.
a
.
abs
=
z
.
a
.
abs
.
set
(
a
.
abs
)
z
.
b
=
z
.
b
.
set
(
babs
)
return
z
.
norm
()
}
...
...
src/pkg/big/rat_test.go
View file @
c7cf56be
...
...
@@ -330,3 +330,43 @@ func TestRatGobEncoding(t *testing.T) {
}
}
}
func
TestIssue2379
(
t
*
testing
.
T
)
{
// 1) no aliasing
q
:=
NewRat
(
3
,
2
)
x
:=
new
(
Rat
)
x
.
SetFrac
(
NewInt
(
3
),
NewInt
(
2
))
if
x
.
Cmp
(
q
)
!=
0
{
t
.
Errorf
(
"1) got %s want %s"
,
x
,
q
)
}
// 2) aliasing of numerator
x
=
NewRat
(
2
,
3
)
x
.
SetFrac
(
NewInt
(
3
),
x
.
Num
())
if
x
.
Cmp
(
q
)
!=
0
{
t
.
Errorf
(
"2) got %s want %s"
,
x
,
q
)
}
// 3) aliasing of denominator
x
=
NewRat
(
2
,
3
)
x
.
SetFrac
(
x
.
Denom
(),
NewInt
(
2
))
if
x
.
Cmp
(
q
)
!=
0
{
t
.
Errorf
(
"3) got %s want %s"
,
x
,
q
)
}
// 4) aliasing of numerator and denominator
x
=
NewRat
(
2
,
3
)
x
.
SetFrac
(
x
.
Denom
(),
x
.
Num
())
if
x
.
Cmp
(
q
)
!=
0
{
t
.
Errorf
(
"4) got %s want %s"
,
x
,
q
)
}
// 5) numerator and denominator are the same
q
=
NewRat
(
1
,
1
)
x
=
new
(
Rat
)
n
:=
NewInt
(
7
)
x
.
SetFrac
(
n
,
n
)
if
x
.
Cmp
(
q
)
!=
0
{
t
.
Errorf
(
"5) got %s want %s"
,
x
,
q
)
}
}
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