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
448c05d7
Commit
448c05d7
authored
Nov 30, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strconv: Atof on Infs and NaNs
R=rsc CC=golang-dev
https://golang.org/cl/3359041
parent
b3896462
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
0 deletions
+52
-0
src/pkg/strconv/atof.go
src/pkg/strconv/atof.go
+42
-0
src/pkg/strconv/atof_test.go
src/pkg/strconv/atof_test.go
+10
-0
No files found.
src/pkg/strconv/atof.go
View file @
448c05d7
...
...
@@ -19,6 +19,40 @@ import (
var
optimize
=
true
// can change for testing
func
equalIgnoreCase
(
s1
,
s2
string
)
bool
{
if
len
(
s1
)
!=
len
(
s2
)
{
return
false
}
for
i
:=
0
;
i
<
len
(
s1
);
i
++
{
c1
:=
s1
[
i
]
if
'A'
<=
c1
&&
c1
<=
'Z'
{
c1
+=
'a'
-
'A'
}
c2
:=
s2
[
i
]
if
'A'
<=
c2
&&
c2
<=
'Z'
{
c2
+=
'a'
-
'A'
}
if
c1
!=
c2
{
return
false
}
}
return
true
}
func
special
(
s
string
)
(
f
float64
,
ok
bool
)
{
switch
{
case
equalIgnoreCase
(
s
,
"nan"
)
:
return
math
.
NaN
(),
true
case
equalIgnoreCase
(
s
,
"-inf"
)
:
return
math
.
Inf
(
-
1
),
true
case
equalIgnoreCase
(
s
,
"+inf"
)
:
return
math
.
Inf
(
1
),
true
case
equalIgnoreCase
(
s
,
"inf"
)
:
return
math
.
Inf
(
1
),
true
}
return
}
// TODO(rsc): Better truncation handling.
func
stringToDecimal
(
s
string
)
(
neg
bool
,
d
*
decimal
,
trunc
bool
,
ok
bool
)
{
i
:=
0
...
...
@@ -320,6 +354,10 @@ func decimalAtof32(neg bool, d *decimal, trunc bool) (f float32, ok bool) {
// away from the largest floating point number of the given size,
// Atof32 returns f = ±Inf, err.Error = os.ERANGE.
func
Atof32
(
s
string
)
(
f
float32
,
err
os
.
Error
)
{
if
val
,
ok
:=
special
(
s
);
ok
{
return
float32
(
val
),
nil
}
neg
,
d
,
trunc
,
ok
:=
stringToDecimal
(
s
)
if
!
ok
{
return
0
,
&
NumError
{
s
,
os
.
EINVAL
}
...
...
@@ -341,6 +379,10 @@ func Atof32(s string) (f float32, err os.Error) {
// Except for the type of its result, its definition is the same as that
// of Atof32.
func
Atof64
(
s
string
)
(
f
float64
,
err
os
.
Error
)
{
if
val
,
ok
:=
special
(
s
);
ok
{
return
val
,
nil
}
neg
,
d
,
trunc
,
ok
:=
stringToDecimal
(
s
)
if
!
ok
{
return
0
,
&
NumError
{
s
,
os
.
EINVAL
}
...
...
src/pkg/strconv/atof_test.go
View file @
448c05d7
...
...
@@ -37,6 +37,16 @@ var atoftests = []atofTest{
{
"1e-20"
,
"1e-20"
,
nil
},
{
"625e-3"
,
"0.625"
,
nil
},
// NaNs
{
"nan"
,
"NaN"
,
nil
},
{
"NaN"
,
"NaN"
,
nil
},
{
"NAN"
,
"NaN"
,
nil
},
// Infs
{
"inf"
,
"+Inf"
,
nil
},
{
"-Inf"
,
"-Inf"
,
nil
},
{
"+INF"
,
"+Inf"
,
nil
},
// largest float64
{
"1.7976931348623157e308"
,
"1.7976931348623157e+308"
,
nil
},
{
"-1.7976931348623157e308"
,
"-1.7976931348623157e+308"
,
nil
},
...
...
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