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.
17 func pow2(i int) float64 {
26 return pow2(i/2) * pow2(i-i/2)
29 // Wrapper around strconv.ParseFloat(x, 64). Handles dddddp+ddd (binary exponent)
30 // itself, passes the rest on to strconv.ParseFloat.
31 func myatof64(s string) (f float64, ok bool) {
32 a := strings.SplitN(s, "p", 2)
34 n, err := strconv.ParseInt(a[0], 10, 64)
38 e, err1 := strconv.Atoi(a[1])
40 println("bad e", a[1])
44 // We expect that v*pow2(e) fits in a float64,
45 // but pow2(e) by itself may not. Be careful.
64 return v * pow2(e), true
66 f1, err := strconv.ParseFloat(s, 64)
73 // Wrapper around strconv.ParseFloat(x, 32). Handles dddddp+ddd (binary exponent)
74 // itself, passes the rest on to strconv.ParseFloat.
75 func myatof32(s string) (f float32, ok bool) {
76 a := strings.SplitN(s, "p", 2)
78 n, err := strconv.Atoi(a[0])
80 println("bad n", a[0])
83 e, err1 := strconv.Atoi(a[1])
85 println("bad p", a[1])
88 return float32(float64(n) * pow2(e)), true
90 f64, err1 := strconv.ParseFloat(s, 32)
98 func TestFp(t *testing.T) {
99 f, err := os.Open("testdata/testfp.txt")
101 t.Fatal("testfp: open testdata/testfp.txt:", err)
105 b := bufio.NewReader(f)
109 line, err2 := b.ReadString('\n')
114 t.Fatal("testfp: read testdata/testfp.txt: " + err2.Error())
116 line = line[0 : len(line)-1]
118 if len(line) == 0 || line[0] == '#' {
121 a := strings.Split(line, " ")
123 t.Error("testdata/testfp.txt:", lineno, ": wrong field count")
131 v, ok = myatof64(a[2])
133 t.Error("testdata/testfp.txt:", lineno, ": cannot atof64 ", a[2])
136 s = fmt.Sprintf(a[1], v)
138 v1, ok := myatof32(a[2])
140 t.Error("testdata/testfp.txt:", lineno, ": cannot atof32 ", a[2])
143 s = fmt.Sprintf(a[1], v1)
147 t.Error("testdata/testfp.txt:", lineno, ": ", a[0], " ", a[1], " ", a[2], " (", v, ") ",
148 "want ", a[3], " got ", s)