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
9f46962c
Commit
9f46962c
authored
Aug 31, 2010
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
big: added RatString, some simplifications
R=rsc CC=golang-dev
https://golang.org/cl/2095041
parent
3ad995ea
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
14 deletions
+19
-14
src/pkg/big/rat.go
src/pkg/big/rat.go
+17
-12
src/pkg/big/rat_test.go
src/pkg/big/rat_test.go
+2
-2
No files found.
src/pkg/big/rat.go
View file @
9f46962c
...
...
@@ -269,29 +269,34 @@ func (z *Rat) SetString(s string) (*Rat, bool) {
}
// String returns a string representation of z in the form "a/b".
// String returns a string representation of z in the form "a/b"
(even if b == 1)
.
func
(
z
*
Rat
)
String
()
string
{
s
:=
z
.
a
.
String
()
if
len
(
z
.
b
)
==
1
&&
z
.
b
[
0
]
==
1
{
return
s
return
z
.
a
.
String
()
+
"/"
+
z
.
b
.
string
(
10
)
}
// RatString returns a string representation of z in the form "a/b" if b != 1,
// and in the form "a" if b == 1.
func
(
z
*
Rat
)
RatString
()
string
{
if
z
.
IsInt
()
{
return
z
.
a
.
String
()
}
return
s
+
"/"
+
z
.
b
.
string
(
10
)
return
z
.
String
(
)
}
// FloatString returns a string representation of z in decimal form with prec
// digits of precision after the decimal point and the last digit rounded.
func
(
z
*
Rat
)
FloatString
(
prec
int
)
string
{
if
z
.
IsInt
()
{
return
z
.
a
.
String
()
}
q
,
r
:=
nat
{}
.
div
(
nat
{},
z
.
a
.
abs
,
z
.
b
)
s
:=
""
s
:=
q
.
string
(
10
)
if
z
.
a
.
neg
{
s
=
"-"
}
s
+=
q
.
string
(
10
)
if
len
(
z
.
b
)
==
1
&&
z
.
b
[
0
]
==
1
{
return
s
s
=
"-"
+
s
}
p
:=
nat
{}
.
expNN
(
natTen
,
nat
{
Word
(
prec
)},
nil
)
...
...
src/pkg/big/rat_test.go
View file @
9f46962c
...
...
@@ -48,8 +48,8 @@ func TestRatSetString(t *testing.T) {
for
i
,
test
:=
range
setStringTests
{
x
,
ok
:=
new
(
Rat
)
.
SetString
(
test
.
in
)
if
ok
!=
test
.
ok
||
ok
&&
x
.
String
()
!=
test
.
out
{
t
.
Errorf
(
"#%d got %s want %s"
,
i
,
x
.
String
(),
test
.
out
)
if
ok
!=
test
.
ok
||
ok
&&
x
.
Rat
String
()
!=
test
.
out
{
t
.
Errorf
(
"#%d got %s want %s"
,
i
,
x
.
Rat
String
(),
test
.
out
)
}
}
}
...
...
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