diff options
| author | Russ Cox <rsc@golang.org> | 2008-11-17 12:34:03 -0800 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2008-11-17 12:34:03 -0800 |
| commit | 079c00a475d11f71a69fe848dd67e8fe34ac88a8 (patch) | |
| tree | aeb58cc993873dadfe1a2adbc63521879b2dd66a /src/lib/strconv/testfp.go | |
| parent | f333f4685cc667dda0be6ecd5500ff8fa10f4a2a (diff) | |
| download | go-079c00a475d11f71a69fe848dd67e8fe34ac88a8.tar.xz | |
correctly rounded floating-point conversions
in new package strconv.
move atoi etc to strconv too.
update fmt, etc to use strconv.
R=r
DELTA=2232 (1691 added, 424 deleted, 117 changed)
OCL=19286
CL=19380
Diffstat (limited to 'src/lib/strconv/testfp.go')
| -rw-r--r-- | src/lib/strconv/testfp.go | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/src/lib/strconv/testfp.go b/src/lib/strconv/testfp.go new file mode 100644 index 0000000000..65428b9777 --- /dev/null +++ b/src/lib/strconv/testfp.go @@ -0,0 +1,156 @@ +// 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. + +package main + +import ( + "bufio"; + "fmt"; + "os"; + "strconv"; + "strings"; +) + +func pow2(i int) float64 { + switch { + case i < 0: + return 1 / pow2(-i); + case i == 0: + return 1; + case i == 1: + return 2; + } + return pow2(i/2) * pow2(i-i/2); +} + +// Wrapper around strconv.atof64. Handles dddddp+ddd (binary exponent) +// itself, passes the rest on to strconv.atof64. +func atof64(s string) (f float64, ok bool) { + a := strings.split(s, "p"); + if len(a) == 2 { + n, ok := strconv.atoi64(a[0]); + if !ok { + return 0, false; + } + e, ok1 := strconv.atoi(a[1]); + if !ok1 { + println("bad e", a[1]); + return 0, false; + } + v := float64(n); + // We expect that v*pow2(e) fits in a float64, + // but pow2(e) by itself may not. Be careful. + if e <= -1000 { + v *= pow2(-1000); + e += 1000; + for e < 0 { + v /= 2; + e++; + } + return v, true; + } + if e >= 1000 { + v *= pow2(1000); + e -= 1000; + for e > 0 { + v *= 2; + e--; + } + return v, true; + } + return v*pow2(e), true; + } + f1, overflow, ok1 := strconv.atof64(s); + if !ok1 { + return 0, false; + } + return f1, true; +} + +// Wrapper around strconv.atof32. Handles dddddp+ddd (binary exponent) +// itself, passes the rest on to strconv.atof32. +func atof32(s string) (f float32, ok bool) { + a := strings.split(s, "p"); + if len(a) == 2 { + n, ok := strconv.atoi(a[0]); + if !ok { + println("bad n", a[0]); + return 0, false; + } + e, ok1 := strconv.atoi(a[1]); + if !ok1 { + println("bad p", a[1]); + return 0, false; + } + return float32(float64(n)*pow2(e)), true; + } + f1, overflow, ok1 := strconv.atof32(s); + if !ok1 { + return 0, false; + } + return f1, true; +} + +func main() +{ + fd, err := os.Open("testfp.txt", os.O_RDONLY, 0); + if err != nil { + panicln("testfp: open testfp.txt:", err.String()); + } + + b, err1 := bufio.NewBufRead(fd); + if err1 != nil { + panicln("testfp NewBufRead:", err1.String()); + } + + lineno := 0; + ok := true; + for { + line, err2 := b.ReadLineString('\n', false); + if err2 == bufio.EndOfFile { + break; + } + if err2 != nil { + panicln("testfp: read testfp.txt:", err2.String()); + } + lineno++; + if len(line) == 0 || line[0] == '#' { + continue + } + a := strings.split(line, " "); + if len(a) != 4 { + print("testfp.txt:", lineno, ": wrong field count\n"); + continue; + } + var s string; + var v float64; + switch a[0] { + case "float64": + var ok bool; + v, ok = atof64(a[2]); + if !ok { + print("testfp.txt:", lineno, ": cannot atof64 ", a[2]); + continue; + } + s = fmt.sprintf(a[1], v); + case "float32": + v1, ok := atof32(a[2]); + if !ok { + print("testfp.txt:", lineno, ": cannot atof32 ", a[2]); + continue; + } + s = fmt.sprintf(a[1], v1); + v = float64(v1); + } + if s != a[3] { + print("testfp.txt:", lineno, ": ", a[0], " ", a[1], " ", a[2], " (", v, ") ", + "want ", a[3], " got ", s, "\n"); + ok = false; + } +//else print("testfp.txt:", lineno, ": worked! ", s, "\n"); + } + if !ok { + panicln("testfp failed"); + } +} |
