/
atan2.t
341 lines (277 loc) · 10.9 KB
/
atan2.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# WARNING:
# This is a generated file and should not be edited directly.
# look into generate-tests.pl instead
use v6;
use Test;
sub degrees-to-radians($x) {
$x * (312689/99532) / 180;
}
my @sines = (
degrees-to-radians(-360) => 0,
degrees-to-radians(135 - 360) => 1/2*sqrt(2),
degrees-to-radians(330 - 360) => -0.5,
degrees-to-radians(0) => 0,
degrees-to-radians(30) => 0.5,
degrees-to-radians(45) => 1/2*sqrt(2),
degrees-to-radians(90) => 1,
degrees-to-radians(135) => 1/2*sqrt(2),
degrees-to-radians(180) => 0,
degrees-to-radians(225) => -1/2*sqrt(2),
degrees-to-radians(270) => -1,
degrees-to-radians(315) => -1/2*sqrt(2),
degrees-to-radians(360) => 0,
degrees-to-radians(30 + 360) => 0.5,
degrees-to-radians(225 + 360) => -1/2*sqrt(2),
degrees-to-radians(720) => 0
);
my @cosines = @sines.map({; $_.key - degrees-to-radians(90) => $_.value }); #OK
my @sinhes = @sines.grep({ $_.key < degrees-to-radians(500) }).map({; $_.key => #OK
(exp($_.key) - exp(-$_.key)) / 2.0 });
my @coshes = @sines.grep({ $_.key < degrees-to-radians(500) }).map({; $_.key => #OK
(exp($_.key) + exp(-$_.key)) / 2.0 });
class NotComplex is Cool {
has $.value;
multi method new(Complex $value is copy) {
self.bless(*, :$value);
}
multi method Numeric() {
self.value;
}
}
class DifferentReal is Real {
has $.value;
multi method new($value is copy) {
self.bless(*, :$value);
}
multi method Bridge() {
self.value.Num;
}
}
# atan2 tests
# First, test atan2 with x = 1
for @sines -> $angle
{
next if abs(cos($angle.key())) < 1e-6;
my $desired-result = sin($angle.key()) / cos($angle.key());
# Num.atan2 tests
is_approx($desired-result.Num.atan2.tan, $desired-result,
"Num.atan2() - {$angle.key()}");
is_approx($desired-result.Num.atan2(1.Num).tan, $desired-result,
"Num.atan2(1.Num) - {$angle.key()}");
}
# check that the proper quadrant is returned
is_approx(atan2(4, 4), pi / 4, "atan2(4, 4) is pi / 4");
is_approx(atan2(-4, 4), -pi / 4, "atan2(-4, 4) is -pi / 4");
is_approx(atan2(4, -4), 3 * pi / 4, "atan2(4, -4) is 3pi / 4");
is_approx(atan2(-4, -4), -3 * pi / 4, "atan2(-4, -4) is -3pi / 4");
{
# Num tests
is_approx(atan2((-0.1).Num), -0.099668652491162, "atan2(Num)");
}
{
# Num vs Num tests
is_approx((-100).Num.atan2((100).Num), -0.785398163397448, "Num.atan2(Num)");
is_approx(atan2((0.1).Num, (-0.1).Num), 2.35619449019234, "atan2(Num, Num)");
}
{
# Num vs Rat tests
is_approx((10).Num.atan2((-10).Rat(1e-9)), 2.35619449019234, "Num.atan2(Rat)");
is_approx(atan2((-100).Num, (-100).Rat(1e-9)), -2.35619449019234, "atan2(Num, Rat)");
}
{
# Num vs Int tests
is_approx((-0.1).Num.atan2((1).Int), -0.099668652491162, "Num.atan2(Int)");
is_approx(atan2((-10).Num, (-1).Int), -1.67046497928606, "atan2(Num, Int)");
}
{
# Num vs Str tests
is_approx((-1).Num.atan2((0.1).Str), -1.47112767430373, "Num.atan2(Str)");
is_approx(atan2((10).Num, (-0.1).Str), 1.58079599348156, "atan2(Num, Str)");
}
{
# Num vs DifferentReal tests
is_approx((-1).Num.atan2(DifferentReal.new(-10)), -3.04192400109863, "Num.atan2(DifferentReal)");
is_approx(atan2((100).Num, DifferentReal.new(-100)), 2.35619449019234, "atan2(Num, DifferentReal)");
}
{
# Num vs FatRat tests
is_approx((-100).Num.atan2((10).FatRat), -1.47112767430373, "Num.atan2(FatRat)");
is_approx(atan2((0.1).Num, (-1).FatRat), 3.04192400109863, "atan2(Num, FatRat)");
}
{
# Rat tests
is_approx((10).Rat(1e-9).atan2, 1.47112767430373, "Rat.atan2");
is_approx(atan2((1).Rat(1e-9)), 0.785398163397448, "atan2(Rat)");
}
{
# Rat vs Num tests
is_approx((-0.1).Rat(1e-9).atan2((0.1).Num), -0.785398163397448, "Rat.atan2(Num)");
is_approx(atan2((0.1).Rat(1e-9), (-1).Num), 3.04192400109863, "atan2(Rat, Num)");
}
{
# Rat vs Rat tests
is_approx((-0.1).Rat(1e-9).atan2((-0.1).Rat(1e-9)), -2.35619449019234, "Rat.atan2(Rat)");
is_approx(atan2((10).Rat(1e-9), (-10).Rat(1e-9)), 2.35619449019234, "atan2(Rat, Rat)");
}
{
# Rat vs Int tests
is_approx((-0.1).Rat(1e-9).atan2((1).Int), -0.099668652491162, "Rat.atan2(Int)");
is_approx(atan2((100).Rat(1e-9), (100).Int), 0.785398163397448, "atan2(Rat, Int)");
}
{
# Rat vs Str tests
is_approx((0.1).Rat(1e-9).atan2((0.1).Str), 0.785398163397448, "Rat.atan2(Str)");
is_approx(atan2((1).Rat(1e-9), (-10).Str), 3.04192400109863, "atan2(Rat, Str)");
}
{
# Rat vs DifferentReal tests
is_approx((-100).Rat(1e-9).atan2(DifferentReal.new(1)), -1.56079666010823, "Rat.atan2(DifferentReal)");
is_approx(atan2((0.1).Rat(1e-9), DifferentReal.new(1)), 0.099668652491162, "atan2(Rat, DifferentReal)");
}
{
# Rat vs FatRat tests
is_approx((-0.1).Rat(1e-9).atan2((1).FatRat), -0.099668652491162, "Rat.atan2(FatRat)");
is_approx(atan2((-10).Rat(1e-9), (-100).FatRat), -3.04192400109863, "atan2(Rat, FatRat)");
}
{
# Int tests
is_approx((-10).Int.atan2, -1.47112767430373, "Int.atan2");
is_approx(atan2((1).Int), 0.785398163397448, "atan2(Int)");
}
{
# Int vs Num tests
is_approx((-1).Int.atan2((100).Num), -0.00999966668666524, "Int.atan2(Num)");
is_approx(atan2((-100).Int, (100).Num), -0.785398163397448, "atan2(Int, Num)");
}
{
# Int vs Rat tests
is_approx((10).Int.atan2((-10).Rat(1e-9)), 2.35619449019234, "Int.atan2(Rat)");
is_approx(atan2((-10).Int, (100).Rat(1e-9)), -0.099668652491162, "atan2(Int, Rat)");
}
{
# Int vs Int tests
is_approx((-100).Int.atan2((1).Int), -1.56079666010823, "Int.atan2(Int)");
is_approx(atan2((-1).Int, (-100).Int), -3.13159298690313, "atan2(Int, Int)");
}
{
# Int vs Str tests
is_approx((-1).Int.atan2((1).Str), -0.785398163397448, "Int.atan2(Str)");
is_approx(atan2((-1).Int, (-0.1).Str), -1.67046497928606, "atan2(Int, Str)");
}
{
# Int vs DifferentReal tests
is_approx((1).Int.atan2(DifferentReal.new(-0.1)), 1.67046497928606, "Int.atan2(DifferentReal)");
is_approx(atan2((1).Int, DifferentReal.new(10)), 0.099668652491162, "atan2(Int, DifferentReal)");
}
{
# Int vs FatRat tests
is_approx((-1).Int.atan2((-100).FatRat), -3.13159298690313, "Int.atan2(FatRat)");
is_approx(atan2((-1).Int, (-0.1).FatRat), -1.67046497928606, "atan2(Int, FatRat)");
}
{
# Str tests
is_approx((1).Str.atan2, 0.785398163397448, "Str.atan2");
is_approx(atan2((-1).Str), -0.785398163397448, "atan2(Str)");
}
{
# Str vs Num tests
is_approx((-10).Str.atan2((10).Num), -0.785398163397448, "Str.atan2(Num)");
is_approx(atan2((0.1).Str, (1).Num), 0.099668652491162, "atan2(Str, Num)");
}
{
# Str vs Rat tests
is_approx((100).Str.atan2((-10).Rat(1e-9)), 1.67046497928606, "Str.atan2(Rat)");
is_approx(atan2((0.1).Str, (-10).Rat(1e-9)), 3.13159298690313, "atan2(Str, Rat)");
}
{
# Str vs Int tests
is_approx((-100).Str.atan2((-100).Int), -2.35619449019234, "Str.atan2(Int)");
is_approx(atan2((0.1).Str, (-10).Int), 3.13159298690313, "atan2(Str, Int)");
}
{
# Str vs Str tests
is_approx((-1).Str.atan2((0.1).Str), -1.47112767430373, "Str.atan2(Str)");
is_approx(atan2((-10).Str, (1).Str), -1.47112767430373, "atan2(Str, Str)");
}
{
# Str vs DifferentReal tests
is_approx((-0.1).Str.atan2(DifferentReal.new(100)), -0.000999999666666867, "Str.atan2(DifferentReal)");
is_approx(atan2((0.1).Str, DifferentReal.new(1)), 0.099668652491162, "atan2(Str, DifferentReal)");
}
{
# Str vs FatRat tests
is_approx((10).Str.atan2((-100).FatRat), 3.04192400109863, "Str.atan2(FatRat)");
is_approx(atan2((1).Str, (-10).FatRat), 3.04192400109863, "atan2(Str, FatRat)");
}
{
# DifferentReal tests
is_approx(DifferentReal.new(0.1).atan2, 0.099668652491162, "DifferentReal.atan2");
is_approx(atan2(DifferentReal.new(0.1)), 0.099668652491162, "atan2(DifferentReal)");
}
{
# DifferentReal vs Num tests
is_approx(DifferentReal.new(-10).atan2((10).Num), -0.785398163397448, "DifferentReal.atan2(Num)");
is_approx(atan2(DifferentReal.new(-100), (-1).Num), -1.58079599348156, "atan2(DifferentReal, Num)");
}
{
# DifferentReal vs Rat tests
is_approx(DifferentReal.new(1).atan2((10).Rat(1e-9)), 0.099668652491162, "DifferentReal.atan2(Rat)");
is_approx(atan2(DifferentReal.new(-10), (-100).Rat(1e-9)), -3.04192400109863, "atan2(DifferentReal, Rat)");
}
{
# DifferentReal vs Int tests
is_approx(DifferentReal.new(10).atan2((-10).Int), 2.35619449019234, "DifferentReal.atan2(Int)");
is_approx(atan2(DifferentReal.new(100), (-1).Int), 1.58079599348156, "atan2(DifferentReal, Int)");
}
{
# DifferentReal vs Str tests
is_approx(DifferentReal.new(-1).atan2((-100).Str), -3.13159298690313, "DifferentReal.atan2(Str)");
is_approx(atan2(DifferentReal.new(-100), (1).Str), -1.56079666010823, "atan2(DifferentReal, Str)");
}
{
# DifferentReal vs DifferentReal tests
is_approx(DifferentReal.new(-10).atan2(DifferentReal.new(100)), -0.099668652491162, "DifferentReal.atan2(DifferentReal)");
is_approx(atan2(DifferentReal.new(1), DifferentReal.new(-100)), 3.13159298690313, "atan2(DifferentReal, DifferentReal)");
}
{
# DifferentReal vs FatRat tests
is_approx(DifferentReal.new(1).atan2((100).FatRat), 0.00999966668666524, "DifferentReal.atan2(FatRat)");
is_approx(atan2(DifferentReal.new(0.1), (-100).FatRat), 3.14059265392313, "atan2(DifferentReal, FatRat)");
}
{
# FatRat tests
is_approx((0.1).FatRat.atan2, 0.099668652491162, "FatRat.atan2");
is_approx(atan2((-0.1).FatRat), -0.099668652491162, "atan2(FatRat)");
}
{
# FatRat vs Num tests
is_approx((1).FatRat.atan2((10).Num), 0.099668652491162, "FatRat.atan2(Num)");
is_approx(atan2((0.1).FatRat, (0.1).Num), 0.785398163397448, "atan2(FatRat, Num)");
}
{
# FatRat vs Rat tests
is_approx((10).FatRat.atan2((-0.1).Rat(1e-9)), 1.58079599348156, "FatRat.atan2(Rat)");
is_approx(atan2((0.1).FatRat, (1).Rat(1e-9)), 0.099668652491162, "atan2(FatRat, Rat)");
}
{
# FatRat vs Int tests
is_approx((10).FatRat.atan2((1).Int), 1.47112767430373, "FatRat.atan2(Int)");
is_approx(atan2((-100).FatRat, (100).Int), -0.785398163397448, "atan2(FatRat, Int)");
}
{
# FatRat vs Str tests
is_approx((100).FatRat.atan2((100).Str), 0.785398163397448, "FatRat.atan2(Str)");
is_approx(atan2((-0.1).FatRat, (100).Str), -0.000999999666666867, "atan2(FatRat, Str)");
}
{
# FatRat vs DifferentReal tests
is_approx((100).FatRat.atan2(DifferentReal.new(-10)), 1.67046497928606, "FatRat.atan2(DifferentReal)");
is_approx(atan2((-10).FatRat, DifferentReal.new(-0.1)), -1.58079599348156, "atan2(FatRat, DifferentReal)");
}
{
# FatRat vs FatRat tests
is_approx((0.1).FatRat.atan2((-1).FatRat), 3.04192400109863, "FatRat.atan2(FatRat)");
is_approx(atan2((0.1).FatRat, (-10).FatRat), 3.13159298690313, "atan2(FatRat, FatRat)");
}
done;
# vim: ft=perl6 nomodifiable