blob: 7b91d88e51a56a66e833bd52d1b9f81d2714b62f [file] [log] [blame]
Robert Griesemerd2490e72008-03-19 15:45:07 -07001// $G $F.go && $L $F.$A && ./$A.out
2
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
Kai Backman36057e72010-07-20 15:53:16 +03009var bad bool
10
Russ Cox8fff9162010-10-25 21:25:13 -070011func pow10(pow int) float64 {
12 if pow < 0 {
13 return 1 / pow10(-pow)
Kai Backman36057e72010-07-20 15:53:16 +030014 }
Russ Cox8fff9162010-10-25 21:25:13 -070015 if pow > 0 {
16 return pow10(pow-1) * 10
17 }
Rob Pike4f61fc92010-09-04 10:36:13 +100018 return 1
Ken Thompsonb1a34632008-06-10 13:23:19 -070019}
20
Russ Cox8fff9162010-10-25 21:25:13 -070021func close(da float64, ia, ib int64, pow int) bool {
Rob Pike4f61fc92010-09-04 10:36:13 +100022 db := float64(ia) / float64(ib)
23 db *= pow10(pow)
Ken Thompsonb1a34632008-06-10 13:23:19 -070024
Russ Coxef46a9d2009-11-15 17:24:14 -080025 if da == 0 || db == 0 {
26 if da == 0 && db == 0 {
Rob Pike4f61fc92010-09-04 10:36:13 +100027 return true
Ken Thompsonad073b12008-06-08 16:16:17 -070028 }
Rob Pike4f61fc92010-09-04 10:36:13 +100029 return false
Ken Thompsonad073b12008-06-08 16:16:17 -070030 }
Ken Thompsonb1a34632008-06-10 13:23:19 -070031
Russ Cox8fff9162010-10-25 21:25:13 -070032 de := (da - db) / da
Ken Thompsonb1a34632008-06-10 13:23:19 -070033 if de < 0 {
Rob Pike4f61fc92010-09-04 10:36:13 +100034 de = -de
Ken Thompsonad073b12008-06-08 16:16:17 -070035 }
Ken Thompsonb1a34632008-06-10 13:23:19 -070036
Russ Cox8fff9162010-10-25 21:25:13 -070037 if de < 1e-14 {
Rob Pike4f61fc92010-09-04 10:36:13 +100038 return true
Ken Thompsonad073b12008-06-08 16:16:17 -070039 }
Kai Backman36057e72010-07-20 15:53:16 +030040 if !bad {
41 println("BUG")
42 bad = true
43 }
Rob Pike4f61fc92010-09-04 10:36:13 +100044 return false
Ken Thompsonad073b12008-06-08 16:16:17 -070045}
46
Russ Cox8fff9162010-10-25 21:25:13 -070047func main() {
48 if !close(0., 0, 1, 0) {
49 print("0. is ", 0., "\n")
50 }
51 if !close(+10., 10, 1, 0) {
52 print("+10. is ", +10., "\n")
53 }
54 if !close(-210., -210, 1, 0) {
55 print("-210. is ", -210., "\n")
56 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070057
Russ Cox8fff9162010-10-25 21:25:13 -070058 if !close(.0, 0, 1, 0) {
59 print(".0 is ", .0, "\n")
60 }
61 if !close(+.01, 1, 100, 0) {
62 print("+.01 is ", +.01, "\n")
63 }
64 if !close(-.012, -12, 1000, 0) {
65 print("-.012 is ", -.012, "\n")
66 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070067
Russ Cox8fff9162010-10-25 21:25:13 -070068 if !close(0.0, 0, 1, 0) {
69 print("0.0 is ", 0.0, "\n")
70 }
71 if !close(+10.01, 1001, 100, 0) {
72 print("+10.01 is ", +10.01, "\n")
73 }
74 if !close(-210.012, -210012, 1000, 0) {
75 print("-210.012 is ", -210.012, "\n")
76 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070077
Russ Cox8fff9162010-10-25 21:25:13 -070078 if !close(0E+1, 0, 1, 0) {
79 print("0E+1 is ", 0E+1, "\n")
80 }
81 if !close(+10e2, 10, 1, 2) {
82 print("+10e2 is ", +10e2, "\n")
83 }
84 if !close(-210e3, -210, 1, 3) {
85 print("-210e3 is ", -210e3, "\n")
86 }
Robert Griesemerd2490e72008-03-19 15:45:07 -070087
Russ Cox8fff9162010-10-25 21:25:13 -070088 if !close(0E-1, 0, 1, 0) {
89 print("0E-1 is ", 0E-1, "\n")
90 }
91 if !close(+0e23, 0, 1, 1) {
92 print("+0e23 is ", +0e23, "\n")
93 }
94 if !close(-0e345, 0, 1, 1) {
95 print("-0e345 is ", -0e345, "\n")
96 }
Ken Thompsonad073b12008-06-08 16:16:17 -070097
Russ Cox8fff9162010-10-25 21:25:13 -070098 if !close(0E1, 0, 1, 1) {
99 print("0E1 is ", 0E1, "\n")
100 }
101 if !close(+10e23, 10, 1, 23) {
102 print("+10e23 is ", +10e23, "\n")
103 }
104 if !close(-210e34, -210, 1, 34) {
105 print("-210e34 is ", -210e34, "\n")
106 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700107
Russ Cox8fff9162010-10-25 21:25:13 -0700108 if !close(0.E1, 0, 1, 1) {
109 print("0.E1 is ", 0.E1, "\n")
110 }
111 if !close(+10.e+2, 10, 1, 2) {
112 print("+10.e+2 is ", +10.e+2, "\n")
113 }
114 if !close(-210.e-3, -210, 1, -3) {
115 print("-210.e-3 is ", -210.e-3, "\n")
116 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700117
Russ Cox8fff9162010-10-25 21:25:13 -0700118 if !close(.0E1, 0, 1, 1) {
119 print(".0E1 is ", .0E1, "\n")
120 }
121 if !close(+.01e2, 1, 100, 2) {
122 print("+.01e2 is ", +.01e2, "\n")
123 }
124 if !close(-.012e3, -12, 1000, 3) {
125 print("-.012e3 is ", -.012e3, "\n")
126 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700127
Russ Cox8fff9162010-10-25 21:25:13 -0700128 if !close(0.0E1, 0, 1, 0) {
129 print("0.0E1 is ", 0.0E1, "\n")
130 }
131 if !close(+10.01e2, 1001, 100, 2) {
132 print("+10.01e2 is ", +10.01e2, "\n")
133 }
134 if !close(-210.012e3, -210012, 1000, 3) {
135 print("-210.012e3 is ", -210.012e3, "\n")
136 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700137
Russ Cox8fff9162010-10-25 21:25:13 -0700138 if !close(0.E+12, 0, 1, 0) {
139 print("0.E+12 is ", 0.E+12, "\n")
140 }
141 if !close(+10.e23, 10, 1, 23) {
142 print("+10.e23 is ", +10.e23, "\n")
143 }
144 if !close(-210.e33, -210, 1, 33) {
145 print("-210.e33 is ", -210.e33, "\n")
146 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700147
Russ Cox8fff9162010-10-25 21:25:13 -0700148 if !close(.0E-12, 0, 1, 0) {
149 print(".0E-12 is ", .0E-12, "\n")
150 }
151 if !close(+.01e23, 1, 100, 23) {
152 print("+.01e23 is ", +.01e23, "\n")
153 }
154 if !close(-.012e34, -12, 1000, 34) {
155 print("-.012e34 is ", -.012e34, "\n")
156 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700157
Russ Cox8fff9162010-10-25 21:25:13 -0700158 if !close(0.0E12, 0, 1, 12) {
159 print("0.0E12 is ", 0.0E12, "\n")
160 }
161 if !close(+10.01e23, 1001, 100, 23) {
162 print("+10.01e23 is ", +10.01e23, "\n")
163 }
164 if !close(-210.012e33, -210012, 1000, 33) {
165 print("-210.012e33 is ", -210.012e33, "\n")
166 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700167
Russ Cox8fff9162010-10-25 21:25:13 -0700168 if !close(0.E123, 0, 1, 123) {
169 print("0.E123 is ", 0.E123, "\n")
170 }
171 if !close(+10.e+23, 10, 1, 23) {
172 print("+10.e+234 is ", +10.e+234, "\n")
173 }
174 if !close(-210.e-35, -210, 1, -35) {
175 print("-210.e-35 is ", -210.e-35, "\n")
176 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700177
Russ Cox8fff9162010-10-25 21:25:13 -0700178 if !close(.0E123, 0, 1, 123) {
179 print(".0E123 is ", .0E123, "\n")
180 }
181 if !close(+.01e29, 1, 100, 29) {
182 print("+.01e29 is ", +.01e29, "\n")
183 }
184 if !close(-.012e29, -12, 1000, 29) {
185 print("-.012e29 is ", -.012e29, "\n")
186 }
Ken Thompsonad073b12008-06-08 16:16:17 -0700187
Russ Cox8fff9162010-10-25 21:25:13 -0700188 if !close(0.0E123, 0, 1, 123) {
189 print("0.0E123 is ", 0.0E123, "\n")
190 }
191 if !close(+10.01e31, 1001, 100, 31) {
192 print("+10.01e31 is ", +10.01e31, "\n")
193 }
194 if !close(-210.012e19, -210012, 1000, 19) {
195 print("-210.012e19 is ", -210.012e19, "\n")
196 }
Robert Griesemerd2490e72008-03-19 15:45:07 -0700197}