1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func pow2(i
int) float64 {
25 return pow2(i
/2) * pow2(i
-i
/2)
28 // Wrapper around strconv.Atof64. Handles dddddp+ddd (binary exponent)
29 // itself, passes the rest on to strconv.Atof64.
30 func myatof64(s
string) (f
float64, ok
bool) {
31 a
:= strings
.Split(s
, "p", 2)
33 n
, err
:= strconv
.Atoi64(a
[0])
37 e
, err1
:= strconv
.Atoi(a
[1])
39 println("bad e", a
[1])
43 // We expect that v*pow2(e) fits in a float64,
44 // but pow2(e) by itself may not. Be careful.
63 return v
* pow2(e
), true
65 f1
, err
:= strconv
.Atof64(s
)
72 // Wrapper around strconv.Atof32. Handles dddddp+ddd (binary exponent)
73 // itself, passes the rest on to strconv.Atof32.
74 func myatof32(s
string) (f
float32, ok
bool) {
75 a
:= strings
.Split(s
, "p", 2)
77 n
, err
:= strconv
.Atoi(a
[0])
79 println("bad n", a
[0])
82 e
, err1
:= strconv
.Atoi(a
[1])
84 println("bad p", a
[1])
87 return float32(float64(n
) * pow2(e
)), true
89 f1
, err1
:= strconv
.Atof32(s
)
96 func TestFp(t
*testing
.T
) {
97 f
, err
:= os
.Open("testfp.txt", os
.O_RDONLY
, 0)
99 t
.Fatal("testfp: open testfp.txt:", err
.String())
103 b
:= bufio
.NewReader(f
)
107 line
, err2
:= b
.ReadString('\n')
112 t
.Fatal("testfp: read testfp.txt: " + err2
.String())
114 line
= line
[0 : len(line
)-1]
116 if len(line
) == 0 || line
[0] == '#' {
119 a
:= strings
.Split(line
, " ", -1)
121 t
.Error("testfp.txt:", lineno
, ": wrong field count")
129 v
, ok
= myatof64(a
[2])
131 t
.Error("testfp.txt:", lineno
, ": cannot atof64 ", a
[2])
134 s
= fmt
.Sprintf(a
[1], v
)
136 v1
, ok
:= myatof32(a
[2])
138 t
.Error("testfp.txt:", lineno
, ": cannot atof32 ", a
[2])
141 s
= fmt
.Sprintf(a
[1], v1
)
145 t
.Error("testfp.txt:", lineno
, ": ", a
[0], " ", a
[1], " ", a
[2], " (", v
, ") ",
146 "want ", a
[3], " got ", s
)