1
2
3
4
5
6
7 package main
8
9 import "os"
10 import "strconv"
11
12 type Test struct {
13 f float64
14 in string
15 out string
16 }
17
18 var tests = []Test{
19 Test{123.5, "123.5", "123.5"},
20 Test{456.7, "456.7", "456.7"},
21 Test{1e23 + 8.5e6, "1e23+8.5e6", "1.0000000000000001e+23"},
22 Test{100000000000000008388608, "100000000000000008388608", "1.0000000000000001e+23"},
23 Test{1e23 + 8388609, "1e23+8388609", "1.0000000000000001e+23"},
24
25
26
27
28
29
30
31
32
33
34
35
36 Test{1e23 + 8.388608e6, "1e23+8.388608e6", "1.0000000000000001e+23"},
37 Test{1e23 + 1, "1e23+1", "1.0000000000000001e+23"},
38 }
39
40 func main() {
41 ok := true
42 for i := 0; i < len(tests); i++ {
43 t := tests[i]
44 v := strconv.FormatFloat(t.f, 'g', -1, 64)
45 if v != t.out {
46 println("Bad float64 const:", t.in, "want", t.out, "got", v)
47 x, err := strconv.ParseFloat(t.out, 64)
48 if err != nil {
49 println("bug120: strconv.Atof64", t.out)
50 panic("fail")
51 }
52 println("\twant exact:", strconv.FormatFloat(x, 'g', 1000, 64))
53 println("\tgot exact: ", strconv.FormatFloat(t.f, 'g', 1000, 64))
54 ok = false
55 }
56 }
57 if !ok {
58 os.Exit(1)
59 }
60 }
61
View as plain text