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
2d3e47ca
Commit
2d3e47ca
authored
Jun 18, 2010
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strconv: add AtofN, FtoaN
R=r CC=golang-dev
https://golang.org/cl/1700043
parent
34cc0112
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
0 deletions
+47
-0
src/pkg/strconv/atof.go
src/pkg/strconv/atof.go
+13
-0
src/pkg/strconv/atof_test.go
src/pkg/strconv/atof_test.go
+15
-0
src/pkg/strconv/ftoa.go
src/pkg/strconv/ftoa.go
+11
-0
src/pkg/strconv/ftoa_test.go
src/pkg/strconv/ftoa_test.go
+8
-0
No files found.
src/pkg/strconv/atof.go
View file @
2d3e47ca
...
...
@@ -367,3 +367,16 @@ func Atof(s string) (f float, err os.Error) {
f1
,
err1
:=
Atof64
(
s
)
return
float
(
f1
),
err1
}
// AtofN converts the string s to a 64-bit floating-point number,
// but it rounds the result assuming that it will be stored in a value
// of n bits (32 or 64).
func
AtofN
(
s
string
,
n
int
)
(
f
float64
,
err
os
.
Error
)
{
if
n
==
32
{
f1
,
err1
:=
Atof32
(
s
)
return
float64
(
f1
),
err1
}
f1
,
err1
:=
Atof64
(
s
)
return
f1
,
err1
}
src/pkg/strconv/atof_test.go
View file @
2d3e47ca
...
...
@@ -114,6 +114,13 @@ func testAtof(t *testing.T, opt bool) {
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
)
}
out
,
err
=
AtofN
(
test
.
in
,
64
)
outs
=
FtoaN
(
out
,
'g'
,
-
1
,
64
)
if
outs
!=
test
.
out
||
!
reflect
.
DeepEqual
(
err
,
test
.
err
)
{
t
.
Errorf
(
"AtofN(%v, 64) = %v, %v want %v, %v
\n
"
,
test
.
in
,
out
,
err
,
test
.
out
,
test
.
err
)
}
if
float64
(
float32
(
out
))
==
out
{
out32
,
err
:=
Atof32
(
test
.
in
)
outs
:=
Ftoa32
(
out32
,
'g'
,
-
1
)
...
...
@@ -121,6 +128,14 @@ func testAtof(t *testing.T, opt bool) {
t
.
Errorf
(
"Atof32(%v) = %v, %v want %v, %v # %v
\n
"
,
test
.
in
,
out32
,
err
,
test
.
out
,
test
.
err
,
out
)
}
out
,
err
:=
AtofN
(
test
.
in
,
32
)
out32
=
float32
(
out
)
outs
=
FtoaN
(
float64
(
out32
),
'g'
,
-
1
,
32
)
if
outs
!=
test
.
out
||
!
reflect
.
DeepEqual
(
err
,
test
.
err
)
{
t
.
Errorf
(
"AtofN(%v, 32) = %v, %v want %v, %v # %v
\n
"
,
test
.
in
,
out32
,
err
,
test
.
out
,
test
.
err
,
out
)
}
}
if
FloatSize
==
64
||
float64
(
float32
(
out
))
==
out
{
...
...
src/pkg/strconv/ftoa.go
View file @
2d3e47ca
...
...
@@ -64,6 +64,17 @@ func Ftoa64(f float64, fmt byte, prec int) string {
return
genericFtoa
(
math
.
Float64bits
(
f
),
fmt
,
prec
,
&
float64info
)
}
// FtoaN converts the 64-bit floating-point number f to a string,
// according to the format fmt and precision prec, but it rounds the
// result assuming that it was obtained from a floating-point value
// of n bits (32 or 64).
func
FtoaN
(
f
float64
,
fmt
byte
,
prec
int
,
n
int
)
string
{
if
n
==
32
{
return
Ftoa32
(
float32
(
f
),
fmt
,
prec
)
}
return
Ftoa64
(
f
,
fmt
,
prec
)
}
// Ftoa behaves as Ftoa32 or Ftoa64, depending on the size of the float type.
func
Ftoa
(
f
float
,
fmt
byte
,
prec
int
)
string
{
if
FloatSize
==
32
{
...
...
src/pkg/strconv/ftoa_test.go
View file @
2d3e47ca
...
...
@@ -110,11 +110,19 @@ func TestFtoa(t *testing.T) {
if
s
!=
test
.
s
{
t
.
Error
(
"test"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
)
}
s
=
FtoaN
(
test
.
f
,
test
.
fmt
,
test
.
prec
,
64
)
if
s
!=
test
.
s
{
t
.
Error
(
"testN=64"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
)
}
if
float64
(
float32
(
test
.
f
))
==
test
.
f
&&
test
.
fmt
!=
'b'
{
s
:=
Ftoa32
(
float32
(
test
.
f
),
test
.
fmt
,
test
.
prec
)
if
s
!=
test
.
s
{
t
.
Error
(
"test32"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
)
}
s
=
FtoaN
(
test
.
f
,
test
.
fmt
,
test
.
prec
,
32
)
if
s
!=
test
.
s
{
t
.
Error
(
"testN=32"
,
test
.
f
,
string
(
test
.
fmt
),
test
.
prec
,
"want"
,
test
.
s
,
"got"
,
s
)
}
}
}
}
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