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
5781a00e
Commit
5781a00e
authored
Oct 07, 2010
by
Anthony Martin
Committed by
Robert Griesemer
Oct 07, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
big: fix panic and round correctly in Rat.FloatString
R=gri, rsc CC=golang-dev
https://golang.org/cl/2212044
parent
1f6f5639
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
9 deletions
+23
-9
src/pkg/big/rat.go
src/pkg/big/rat.go
+18
-9
src/pkg/big/rat_test.go
src/pkg/big/rat_test.go
+5
-0
No files found.
src/pkg/big/rat.go
View file @
5781a00e
...
...
@@ -294,25 +294,34 @@ func (z *Rat) FloatString(prec int) string {
q
,
r
:=
nat
{}
.
div
(
nat
{},
z
.
a
.
abs
,
z
.
b
)
s
:=
q
.
string
(
10
)
if
z
.
a
.
neg
{
s
=
"-"
+
s
p
:=
natOne
if
prec
>
0
{
p
=
nat
{}
.
expNN
(
natTen
,
nat
{}
.
setUint64
(
uint64
(
prec
)),
nil
)
}
p
:=
nat
{}
.
expNN
(
natTen
,
nat
{
Word
(
prec
)},
nil
)
r
=
r
.
mul
(
r
,
p
)
r
,
r2
:=
r
.
div
(
nat
{},
r
,
z
.
b
)
// see if we need to round up
r2
=
r2
.
mul
(
r2
,
natTwo
)
r2
=
r2
.
add
(
r2
,
r2
)
if
z
.
b
.
cmp
(
r2
)
<=
0
{
r
=
r
.
add
(
r
,
natOne
)
if
r
.
cmp
(
p
)
>=
0
{
q
=
nat
{}
.
add
(
q
,
natOne
)
r
=
nat
{}
.
sub
(
r
,
p
)
}
}
s
:=
q
.
string
(
10
)
if
z
.
a
.
neg
{
s
=
"-"
+
s
}
if
prec
>
0
{
rs
:=
r
.
string
(
10
)
leadingZeros
:=
prec
-
len
(
rs
)
s
+=
"."
+
strings
.
Repeat
(
"0"
,
leadingZeros
)
+
rs
s
=
strings
.
TrimRight
(
s
,
"0"
)
}
return
s
}
src/pkg/big/rat_test.go
View file @
5781a00e
...
...
@@ -71,6 +71,11 @@ var floatStringTests = []floatStringTest{
floatStringTest
{
".25"
,
1
,
"0.3"
},
floatStringTest
{
"-1/3"
,
3
,
"-0.333"
},
floatStringTest
{
"-2/3"
,
4
,
"-0.6667"
},
floatStringTest
{
"0.96"
,
1
,
"1.0"
},
floatStringTest
{
"0.999"
,
2
,
"1.00"
},
floatStringTest
{
"0.9"
,
0
,
"1"
},
floatStringTest
{
".25"
,
-
1
,
"0"
},
floatStringTest
{
".55"
,
-
1
,
"1"
},
}
func
TestFloatString
(
t
*
testing
.
T
)
{
...
...
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