2008-11-17 13:34:03 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-08-12 14:18:37 -06:00
|
|
|
package strconv_test
|
2009-10-07 12:55:06 -06:00
|
|
|
|
2008-11-17 13:34:03 -07:00
|
|
|
import (
|
2009-12-15 16:40:16 -07:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2008-11-17 13:34:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func pow2(i int) float64 {
|
|
|
|
switch {
|
|
|
|
case i < 0:
|
2009-11-09 22:23:52 -07:00
|
|
|
return 1 / pow2(-i)
|
2008-11-17 13:34:03 -07:00
|
|
|
case i == 0:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 1
|
2008-11-17 13:34:03 -07:00
|
|
|
case i == 1:
|
2009-11-09 13:07:39 -07:00
|
|
|
return 2
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return pow2(i/2) * pow2(i-i/2)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2012-01-14 11:59:45 -07:00
|
|
|
// Wrapper around strconv.ParseFloat(x, 64). Handles dddddp+ddd (binary exponent)
|
|
|
|
// itself, passes the rest on to strconv.ParseFloat.
|
2008-11-18 17:13:25 -07:00
|
|
|
func myatof64(s string) (f float64, ok bool) {
|
2011-06-27 17:43:14 -06:00
|
|
|
a := strings.SplitN(s, "p", 2)
|
2008-11-17 13:34:03 -07:00
|
|
|
if len(a) == 2 {
|
2011-12-05 13:48:21 -07:00
|
|
|
n, err := strconv.ParseInt(a[0], 10, 64)
|
2008-11-18 17:13:25 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, false
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
e, err1 := strconv.Atoi(a[1])
|
2008-11-18 17:13:25 -07:00
|
|
|
if err1 != nil {
|
2009-12-15 16:40:16 -07:00
|
|
|
println("bad e", a[1])
|
|
|
|
return 0, false
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
v := float64(n)
|
2008-11-17 13:34:03 -07:00
|
|
|
// We expect that v*pow2(e) fits in a float64,
|
2016-03-01 16:21:55 -07:00
|
|
|
// but pow2(e) by itself may not. Be careful.
|
2008-11-17 13:34:03 -07:00
|
|
|
if e <= -1000 {
|
2009-12-15 16:40:16 -07:00
|
|
|
v *= pow2(-1000)
|
|
|
|
e += 1000
|
2008-11-17 13:34:03 -07:00
|
|
|
for e < 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
v /= 2
|
|
|
|
e++
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return v, true
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
if e >= 1000 {
|
2009-12-15 16:40:16 -07:00
|
|
|
v *= pow2(1000)
|
|
|
|
e -= 1000
|
2008-11-17 13:34:03 -07:00
|
|
|
for e > 0 {
|
2009-12-15 16:40:16 -07:00
|
|
|
v *= 2
|
|
|
|
e--
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return v, true
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return v * pow2(e), true
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2011-12-05 13:48:21 -07:00
|
|
|
f1, err := strconv.ParseFloat(s, 64)
|
2008-11-18 17:13:25 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, false
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return f1, true
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2012-01-14 11:59:45 -07:00
|
|
|
// Wrapper around strconv.ParseFloat(x, 32). Handles dddddp+ddd (binary exponent)
|
|
|
|
// itself, passes the rest on to strconv.ParseFloat.
|
2008-11-18 17:13:25 -07:00
|
|
|
func myatof32(s string) (f float32, ok bool) {
|
2011-06-27 17:43:14 -06:00
|
|
|
a := strings.SplitN(s, "p", 2)
|
2008-11-17 13:34:03 -07:00
|
|
|
if len(a) == 2 {
|
2009-12-15 16:40:16 -07:00
|
|
|
n, err := strconv.Atoi(a[0])
|
2008-11-18 17:13:25 -07:00
|
|
|
if err != nil {
|
2009-12-15 16:40:16 -07:00
|
|
|
println("bad n", a[0])
|
|
|
|
return 0, false
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
e, err1 := strconv.Atoi(a[1])
|
2008-11-18 17:13:25 -07:00
|
|
|
if err1 != nil {
|
2009-12-15 16:40:16 -07:00
|
|
|
println("bad p", a[1])
|
|
|
|
return 0, false
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return float32(float64(n) * pow2(e)), true
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2011-12-05 13:48:21 -07:00
|
|
|
f64, err1 := strconv.ParseFloat(s, 32)
|
|
|
|
f1 := float32(f64)
|
2008-11-18 17:13:25 -07:00
|
|
|
if err1 != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, false
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
return f1, true
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestFp(t *testing.T) {
|
2013-01-22 14:44:35 -07:00
|
|
|
f, err := os.Open("testdata/testfp.txt")
|
2008-11-17 13:34:03 -07:00
|
|
|
if err != nil {
|
2013-01-22 14:44:35 -07:00
|
|
|
t.Fatal("testfp: open testdata/testfp.txt:", err)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
defer f.Close()
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2013-02-20 14:38:19 -07:00
|
|
|
s := bufio.NewScanner(f)
|
2008-11-17 13:34:03 -07:00
|
|
|
|
2013-02-20 14:38:19 -07:00
|
|
|
for lineno := 1; s.Scan(); lineno++ {
|
|
|
|
line := s.Text()
|
2008-11-17 13:34:03 -07:00
|
|
|
if len(line) == 0 || line[0] == '#' {
|
2009-11-09 13:07:39 -07:00
|
|
|
continue
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2011-06-27 17:43:14 -06:00
|
|
|
a := strings.Split(line, " ")
|
2008-11-17 13:34:03 -07:00
|
|
|
if len(a) != 4 {
|
2013-01-22 14:44:35 -07:00
|
|
|
t.Error("testdata/testfp.txt:", lineno, ": wrong field count")
|
2009-12-15 16:40:16 -07:00
|
|
|
continue
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
var s string
|
|
|
|
var v float64
|
2008-11-17 13:34:03 -07:00
|
|
|
switch a[0] {
|
|
|
|
case "float64":
|
2009-12-15 16:40:16 -07:00
|
|
|
var ok bool
|
|
|
|
v, ok = myatof64(a[2])
|
2008-11-17 13:34:03 -07:00
|
|
|
if !ok {
|
2013-01-22 14:44:35 -07:00
|
|
|
t.Error("testdata/testfp.txt:", lineno, ": cannot atof64 ", a[2])
|
2009-12-15 16:40:16 -07:00
|
|
|
continue
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
s = fmt.Sprintf(a[1], v)
|
2008-11-17 13:34:03 -07:00
|
|
|
case "float32":
|
2009-12-15 16:40:16 -07:00
|
|
|
v1, ok := myatof32(a[2])
|
2008-11-17 13:34:03 -07:00
|
|
|
if !ok {
|
2013-01-22 14:44:35 -07:00
|
|
|
t.Error("testdata/testfp.txt:", lineno, ": cannot atof32 ", a[2])
|
2009-12-15 16:40:16 -07:00
|
|
|
continue
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
2009-12-15 16:40:16 -07:00
|
|
|
s = fmt.Sprintf(a[1], v1)
|
|
|
|
v = float64(v1)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
if s != a[3] {
|
2013-01-22 14:44:35 -07:00
|
|
|
t.Error("testdata/testfp.txt:", lineno, ": ", a[0], " ", a[1], " ", a[2], " (", v, ") ",
|
2009-11-09 13:07:39 -07:00
|
|
|
"want ", a[3], " got ", s)
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|
|
|
|
}
|
2013-02-20 14:38:19 -07:00
|
|
|
if s.Err() != nil {
|
|
|
|
t.Fatal("testfp: read testdata/testfp.txt: ", s.Err())
|
|
|
|
}
|
2008-11-17 13:34:03 -07:00
|
|
|
}
|