blob: 58355e53de951479be053f51811be84e68540a38 [file] [log] [blame]
Rémy Oudompheng2ece2f52012-02-18 22:15:42 +01001// run
Russ Coxf333f462008-11-17 12:33:49 -08002
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package main
8
Russ Cox918afd942009-05-08 15:21:41 -07009import "os"
Rob Pike325cf8e2010-03-24 16:46:53 -070010import "strconv"
Russ Coxf333f462008-11-17 12:33:49 -080011
Russ Cox839a6842009-01-20 14:40:40 -080012type Test struct {
Rob Pike325cf8e2010-03-24 16:46:53 -070013 f float64
14 in string
15 out string
Russ Coxf333f462008-11-17 12:33:49 -080016}
17
Rob Pike325cf8e2010-03-24 16:46:53 -070018var 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"},
Russ Coxa1585b62008-11-17 13:58:45 -080024
25 // "x" = the floating point value from converting the string x.
26 // These are exactly representable in 64-bit floating point:
27 // 1e23-8388608
28 // 1e23+8388608
29 // The former has an even mantissa, so "1e23" rounds to 1e23-8388608.
30 // If "1e23+8388608" is implemented as "1e23" + "8388608",
31 // that ends up computing 1e23-8388608 + 8388608 = 1e23,
32 // which rounds back to 1e23-8388608.
33 // The correct answer, of course, would be "1e23+8388608" = 1e23+8388608.
34 // This is not going to be correct until 6g has multiprecision floating point.
35 // A simpler case is "1e23+1", which should also round to 1e23+8388608.
Rob Pike325cf8e2010-03-24 16:46:53 -070036 Test{1e23 + 8.388608e6, "1e23+8.388608e6", "1.0000000000000001e+23"},
37 Test{1e23 + 1, "1e23+1", "1.0000000000000001e+23"},
Russ Coxbe2edb52009-03-03 08:39:12 -080038}
Russ Coxf333f462008-11-17 12:33:49 -080039
40func main() {
Rob Pike325cf8e2010-03-24 16:46:53 -070041 ok := true
Russ Coxf333f462008-11-17 12:33:49 -080042 for i := 0; i < len(tests); i++ {
Rob Pike325cf8e2010-03-24 16:46:53 -070043 t := tests[i]
Russ Cox2666b812011-12-05 15:48:46 -050044 v := strconv.FormatFloat(t.f, 'g', -1, 64)
Russ Coxf333f462008-11-17 12:33:49 -080045 if v != t.out {
Rob Pike325cf8e2010-03-24 16:46:53 -070046 println("Bad float64 const:", t.in, "want", t.out, "got", v)
Russ Cox2666b812011-12-05 15:48:46 -050047 x, err := strconv.ParseFloat(t.out, 64)
Russ Cox6cc001c2008-11-18 17:12:07 -080048 if err != nil {
Rob Pike325cf8e2010-03-24 16:46:53 -070049 println("bug120: strconv.Atof64", t.out)
50 panic("fail")
Russ Coxa1585b62008-11-17 13:58:45 -080051 }
Russ Cox2666b812011-12-05 15:48:46 -050052 println("\twant exact:", strconv.FormatFloat(x, 'g', 1000, 64))
53 println("\tgot exact: ", strconv.FormatFloat(t.f, 'g', 1000, 64))
Rob Pike325cf8e2010-03-24 16:46:53 -070054 ok = false
Russ Coxf333f462008-11-17 12:33:49 -080055 }
56 }
57 if !ok {
Rob Pike325cf8e2010-03-24 16:46:53 -070058 os.Exit(1)
Russ Coxf333f462008-11-17 12:33:49 -080059 }
60}