blob: 4efae2362da98b9ddbf0e3da70c55f698cbd58bf [file] [log] [blame]
Russ Coxd2cc9882012-02-16 23:50:37 -05001// run
Robert Griesemerd2490e72008-03-19 15:45:07 -07002
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
Rob Pike83976e32012-02-19 14:28:53 +11007// Test floating-point literal syntax.
8
Robert Griesemerd2490e72008-03-19 15:45:07 -07009package main
10
Kai Backman36057e72010-07-20 15:53:16 +030011var bad bool
12
Russ Cox8fff9162010-10-25 21:25:13 -070013func pow10(pow int) float64 {
14 if pow < 0 {
15 return 1 / pow10(-pow)
Kai Backman36057e72010-07-20 15:53:16 +030016 }
Russ Cox8fff9162010-10-25 21:25:13 -070017 if pow > 0 {
18 return pow10(pow-1) * 10
19 }
Rob Pike4f61fc92010-09-04 10:36:13 +100020 return 1
Ken Thompsonb1a34632008-06-10 13:23:19 -070021}
22
Russ Cox8fff9162010-10-25 21:25:13 -070023func close(da float64, ia, ib int64, pow int) bool {
Rob Pike4f61fc92010-09-04 10:36:13 +100024 db := float64(ia) / float64(ib)
25 db *= pow10(pow)
Ken Thompsonb1a34632008-06-10 13:23:19 -070026
Russ Coxef46a9d2009-11-15 17:24:14 -080027 if da == 0 || db == 0 {
28 if da == 0 && db == 0 {
Rob Pike4f61fc92010-09-04 10:36:13 +100029 return true
Ken Thompsonad073b12008-06-08 16:16:17 -070030 }
Rob Pike4f61fc92010-09-04 10:36:13 +100031 return false
Ken Thompsonad073b12008-06-08 16:16:17 -070032 }
Ken Thompsonb1a34632008-06-10 13:23:19 -070033
Russ Cox8fff9162010-10-25 21:25:13 -070034 de := (da - db) / da
Ken Thompsonb1a34632008-06-10 13:23:19 -070035 if de < 0 {
Rob Pike4f61fc92010-09-04 10:36:13 +100036 de = -de
Ken Thompsonad073b12008-06-08 16:16:17 -070037 }
Ken Thompsonb1a34632008-06-10 13:23:19 -070038
Russ Cox8fff9162010-10-25 21:25:13 -070039 if de < 1e-14 {
Rob Pike4f61fc92010-09-04 10:36:13 +100040 return true
Ken Thompsonad073b12008-06-08 16:16:17 -070041 }
Kai Backman36057e72010-07-20 15:53:16 +030042 if !bad {
43 println("BUG")
44 bad = true
45 }
Rob Pike4f61fc92010-09-04 10:36:13 +100046 return false
Ken Thompsonad073b12008-06-08 16:16:17 -070047}
48
Russ Cox8fff9162010-10-25 21:25:13 -070049func main() {
50 if !close(0., 0, 1, 0) {
51 print("0. is ", 0., "\n")
52 }
53 if !close(+10., 10, 1, 0) {
54 print("+10. is ", +10., "\n")
55 }
56 if !close(-210., -210, 1, 0) {
57 print("-210. is ", -210., "\n")
58 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070059
Russ Cox8fff9162010-10-25 21:25:13 -070060 if !close(.0, 0, 1, 0) {
61 print(".0 is ", .0, "\n")
62 }
63 if !close(+.01, 1, 100, 0) {
64 print("+.01 is ", +.01, "\n")
65 }
66 if !close(-.012, -12, 1000, 0) {
67 print("-.012 is ", -.012, "\n")
68 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070069
Russ Cox8fff9162010-10-25 21:25:13 -070070 if !close(0.0, 0, 1, 0) {
71 print("0.0 is ", 0.0, "\n")
72 }
73 if !close(+10.01, 1001, 100, 0) {
74 print("+10.01 is ", +10.01, "\n")
75 }
76 if !close(-210.012, -210012, 1000, 0) {
77 print("-210.012 is ", -210.012, "\n")
78 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070079
Russ Cox8fff9162010-10-25 21:25:13 -070080 if !close(0E+1, 0, 1, 0) {
81 print("0E+1 is ", 0E+1, "\n")
82 }
83 if !close(+10e2, 10, 1, 2) {
84 print("+10e2 is ", +10e2, "\n")
85 }
86 if !close(-210e3, -210, 1, 3) {
87 print("-210e3 is ", -210e3, "\n")
88 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070089
Russ Cox8fff9162010-10-25 21:25:13 -070090 if !close(0E-1, 0, 1, 0) {
91 print("0E-1 is ", 0E-1, "\n")
92 }
93 if !close(+0e23, 0, 1, 1) {
94 print("+0e23 is ", +0e23, "\n")
95 }
96 if !close(-0e345, 0, 1, 1) {
97 print("-0e345 is ", -0e345, "\n")
98 }
Ken Thompsonad073b12008-06-08 16:16:17 -070099
Russ Cox8fff9162010-10-25 21:25:13 -0700100 if !close(0E1, 0, 1, 1) {
101 print("0E1 is ", 0E1, "\n")
102 }
103 if !close(+10e23, 10, 1, 23) {
104 print("+10e23 is ", +10e23, "\n")
105 }
106 if !close(-210e34, -210, 1, 34) {
107 print("-210e34 is ", -210e34, "\n")
108 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700109
Russ Cox8fff9162010-10-25 21:25:13 -0700110 if !close(0.E1, 0, 1, 1) {
111 print("0.E1 is ", 0.E1, "\n")
112 }
113 if !close(+10.e+2, 10, 1, 2) {
114 print("+10.e+2 is ", +10.e+2, "\n")
115 }
116 if !close(-210.e-3, -210, 1, -3) {
117 print("-210.e-3 is ", -210.e-3, "\n")
118 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700119
Russ Cox8fff9162010-10-25 21:25:13 -0700120 if !close(.0E1, 0, 1, 1) {
121 print(".0E1 is ", .0E1, "\n")
122 }
123 if !close(+.01e2, 1, 100, 2) {
124 print("+.01e2 is ", +.01e2, "\n")
125 }
126 if !close(-.012e3, -12, 1000, 3) {
127 print("-.012e3 is ", -.012e3, "\n")
128 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700129
Russ Cox8fff9162010-10-25 21:25:13 -0700130 if !close(0.0E1, 0, 1, 0) {
131 print("0.0E1 is ", 0.0E1, "\n")
132 }
133 if !close(+10.01e2, 1001, 100, 2) {
134 print("+10.01e2 is ", +10.01e2, "\n")
135 }
136 if !close(-210.012e3, -210012, 1000, 3) {
137 print("-210.012e3 is ", -210.012e3, "\n")
138 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700139
Russ Cox8fff9162010-10-25 21:25:13 -0700140 if !close(0.E+12, 0, 1, 0) {
141 print("0.E+12 is ", 0.E+12, "\n")
142 }
143 if !close(+10.e23, 10, 1, 23) {
144 print("+10.e23 is ", +10.e23, "\n")
145 }
146 if !close(-210.e33, -210, 1, 33) {
147 print("-210.e33 is ", -210.e33, "\n")
148 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700149
Russ Cox8fff9162010-10-25 21:25:13 -0700150 if !close(.0E-12, 0, 1, 0) {
151 print(".0E-12 is ", .0E-12, "\n")
152 }
153 if !close(+.01e23, 1, 100, 23) {
154 print("+.01e23 is ", +.01e23, "\n")
155 }
156 if !close(-.012e34, -12, 1000, 34) {
157 print("-.012e34 is ", -.012e34, "\n")
158 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700159
Russ Cox8fff9162010-10-25 21:25:13 -0700160 if !close(0.0E12, 0, 1, 12) {
161 print("0.0E12 is ", 0.0E12, "\n")
162 }
163 if !close(+10.01e23, 1001, 100, 23) {
164 print("+10.01e23 is ", +10.01e23, "\n")
165 }
166 if !close(-210.012e33, -210012, 1000, 33) {
167 print("-210.012e33 is ", -210.012e33, "\n")
168 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700169
Russ Cox8fff9162010-10-25 21:25:13 -0700170 if !close(0.E123, 0, 1, 123) {
171 print("0.E123 is ", 0.E123, "\n")
172 }
173 if !close(+10.e+23, 10, 1, 23) {
174 print("+10.e+234 is ", +10.e+234, "\n")
175 }
176 if !close(-210.e-35, -210, 1, -35) {
177 print("-210.e-35 is ", -210.e-35, "\n")
178 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700179
Russ Cox8fff9162010-10-25 21:25:13 -0700180 if !close(.0E123, 0, 1, 123) {
181 print(".0E123 is ", .0E123, "\n")
182 }
183 if !close(+.01e29, 1, 100, 29) {
184 print("+.01e29 is ", +.01e29, "\n")
185 }
186 if !close(-.012e29, -12, 1000, 29) {
187 print("-.012e29 is ", -.012e29, "\n")
188 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700189
Russ Cox8fff9162010-10-25 21:25:13 -0700190 if !close(0.0E123, 0, 1, 123) {
191 print("0.0E123 is ", 0.0E123, "\n")
192 }
193 if !close(+10.01e31, 1001, 100, 31) {
194 print("+10.01e31 is ", +10.01e31, "\n")
195 }
196 if !close(-210.012e19, -210012, 1000, 19) {
197 print("-210.012e19 is ", -210.012e19, "\n")
198 }
Alan Donovan052c9422013-02-12 13:17:49 -0500199
200 if bad {
201 panic("float_lit")
202 }
Robert Griesemerd2490e72008-03-19 15:45:07 -0700203}