2012-02-18 21:15:42 +00:00
|
|
|
// run
|
2008-11-17 20:33:49 +00: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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2009-05-08 22:21:41 +00:00
|
|
|
import "os"
|
2010-03-24 23:46:53 +00:00
|
|
|
import "strconv"
|
2008-11-17 20:33:49 +00:00
|
|
|
|
2009-01-20 22:40:40 +00:00
|
|
|
type Test struct {
|
2010-03-24 23:46:53 +00:00
|
|
|
f float64
|
|
|
|
in string
|
|
|
|
out string
|
2008-11-17 20:33:49 +00:00
|
|
|
}
|
|
|
|
|
2010-03-24 23:46:53 +00:00
|
|
|
var tests = []Test{
|
|
|
|
Test{123.5, "123.5", "123.5"},
|
|
|
|
Test{456.7, "456.7", "456.7"},
|
|
|
|
Test{1e23 + 8.5e6, "1e23+8.5e6", "1.0000000000000001e+23"},
|
|
|
|
Test{100000000000000008388608, "100000000000000008388608", "1.0000000000000001e+23"},
|
|
|
|
Test{1e23 + 8388609, "1e23+8388609", "1.0000000000000001e+23"},
|
2008-11-17 21:58:45 +00:00
|
|
|
|
|
|
|
// "x" = the floating point value from converting the string x.
|
|
|
|
// These are exactly representable in 64-bit floating point:
|
|
|
|
// 1e23-8388608
|
|
|
|
// 1e23+8388608
|
|
|
|
// The former has an even mantissa, so "1e23" rounds to 1e23-8388608.
|
|
|
|
// If "1e23+8388608" is implemented as "1e23" + "8388608",
|
|
|
|
// that ends up computing 1e23-8388608 + 8388608 = 1e23,
|
|
|
|
// which rounds back to 1e23-8388608.
|
|
|
|
// The correct answer, of course, would be "1e23+8388608" = 1e23+8388608.
|
|
|
|
// This is not going to be correct until 6g has multiprecision floating point.
|
|
|
|
// A simpler case is "1e23+1", which should also round to 1e23+8388608.
|
2010-03-24 23:46:53 +00:00
|
|
|
Test{1e23 + 8.388608e6, "1e23+8.388608e6", "1.0000000000000001e+23"},
|
|
|
|
Test{1e23 + 1, "1e23+1", "1.0000000000000001e+23"},
|
2009-03-03 16:39:12 +00:00
|
|
|
}
|
2008-11-17 20:33:49 +00:00
|
|
|
|
|
|
|
func main() {
|
2010-03-24 23:46:53 +00:00
|
|
|
ok := true
|
2008-11-17 20:33:49 +00:00
|
|
|
for i := 0; i < len(tests); i++ {
|
2010-03-24 23:46:53 +00:00
|
|
|
t := tests[i]
|
2011-12-05 20:48:46 +00:00
|
|
|
v := strconv.FormatFloat(t.f, 'g', -1, 64)
|
2008-11-17 20:33:49 +00:00
|
|
|
if v != t.out {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("Bad float64 const:", t.in, "want", t.out, "got", v)
|
2011-12-05 20:48:46 +00:00
|
|
|
x, err := strconv.ParseFloat(t.out, 64)
|
2008-11-19 01:12:07 +00:00
|
|
|
if err != nil {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("bug120: strconv.Atof64", t.out)
|
|
|
|
panic("fail")
|
2008-11-17 21:58:45 +00:00
|
|
|
}
|
2011-12-05 20:48:46 +00:00
|
|
|
println("\twant exact:", strconv.FormatFloat(x, 'g', 1000, 64))
|
|
|
|
println("\tgot exact: ", strconv.FormatFloat(t.f, 'g', 1000, 64))
|
2010-03-24 23:46:53 +00:00
|
|
|
ok = false
|
2008-11-17 20:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
2010-03-24 23:46:53 +00:00
|
|
|
os.Exit(1)
|
2008-11-17 20:33:49 +00:00
|
|
|
}
|
|
|
|
}
|