-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
math.jl
1662 lines (1541 loc) · 65.2 KB
/
math.jl
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Random
using LinearAlgebra
using Base.Experimental: @force_compile
function isnan_type(::Type{T}, x) where T
isa(x, T) && isnan(x)
end
# has_fma has no runtime support.
# So we need function wrappers to make this work.
has_fma_Int() = Core.Compiler.have_fma(Int)
has_fma_Float32() = Core.Compiler.have_fma(Float32)
has_fma_Float64() = Core.Compiler.have_fma(Float64)
has_fma = Dict(
Int => has_fma_Int(),
Rational{Int} => has_fma_Int(),
Float32 => has_fma_Float32(),
Float64 => has_fma_Float64(),
BigFloat => true,
)
@testset "clamp" begin
@test clamp(0, 1, 3) == 1
@test clamp(1, 1, 3) == 1
@test clamp(2, 1, 3) == 2
@test clamp(3, 1, 3) == 3
@test clamp(4, 1, 3) == 3
@test clamp(0.0, 1, 3) == 1.0
@test clamp(1.0, 1, 3) == 1.0
@test clamp(2.0, 1, 3) == 2.0
@test clamp(3.0, 1, 3) == 3.0
@test clamp(4.0, 1, 3) == 3.0
@test clamp.([0, 1, 2, 3, 4], 1.0, 3.0) == [1.0, 1.0, 2.0, 3.0, 3.0]
@test clamp.([0 1; 2 3], 1.0, 3.0) == [1.0 1.0; 2.0 3.0]
@test clamp(-200, Int8) === typemin(Int8)
@test clamp(100, Int8) === Int8(100)
@test clamp(200, Int8) === typemax(Int8)
begin
x = [0.0, 1.0, 2.0, 3.0, 4.0]
clamp!(x, 1, 3)
@test x == [1.0, 1.0, 2.0, 3.0, 3.0]
end
@test clamp(typemax(UInt64), Int64) === typemax(Int64)
@test clamp(typemin(Int), UInt64) === typemin(UInt64)
@test clamp(Int16(-1), UInt16) === UInt16(0)
@test clamp(-1, 2, UInt(0)) === UInt(2)
@test clamp(typemax(UInt16), Int16) === Int16(32767)
# clamp should not allocate a BigInt for typemax(Int16)
x = big(2) ^ 100
@test (@allocated clamp(x, Int16)) == 0
x = clamp(2.0, BigInt)
@test x isa BigInt
@test x == big(2)
end
@testset "constants" begin
@test pi != ℯ
@test ℯ != 1//2
@test 1//2 <= ℯ
@test ℯ <= 15//3
@test big(1//2) < ℯ
@test ℯ < big(20//6)
@test ℯ^pi == exp(pi)
@test ℯ^2 == exp(2)
@test ℯ^2.4 == exp(2.4)
@test ℯ^(2//3) == exp(2//3)
@test Float16(3.0) < pi
@test pi < Float16(4.0)
@test widen(pi) === pi
@test occursin("3.14159", sprint(show, MIME"text/plain"(), π))
@test repr(Any[pi ℯ; ℯ pi]) == "Any[π ℯ; ℯ π]"
@test string(pi) == "π"
@test sin(π) == sind(180) === sinpi(1) === sinpi(1//1) == tan(π) == 0
@test tan(π) == tand(180) === tanpi(1) === tanpi(1//1) === -0.0
@test cos(π) == cosd(180) === cospi(1) === cospi(1//1) == sec(π) == -1
@test csc(π) == 1/0 && cot(π) == -1/0
@test sincos(π) === sincospi(1) == (0, -1)
end
@testset "frexp,ldexp,significand,exponent" begin
@testset "$T" for T in (Float16,Float32,Float64)
for z in (zero(T),-zero(T))
frexp(z) === (z,0)
significand(z) === z
@test_throws DomainError exponent(z)
end
for (a,b) in [(T(12.8),T(0.8)),
(prevfloat(floatmin(T)), prevfloat(one(T), 2)),
(prevfloat(floatmin(T)), prevfloat(one(T), 2)),
(prevfloat(floatmin(T)), nextfloat(one(T), -2)),
(nextfloat(zero(T), 3), T(0.75)),
(prevfloat(zero(T), -3), T(0.75)),
(nextfloat(zero(T)), T(0.5))]
n = Int(log2(a/b))
@test frexp(a) == (b,n)
@test ldexp(b,n) == a
@test ldexp(a,-n) == b
@test significand(a) == 2b
@test exponent(a) == n-1
@test frexp(-a) == (-b,n)
@test ldexp(-b,n) == -a
@test ldexp(-a,-n) == -b
@test significand(-a) == -2b
@test exponent(-a) == n-1
end
@test_throws DomainError exponent(convert(T,NaN))
@test isnan_type(T, significand(convert(T,NaN)))
x,y = frexp(convert(T,NaN))
@test isnan_type(T, x)
@test y == 0
@testset "ldexp function" begin
@test ldexp(T(0.0), 0) === T(0.0)
@test ldexp(T(-0.0), 0) === T(-0.0)
@test ldexp(T(Inf), 1) === T(Inf)
@test ldexp(T(Inf), 10000) === T(Inf)
@test ldexp(T(-Inf), 1) === T(-Inf)
@test isnan_type(T, ldexp(T(NaN), 10))
@test ldexp(T(1.0), 0) === T(1.0)
@test ldexp(T(0.8), 4) === T(12.8)
@test ldexp(T(-0.854375), 5) === T(-27.34)
@test ldexp(T(1.0), typemax(Int)) === T(Inf)
@test ldexp(T(1.0), typemin(Int)) === T(0.0)
@test ldexp(prevfloat(floatmin(T)), typemax(Int)) === T(Inf)
@test ldexp(prevfloat(floatmin(T)), typemin(Int)) === T(0.0)
@test ldexp(T(0.0), Int128(0)) === T(0.0)
@test ldexp(T(-0.0), Int128(0)) === T(-0.0)
@test ldexp(T(1.0), Int128(0)) === T(1.0)
@test ldexp(T(0.8), Int128(4)) === T(12.8)
@test ldexp(T(-0.854375), Int128(5)) === T(-27.34)
@test ldexp(T(1.0), typemax(Int128)) === T(Inf)
@test ldexp(T(1.0), typemin(Int128)) === T(0.0)
@test ldexp(prevfloat(floatmin(T)), typemax(Int128)) === T(Inf)
@test ldexp(prevfloat(floatmin(T)), typemin(Int128)) === T(0.0)
@test ldexp(T(0.0), BigInt(0)) === T(0.0)
@test ldexp(T(-0.0), BigInt(0)) === T(-0.0)
@test ldexp(T(1.0), BigInt(0)) === T(1.0)
@test ldexp(T(0.8), BigInt(4)) === T(12.8)
@test ldexp(T(-0.854375), BigInt(5)) === T(-27.34)
@test ldexp(T(1.0), BigInt(typemax(Int128))) === T(Inf)
@test ldexp(T(1.0), BigInt(typemin(Int128))) === T(0.0)
@test ldexp(prevfloat(floatmin(T)), BigInt(typemax(Int128))) === T(Inf)
@test ldexp(prevfloat(floatmin(T)), BigInt(typemin(Int128))) === T(0.0)
# Test also against BigFloat reference. Needs to be exactly rounded.
@test ldexp(floatmin(T), -1) == T(ldexp(big(floatmin(T)), -1))
@test ldexp(floatmin(T), -2) == T(ldexp(big(floatmin(T)), -2))
@test ldexp(floatmin(T)/2, 0) == T(ldexp(big(floatmin(T)/2), 0))
@test ldexp(floatmin(T)/3, 0) == T(ldexp(big(floatmin(T)/3), 0))
@test ldexp(floatmin(T)/3, -1) == T(ldexp(big(floatmin(T)/3), -1))
@test ldexp(floatmin(T)/3, 11) == T(ldexp(big(floatmin(T)/3), 11))
@test ldexp(floatmin(T)/11, -10) == T(ldexp(big(floatmin(T)/11), -10))
@test ldexp(-floatmin(T)/11, -10) == T(ldexp(big(-floatmin(T)/11), -10))
end
end
end
# We compare to BigFloat instead of hard-coding
# values, assuming that BigFloat has an independently tested implementation.
@testset "basic math functions" begin
@testset "$T" for T in (Float16, Float32, Float64)
x = T(1//3)
y = T(1//2)
yi = 4
@testset "Random values" begin
@test x^y === T(big(x)^big(y))
@test x^1 === x
@test x^yi === T(big(x)^yi)
@test (-x)^yi == x^yi
@test (-x)^(yi+1) == -(x^(yi+1))
@test acos(x) ≈ acos(big(x))
@test acosh(1+x) ≈ acosh(big(1+x))
@test asin(x) ≈ asin(big(x))
@test asinh(x) ≈ asinh(big(x))
@test atan(x) ≈ atan(big(x))
@test atan(x,y) ≈ atan(big(x),big(y))
@test atanh(x) ≈ atanh(big(x))
@test cbrt(x) ≈ cbrt(big(x))
@test fourthroot(x) ≈ fourthroot(big(x))
@test cos(x) ≈ cos(big(x))
@test cosh(x) ≈ cosh(big(x))
@test cospi(x) ≈ cospi(big(x))
@test exp(x) ≈ exp(big(x))
@test exp10(x) ≈ exp10(big(x))
@test exp2(x) ≈ exp2(big(x))
@test expm1(x) ≈ expm1(big(x))
@test expm1(T(-1.1)) ≈ expm1(big(T(-1.1)))
@test hypot(x,y) ≈ hypot(big(x),big(y))
@test hypot(x,x,y) ≈ hypot(hypot(big(x),big(x)),big(y))
@test hypot(x,x,y,y) ≈ hypot(hypot(big(x),big(x)),hypot(big(y),big(y)))
@test log(x) ≈ log(big(x))
@test log10(x) ≈ log10(big(x))
@test log1p(x) ≈ log1p(big(x))
@test log2(x) ≈ log2(big(x))
@test sin(x) ≈ sin(big(x))
@test sinh(x) ≈ sinh(big(x))
@test sinpi(x) ≈ sinpi(big(x))
@test sqrt(x) ≈ sqrt(big(x))
@test tan(x) ≈ tan(big(x))
@test tanh(x) ≈ tanh(big(x))
@test tanpi(x) ≈ tanpi(big(x))
@test sec(x) ≈ sec(big(x))
@test csc(x) ≈ csc(big(x))
@test secd(x) ≈ secd(big(x))
@test cscd(x) ≈ cscd(big(x))
@test sech(x) ≈ sech(big(x))
@test csch(x) ≈ csch(big(x))
end
@testset "Special values" begin
@test isequal(T(1//4)^T(1//2), T(1//2))
@test isequal(T(1//4)^2, T(1//16))
@test isequal(acos(T(1)), T(0))
@test isequal(acosh(T(1)), T(0))
@test asin(T(1)) ≈ T(pi)/2 atol=eps(T)
@test atan(T(1)) ≈ T(pi)/4 atol=eps(T)
@test atan(T(1),T(1)) ≈ T(pi)/4 atol=eps(T)
@test isequal(cbrt(T(0)), T(0))
@test isequal(cbrt(T(1)), T(1))
@test isequal(cbrt(T(1000000000))^3, T(1000)^3)
@test isequal(fourthroot(T(0)), T(0))
@test isequal(fourthroot(T(1)), T(1))
@test isequal(fourthroot(T(100000000))^4, T(100)^4)
@test isequal(cos(T(0)), T(1))
@test cos(T(pi)/2) ≈ T(0) atol=eps(T)
@test isequal(cos(T(pi)), T(-1))
@test exp(T(1)) ≈ T(ℯ) atol=2*eps(T)
@test isequal(exp10(T(1)), T(10))
@test isequal(exp2(T(1)), T(2))
@test isequal(expm1(T(0)), T(0))
@test isequal(expm1(-floatmax(T)), -one(T))
@test isequal(expm1(floatmax(T)), T(Inf))
@test expm1(T(1)) ≈ T(ℯ)-1 atol=2*eps(T)
@test isequal(hypot(T(3),T(4)), T(5))
@test isequal(hypot(floatmax(T),T(1)),floatmax(T))
@test isequal(hypot(floatmin(T)*sqrt(eps(T)),T(0)),floatmin(T)*sqrt(eps(T)))
@test isequal(floatmin(T)*hypot(1.368423059742933,1.3510496552495361),hypot(floatmin(T)*1.368423059742933,floatmin(T)*1.3510496552495361))
@test isequal(log(T(1)), T(0))
@test isequal(log(ℯ,T(1)), T(0))
@test log(T(ℯ)) ≈ T(1) atol=eps(T)
@test isequal(log10(T(1)), T(0))
@test isequal(log10(T(10)), T(1))
@test isequal(log1p(T(0)), T(0))
@test log1p(T(ℯ)-1) ≈ T(1) atol=eps(T)
@test isequal(log2(T(1)), T(0))
@test isequal(log2(T(2)), T(1))
@test isequal(sin(T(0)), T(0))
@test isequal(sin(T(pi)/2), T(1))
@test sin(T(pi)) ≈ T(0) atol=eps(T)
@test isequal(sqrt(T(0)), T(0))
@test isequal(sqrt(T(1)), T(1))
@test isequal(sqrt(T(100000000))^2, T(10000)^2)
@test isequal(tan(T(0)), T(0))
@test tan(T(pi)/4) ≈ T(1) atol=eps(T)
@test isequal(sec(T(pi)), -one(T))
@test isequal(csc(T(pi)/2), one(T))
@test isequal(secd(T(180)), -one(T))
@test isequal(cscd(T(90)), one(T))
@test isequal(sech(log(one(T))), one(T))
@test isequal(csch(zero(T)), T(Inf))
@test zero(T)^y === zero(T)
@test zero(T)^zero(T) === one(T)
@test zero(T)^(-y) === T(Inf)
@test zero(T)^T(NaN) === T(NaN)
@test one(T)^y === one(T)
@test one(T)^zero(T) === one(T)
@test one(T)^T(NaN) === one(T)
@test isnan(T(NaN)^T(-.5))
end
@testset "Inverses" begin
@test acos(cos(x)) ≈ x
@test acosh(cosh(x)) ≈ x
@test asin(sin(x)) ≈ x
@test cbrt(x)^3 ≈ x
@test cbrt(x^3) ≈ x
@test fourthroot(x)^4 ≈ x
@test fourthroot(x^4) ≈ x
@test asinh(sinh(x)) ≈ x
@test atan(tan(x)) ≈ x
@test atan(x,y) ≈ atan(x/y)
@test atanh(tanh(x)) ≈ x
@test cos(acos(x)) ≈ x
@test cosh(acosh(1+x)) ≈ 1+x
@test exp(log(x)) ≈ x
@test exp10(log10(x)) ≈ x
@test exp2(log2(x)) ≈ x
@test expm1(log1p(x)) ≈ x
@test log(exp(x)) ≈ x
@test log10(exp10(x)) ≈ x
@test log1p(expm1(x)) ≈ x
@test log2(exp2(x)) ≈ x
@test sin(asin(x)) ≈ x
@test sinh(asinh(x)) ≈ x
@test sqrt(x)^2 ≈ x
@test sqrt(x^2) ≈ x
@test tan(atan(x)) ≈ x
@test tanh(atanh(x)) ≈ x
end
@testset "Relations between functions" begin
@test cosh(x) ≈ (exp(x)+exp(-x))/2
@test cosh(x)^2-sinh(x)^2 ≈ 1
@test hypot(x,y) ≈ sqrt(x^2+y^2)
@test sin(x)^2+cos(x)^2 ≈ 1
@test sinh(x) ≈ (exp(x)-exp(-x))/2
@test tan(x) ≈ sin(x)/cos(x)
@test tanh(x) ≈ sinh(x)/cosh(x)
@test sec(x) ≈ inv(cos(x))
@test csc(x) ≈ inv(sin(x))
@test secd(x) ≈ inv(cosd(x))
@test cscd(x) ≈ inv(sind(x))
@test sech(x) ≈ inv(cosh(x))
@test csch(x) ≈ inv(sinh(x))
end
@testset "Edge cases" begin
@test isinf(log(zero(T)))
@test isnan_type(T, log(convert(T,NaN)))
@test_throws DomainError log(-one(T))
@test isinf(log1p(-one(T)))
@test isnan_type(T, log1p(convert(T,NaN)))
@test_throws DomainError log1p(convert(T,-2.0))
@test hypot(T(0), T(0)) === T(0)
@test hypot(T(Inf), T(Inf)) === T(Inf)
@test hypot(T(Inf), T(x)) === T(Inf)
@test hypot(T(Inf), T(NaN)) === T(Inf)
@test isnan_type(T, hypot(T(x), T(NaN)))
@test tanh(T(Inf)) === T(1)
end
end
@testset "Float16 expm1" begin
T=Float16
@test isequal(expm1(T(0)), T(0))
@test isequal(expm1(-floatmax(T)), -one(T))
@test isequal(expm1(floatmax(T)), T(Inf))
@test expm1(T(1)) ≈ T(ℯ)-1 atol=2*eps(T)
end
end
@testset "exponential functions" for T in (Float64, Float32, Float16)
for (func, invfunc) in ((exp2, log2), (exp, log), (exp10, log10))
@testset "$T $func accuracy" begin
minval, maxval = invfunc(floatmin(T)),prevfloat(invfunc(floatmax(T)))
# Test range and extensively test numbers near 0.
X = Iterators.flatten((minval:T(.1):maxval,
minval/100:T(.0021):maxval/100,
minval/10000:T(.000021):maxval/10000,
nextfloat(zero(T)),
T(-100):T(1):T(100) ))
for x in X
y, yb = func(x), func(widen(x))
if isfinite(eps(T(yb)))
@test abs(y-yb) <= 1.2*eps(T(yb))
end
end
end
@testset "$T $func edge cases" begin
@test func(T(-Inf)) === T(0.0)
@test func(T(Inf)) === T(Inf)
@test func(T(NaN)) === T(NaN)
@test func(T(0.0)) === T(1.0) # exact
@test func(T(5000.0)) === T(Inf)
@test func(T(-5000.0)) === T(0.0)
end
end
end
@testset "test abstractarray trig functions" begin
TAA = rand(2,2)
TAA = (TAA + TAA')/2.
STAA = Symmetric(TAA)
@test Array(atanh.(STAA)) == atanh.(TAA)
@test Array(asinh.(STAA)) == asinh.(TAA)
TAA .+= 1
@test Array(acosh.(STAA)) == acosh.(TAA)
@test Array(acsch.(STAA)) == acsch.(TAA)
@test Array(acoth.(STAA)) == acoth.(TAA)
@test sind(TAA) == sin(deg2rad.(TAA))
@test cosd(TAA) == cos(deg2rad.(TAA))
@test tand(TAA) == tan(deg2rad.(TAA))
@test asind(TAA) == rad2deg.(asin(TAA))
@test acosd(TAA) == rad2deg.(acos(TAA))
@test atand(TAA) == rad2deg.(atan(TAA))
@test asecd(TAA) == rad2deg.(asec(TAA))
@test acscd(TAA) == rad2deg.(acsc(TAA))
@test acotd(TAA) == rad2deg.(acot(TAA))
m = rand(3,2) # not square matrix
ex = @test_throws DimensionMismatch sind(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch cosd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch tand(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch asind(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch acosd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch atand(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch asecd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch acscd(m)
@test startswith(ex.value.msg, "matrix is not square")
ex = @test_throws DimensionMismatch acotd(m)
@test startswith(ex.value.msg, "matrix is not square")
end
@testset "check exp2(::Integer) matches exp2(::Float)" begin
for ii in -2048:2048
expected = exp2(float(ii))
@test exp2(Int16(ii)) == expected
@test exp2(Int32(ii)) == expected
@test exp2(Int64(ii)) == expected
@test exp2(Int128(ii)) == expected
if ii >= 0
@test exp2(UInt16(ii)) == expected
@test exp2(UInt32(ii)) == expected
@test exp2(UInt64(ii)) == expected
@test exp2(UInt128(ii)) == expected
end
end
end
@testset "deg2rad/rad2deg" begin
@testset "$T" for T in (Int, Float64, BigFloat)
@test deg2rad(T(180)) ≈ 1pi
@test deg2rad.(T[45, 60]) ≈ [pi/T(4), pi/T(3)]
@test rad2deg.([pi/T(4), pi/T(3)]) ≈ [45, 60]
@test rad2deg(T(1)*pi) ≈ 180
@test rad2deg(T(1)) ≈ rad2deg(true)
@test deg2rad(T(1)) ≈ deg2rad(true)
end
@test deg2rad(180 + 60im) ≈ pi + (pi/3)*im
@test rad2deg(pi + (pi/3)*im) ≈ 180 + 60im
end
# ensure zeros are signed the same
⩲(x,y) = typeof(x) == typeof(y) && x == y && signbit(x) == signbit(y)
⩲(x::Tuple, y::Tuple) = length(x) == length(y) && all(map(⩲,x,y))
@testset "degree-based trig functions" begin
@testset "$T" for T = (Float32,Float64,Rational{Int},BigFloat)
fT = typeof(float(one(T)))
fTsc = typeof( (float(one(T)), float(one(T))) )
for x = -400:40:400
@test sind(convert(T,x))::fT ≈ sin(pi*convert(fT,x)/180) atol=eps(deg2rad(convert(fT,x)))
@test cosd(convert(T,x))::fT ≈ cos(pi*convert(fT,x)/180) atol=eps(deg2rad(convert(fT,x)))
s,c = sincosd(convert(T,x))
@test s::fT ≈ sin(pi*convert(fT,x)/180) atol=eps(deg2rad(convert(fT,x)))
@test c::fT ≈ cos(pi*convert(fT,x)/180) atol=eps(deg2rad(convert(fT,x)))
end
@testset "sind" begin
@test sind(convert(T,0.0))::fT ⩲ zero(fT)
@test sind(convert(T,180.0))::fT ⩲ zero(fT)
@test sind(convert(T,360.0))::fT ⩲ zero(fT)
T != Rational{Int} && @test sind(convert(T,-0.0))::fT ⩲ -zero(fT)
@test sind(convert(T,-180.0))::fT ⩲ -zero(fT)
@test sind(convert(T,-360.0))::fT ⩲ -zero(fT)
if T <: AbstractFloat
@test isnan(sind(T(NaN)))
end
end
@testset "cosd" begin
@test cosd(convert(T,90))::fT ⩲ zero(fT)
@test cosd(convert(T,270))::fT ⩲ zero(fT)
@test cosd(convert(T,-90))::fT ⩲ zero(fT)
@test cosd(convert(T,-270))::fT ⩲ zero(fT)
if T <: AbstractFloat
@test isnan(cosd(T(NaN)))
end
end
@testset "sincosd" begin
@test sincosd(convert(T,-360))::fTsc ⩲ ( -zero(fT), one(fT) )
@test sincosd(convert(T,-270))::fTsc ⩲ ( one(fT), zero(fT) )
@test sincosd(convert(T,-180))::fTsc ⩲ ( -zero(fT), -one(fT) )
@test sincosd(convert(T, -90))::fTsc ⩲ ( -one(fT), zero(fT) )
@test sincosd(convert(T, 0))::fTsc ⩲ ( zero(fT), one(fT) )
@test sincosd(convert(T, 90))::fTsc ⩲ ( one(fT), zero(fT) )
@test sincosd(convert(T, 180))::fTsc ⩲ ( zero(fT), -one(fT) )
@test sincosd(convert(T, 270))::fTsc ⩲ ( -one(fT), zero(fT) )
if T <: AbstractFloat
@test_throws DomainError sincosd(T(Inf))
@test all(isnan.(sincosd(T(NaN))))
end
end
@testset "$name" for (name, (sinpi, cospi)) in (
"sinpi and cospi" => (sinpi, cospi),
"sincospi" => (x->sincospi(x)[1], x->sincospi(x)[2])
)
@testset "pi * $x" for x = -3:0.3:3
@test sinpi(convert(T,x))::fT ≈ sin(pi*convert(fT,x)) atol=eps(pi*convert(fT,x))
@test cospi(convert(T,x))::fT ≈ cos(pi*convert(fT,x)) atol=eps(pi*convert(fT,x))
end
@test sinpi(convert(T,0.0))::fT ⩲ zero(fT)
@test sinpi(convert(T,1.0))::fT ⩲ zero(fT)
@test sinpi(convert(T,2.0))::fT ⩲ zero(fT)
T != Rational{Int} && @test sinpi(convert(T,-0.0))::fT ⩲ -zero(fT)
@test sinpi(convert(T,-1.0))::fT ⩲ -zero(fT)
@test sinpi(convert(T,-2.0))::fT ⩲ -zero(fT)
@test_throws DomainError sinpi(convert(T,Inf))
@test cospi(convert(T,0.5))::fT ⩲ zero(fT)
@test cospi(convert(T,1.5))::fT ⩲ zero(fT)
@test cospi(convert(T,-0.5))::fT ⩲ zero(fT)
@test cospi(convert(T,-1.5))::fT ⩲ zero(fT)
@test_throws DomainError cospi(convert(T,Inf))
end
@testset "trig pi functions accuracy" for numerator in -20:1:20
for func in (sinpi, cospi, tanpi,
x -> sincospi(x)[1],
x -> sincospi(x)[2])
x = numerator // 20
# Check that rational function works
@test func(x) ≈ func(BigFloat(x))
# Use short value so that wider values will be exactly equal
shortx = Float16(x)
# Compare to BigFloat value
bigvalue = func(BigFloat(shortx))
for T in (Float16,Float32,Float64)
@test func(T(shortx)) ≈ T(bigvalue)
end
end
end
@testset begin
# If the machine supports fma (fused multiply add), we require exact equality.
# Otherwise, we only require approximate equality.
if has_fma[T]
my_eq = (==)
@debug "On this machine, FMA is supported for $(T), so we will test for exact equality" my_eq
else
my_eq = isapprox
@debug "On this machine, FMA is not supported for $(T), so we will test for approximate equality" my_eq
end
@testset let context=(T, has_fma[T], my_eq)
@test sind(convert(T,30)) == 0.5
@test cosd(convert(T,60)) == 0.5
@test sind(convert(T,150)) == 0.5
@test my_eq(sinpi(one(T)/convert(T,6)), 0.5)
@test my_eq(sincospi(one(T)/convert(T,6))[1], 0.5)
@test_throws DomainError sind(convert(T,Inf))
@test_throws DomainError cosd(convert(T,Inf))
fT == Float64 && @test my_eq(cospi(one(T)/convert(T,3)), 0.5)
fT == Float64 && @test my_eq(sincospi(one(T)/convert(T,3))[2], 0.5)
T == Rational{Int} && @test my_eq(sinpi(5//6), 0.5)
T == Rational{Int} && @test my_eq(sincospi(5//6)[1], 0.5)
end
end
end
scdm = sincosd(missing)
@test ismissing(scdm[1])
@test ismissing(scdm[2])
end
@testset "Integer and Inf args for sinpi/cospi/tanpi/sinc/cosc" begin
for (sinpi, cospi) in ((sinpi, cospi), (x->sincospi(x)[1], x->sincospi(x)[2]))
@test sinpi(1) === 0.0
@test sinpi(-1) === -0.0
@test cospi(1) == -1
@test cospi(2) == 1
end
@test tanpi(1) === -0.0
@test tanpi(-1) === 0.0
@test tanpi(2) === 0.0
@test tanpi(-2) === -0.0
@test sinc(1) == 0
@test sinc(complex(1,0)) == 0
@test sinc(0) == 1
@test sinc(Inf) == 0
@test cosc(1) == -1
@test cosc(0) == 0
@test cosc(complex(1,0)) == -1
@test cosc(Inf) == 0
@test sinc(Inf + 3im) == 0
@test cosc(Inf + 3im) == 0
@test isequal(sinc(Inf + Inf*im), NaN + NaN*im)
@test isequal(cosc(Inf + Inf*im), NaN + NaN*im)
end
# issue #37227
@testset "sinc/cosc accuracy" begin
setprecision(256) do
for R in (BigFloat, Float16, Float32, Float64)
for T in (R, Complex{R})
for x in (0, 1e-5, 1e-20, 1e-30, 1e-40, 1e-50, 1e-60, 1e-70, 5.07138898934e-313)
if x < eps(R)
@test sinc(T(x)) == 1
end
@test cosc(T(x)) ≈ pi*(-R(x)*pi)/3 rtol=max(eps(R)*100, (pi*R(x))^2)
end
end
end
end
@test @inferred(sinc(0//1)) ⩲ 1.0
@test @inferred(cosc(0//1)) ⩲ -0.0
# test right before/after thresholds of Taylor series
@test sinc(0.001) ≈ 0.999998355066745 rtol=1e-15
@test sinc(0.00099) ≈ 0.9999983878009009 rtol=1e-15
@test sinc(0.05f0) ≈ 0.9958927352435614 rtol=1e-7
@test sinc(0.0499f0) ≈ 0.9959091277049384 rtol=1e-7
if has_fma[Float64]
@test cosc(0.14) ≈ -0.4517331883801308 rtol=1e-15
else
@test cosc(0.14) ≈ -0.4517331883801308 rtol=1e-14
end
@test cosc(0.1399) ≈ -0.45142306168781854 rtol=1e-14
@test cosc(0.26f0) ≈ -0.7996401373462212 rtol=5e-7
@test cosc(0.2599f0) ≈ -0.7993744054401625 rtol=5e-7
setprecision(256) do
@test cosc(big"0.5") ≈ big"-1.273239544735162686151070106980114896275677165923651589981338752471174381073817" rtol=1e-76
@test cosc(big"0.499") ≈ big"-1.272045747741181369948389133250213864178198918667041860771078493955590574971317" rtol=1e-76
end
end
@testset "Irrational args to sinpi/cospi/tanpi/sinc/cosc" begin
for x in (pi, ℯ, Base.MathConstants.golden)
for (sinpi, cospi) in ((sinpi, cospi), (x->sincospi(x)[1], x->sincospi(x)[2]))
@test sinpi(x) ≈ Float64(sinpi(big(x)))
@test cospi(x) ≈ Float64(cospi(big(x)))
@test sinpi(complex(x, x)) ≈ ComplexF64(sinpi(complex(big(x), big(x))))
@test cospi(complex(x, x)) ≈ ComplexF64(cospi(complex(big(x), big(x))))
end
@test tanpi(x) ≈ Float64(tanpi(big(x)))
@test sinc(x) ≈ Float64(sinc(big(x)))
@test cosc(x) ≈ Float64(cosc(big(x)))
@test sinc(complex(x, x)) ≈ ComplexF64(sinc(complex(big(x), big(x))))
@test cosc(complex(x, x)) ≈ ComplexF64(cosc(complex(big(x), big(x))))
end
end
@testset "half-integer and nan/infs for sincospi,sinpi,cospi" begin
@testset for T in (ComplexF32, ComplexF64)
@test sincospi(T(0.5, 0.0)) == (T(1.0,0.0), T(0.0, -0.0))
@test sincospi(T(1.5, 0.0)) == (T(-1.0,0.0), T(0.0, 0.0))
@test sinpi(T(1.5, 1.5)) ≈ T(-cosh(3*π/2), 0.0)
@test cospi(T(0.5, 0.5)) ≈ T(0.0, -sinh(π/2))
s, c = sincospi(T(Inf64, 0.0))
@test isnan(real(s)) && imag(s) == zero(real(T))
@test isnan(real(c)) && imag(c) == -zero(real(T))
s, c = sincospi(T(NaN, 0.0))
@test isnan(real(s)) && imag(s) == zero(real(T))
@test isnan(real(c)) && imag(c) == zero(real(T))
s, c = sincospi(T(NaN, Inf64))
@test isnan(real(s)) && isinf(imag(s))
@test isinf(real(c)) && isnan(imag(c))
s, c = sincospi(T(NaN, 2))
@test isnan(real(s)) && isnan(imag(s))
@test isnan(real(c)) && isnan(imag(c))
end
end
@testset "trig function type stability" begin
@testset "$T $f" for T = (Float32,Float64,BigFloat,Rational{Int16},Complex{Int32},ComplexF16), f = (sind,cosd,sinpi,cospi,tanpi)
@test Base.return_types(f,Tuple{T}) == [float(T)]
end
@testset "$T sincospi" for T = (Float32,Float64,BigFloat,Rational{Int16},Complex{Int32},ComplexF16)
@test Base.return_types(sincospi,Tuple{T}) == [Tuple{float(T),float(T)}]
end
end
# useful test functions for relative error, which differ from isapprox (≈)
# in that relerrc separately looks at the real and imaginary parts
relerr(z, x) = z == x ? 0.0 : abs(z - x) / abs(x)
relerrc(z, x) = max(relerr(real(z),real(x)), relerr(imag(z),imag(x)))
≅(a,b) = relerrc(a,b) ≤ 1e-13
@testset "subnormal flags" begin
# Ensure subnormal flags functions don't segfault
@test any(set_zero_subnormals(true) .== [false,true])
@test any(get_zero_subnormals() .== [false,true])
@test set_zero_subnormals(false)
@test !get_zero_subnormals()
end
@testset "evalpoly" begin
@test @evalpoly(2,3,4,5,6) == 3+2*(4+2*(5+2*6)) == @evalpoly(2+0im,3,4,5,6)
a0 = 1
a1 = 2
c = 3
@test @evalpoly(c, a0, a1) == 7
@test @evalpoly(1, 2) == 2
end
@testset "evalpoly real" begin
for x in -1.0:2.0, p1 in -3.0:3.0, p2 in -3.0:3.0, p3 in -3.0:3.0
evpm = @evalpoly(x, p1, p2, p3)
@test evalpoly(x, (p1, p2, p3)) == evpm
@test evalpoly(x, [p1, p2, p3]) == evpm
end
end
@testset "evalpoly complex" begin
for x in -1.0:2.0, y in -1.0:2.0, p1 in -3.0:3.0, p2 in -3.0:3.0, p3 in -3.0:3.0
z = x + im * y
evpm = @evalpoly(z, p1, p2, p3)
@test evalpoly(z, (p1, p2, p3)) == evpm
@test evalpoly(z, [p1, p2, p3]) == evpm
end
@test evalpoly(1+im, (2,)) == 2
@test evalpoly(1+im, [2,]) == 2
end
@testset "cis" begin
for z in (1.234, 1.234 + 5.678im)
@test cis(z) ≈ exp(im*z)
end
let z = [1.234, 5.678]
@test cis.(z) ≈ exp.(im*z)
end
end
@testset "modf" begin
@testset "$T" for T in (Float16, Float32, Float64)
@test modf(T(1.25)) === (T(0.25), T(1.0))
@test modf(T(1.0)) === (T(0.0), T(1.0))
@test modf(T(-Inf)) === (T(-0.0), T(-Inf))
@test modf(T(Inf)) === (T(0.0), T(Inf))
@test modf(T(NaN)) === (T(NaN), T(NaN))
@test modf(T(-0.0)) === (T(-0.0), T(-0.0))
@test modf(T(-1.0)) === (T(-0.0), T(-1.0))
end
end
@testset "frexp" begin
@testset "$elty" for elty in (Float16, Float32, Float64)
@test frexp( convert(elty,0.5) ) == (0.5, 0)
@test frexp( convert(elty,4.0) ) == (0.5, 3)
@test frexp( convert(elty,10.5) ) == (0.65625, 4)
end
end
@testset "log/log1p" begin
# using Tang's algorithm, should be accurate to within 0.56 ulps
X = rand(100)
for x in X
for n = -5:5
xn = ldexp(x,n)
for T in (Float32,Float64)
xt = T(x)
y = log(xt)
yb = log(big(xt))
@test abs(y-yb) <= 0.56*eps(T(yb))
y = log1p(xt)
yb = log1p(big(xt))
@test abs(y-yb) <= 0.56*eps(T(yb))
if n <= 0
y = log1p(-xt)
yb = log1p(big(-xt))
@test abs(y-yb) <= 0.56*eps(T(yb))
end
end
end
end
for n = 0:28
@test log(2,2^n) == n
end
setprecision(10_000) do
@test log(2,big(2)^100) == 100
@test log(2,big(2)^200) == 200
@test log(2,big(2)^300) == 300
@test log(2,big(2)^400) == 400
end
for T in (Float32,Float64)
@test log(zero(T)) == -Inf
@test isnan_type(T, log(T(NaN)))
@test_throws DomainError log(-one(T))
@test log1p(-one(T)) == -Inf
@test isnan_type(T, log1p(T(NaN)))
@test_throws DomainError log1p(-2*one(T))
end
@testset "log of subnormals" begin
# checked results with WolframAlpha
for (T, lr) in ((Float32, LinRange(2.f0^(-129), 2.f0^(-128), 1000)),
(Float64, LinRange(2.0^(-1025), 2.0^(-1024), 1000)))
for x in lr
@test log(x) ≈ T(log(widen(x))) rtol=2eps(T)
@test log2(x) ≈ T(log2(widen(x))) rtol=2eps(T)
@test log10(x) ≈ T(log10(widen(x))) rtol=2eps(T)
end
end
end
end
@testset "vectorization of 2-arg functions" begin
binary_math_functions = [
copysign, flipsign, log, atan, hypot, max, min,
]
@testset "$f" for f in binary_math_functions
x = y = 2
v = [f(x,y)]
@test f.([x],y) == v
@test f.(x,[y]) == v
@test f.([x],[y]) == v
end
end
@testset "issues #3024, #12822, #24240" begin
p2 = -2
p3 = -3
@test_throws DomainError 2 ^ p2
@test 2 ^ -2 == 0.25 == (2^-1)^2
@test_throws DomainError (-2)^(2.2)
@test_throws DomainError (-2.0)^(2.2)
@test_throws DomainError false ^ p2
@test false ^ -2 == Inf
@test 1 ^ -2 === (-1) ^ -2 == 1 ^ p2 === (-1) ^ p2 === 1
@test (-1) ^ -1 === (-1) ^ -3 == (-1) ^ p3 === -1
@test true ^ -2 == true ^ p2 === true
end
@testset "issue #13748" begin
let A = [1 2; 3 4]; B = [5 6; 7 8]; C = [9 10; 11 12]
@test muladd(A,B,C) == A*B + C
end
end
@testset "issue #19872" begin
f19872a(x) = x ^ 5
f19872b(x) = x ^ (-1024)
@test 0 < f19872b(2.0) < 1e-300
@test issubnormal(2.0 ^ (-1024))
@test issubnormal(f19872b(2.0))
@test !issubnormal(f19872b(0.0))
@test f19872a(2.0) === 32.0
@test !issubnormal(f19872a(2.0))
@test !issubnormal(0.0)
end
# no domain error is thrown for negative values
@test invoke(cbrt, Tuple{AbstractFloat}, -1.0) == -1.0
@testset "promote Float16 irrational #15359" begin
@test typeof(Float16(.5) * pi) == Float16
end
@testset "sincos" begin
@test sincos(1.0) === (sin(1.0), cos(1.0))
@test sincos(1f0) === (sin(1f0), cos(1f0))
@test sincos(Float16(1)) === (sin(Float16(1)), cos(Float16(1)))
@test sincos(1) === (sin(1), cos(1))
@test sincos(big(1)) == (sin(big(1)), cos(big(1)))
@test sincos(big(1.0)) == (sin(big(1.0)), cos(big(1.0)))
@test sincos(NaN) === (NaN, NaN)
@test sincos(NaN32) === (NaN32, NaN32)
@test_throws DomainError sincos(Inf32)
@test_throws DomainError sincos(Inf64)
end
@testset "test fallback definitions" begin
@test exp10(5) ≈ exp10(5.0)
@test exp10(50//10) ≈ exp10(5.0)
@test log10(exp10(ℯ)) ≈ ℯ
@test log(ℯ) === 1
@test exp2(Float16(2.0)) ≈ exp2(2.0)
@test exp2(Float16(1.0)) === Float16(exp2(1.0))
@test exp10(Float16(1.0)) === Float16(exp10(1.0))
end
@testset "isapprox" begin
# #22742: updated isapprox semantics
@test !isapprox(1.0, 1.0+1e-12, atol=1e-14)
@test isapprox(1.0, 1.0+0.5*sqrt(eps(1.0)))
@test !isapprox(1.0, 1.0+1.5*sqrt(eps(1.0)), atol=sqrt(eps(1.0)))
# #13132: Use of `norm` kwarg for scalar arguments
@test isapprox(1, 1+1.0e-12, norm=abs)
@test !isapprox(1, 1+1.0e-12, norm=x->1)
end
# test AbstractFloat fallback pr22716
struct Float22716{T<:AbstractFloat} <: AbstractFloat
x::T
end
Base.:^(x::Number, y::Float22716) = x^(y.x)
let x = 2.0
@test exp2(Float22716(x)) === 2^x
@test exp10(Float22716(x)) === 10^x
end
@testset "asin #23088" begin
for T in (Float32, Float64)
@test asin(zero(T)) === zero(T)
@test asin(-zero(T)) === -zero(T)
@test asin(nextfloat(zero(T))) === nextfloat(zero(T))
@test asin(prevfloat(zero(T))) === prevfloat(zero(T))
@test asin(one(T)) === T(pi)/2
@test asin(-one(T)) === -T(pi)/2
for x in (0.45, 0.6, 0.98)
by = asin(big(T(x)))
@test T(abs(asin(T(x)) - by))/eps(T(abs(by))) <= 1
bym = asin(big(T(-x)))
@test T(abs(asin(T(-x)) - bym))/eps(T(abs(bym))) <= 1
end
@test_throws DomainError asin(-T(Inf))
@test_throws DomainError asin(T(Inf))
@test isnan_type(T, asin(T(NaN)))
end
end
@testset "sin, cos, sincos, tan #23088" begin
for T in (Float32, Float64)
@test sin(zero(T)) === zero(T)
@test sin(-zero(T)) === -zero(T)
@test cos(zero(T)) === T(1.0)
@test cos(-zero(T)) === T(1.0)
@test sin(nextfloat(zero(T))) === nextfloat(zero(T))
@test sin(prevfloat(zero(T))) === prevfloat(zero(T))
@test cos(nextfloat(zero(T))) === T(1.0)
@test cos(prevfloat(zero(T))) === T(1.0)
for x in (0.1, 0.45, 0.6, 0.75, 0.79, 0.98)
for op in (sin, cos, tan)
by = T(op(big(x)))
@test abs(op(T(x)) - by)/eps(by) <= one(T)
bym = T(op(big(-x)))
@test abs(op(T(-x)) - bym)/eps(bym) <= one(T)
end
end
@test_throws DomainError sin(-T(Inf))
@test_throws DomainError sin(T(Inf))
@test_throws DomainError cos(-T(Inf))
@test_throws DomainError cos(T(Inf))
@test_throws DomainError tan(-T(Inf))
@test_throws DomainError tan(T(Inf))
@test sin(T(NaN)) === T(NaN)
@test cos(T(NaN)) === T(NaN)
@test tan(T(NaN)) === T(NaN)
end
end
@testset "rem_pio2 #23088" begin
vals = (2.356194490192345f0, 3.9269908169872414f0, 7.0685834705770345f0,
5.497787143782138f0, 4.216574282663131f8, 4.216574282663131f12)
for (i, x) in enumerate(vals)
for op in (prevfloat, nextfloat)
Ty = Float32(Base.Math.rem_pio2_kernel(op(vals[i]))[2].hi)
By = Float32(rem(big(op(x)), pi/2))
@test Ty ≈ By || Ty ≈ By-Float32(pi)/2
end
end
end
@testset "atan #23383" begin
for T in (Float32, Float64)
@test atan(T(NaN)) === T(NaN)
@test atan(-T(Inf)) === -T(pi)/2
@test atan(T(Inf)) === T(pi)/2
# no reduction needed |x| < 7/16
@test atan(zero(T)) === zero(T)
@test atan(prevfloat(zero(T))) === prevfloat(zero(T))
@test atan(nextfloat(zero(T))) === nextfloat(zero(T))
for x in (T(7/16), (T(7/16)+T(11/16))/2, T(11/16),
(T(11/16)+T(19/16))/2, T(19/16),
(T(19/16)+T(39/16))/2, T(39/16),
(T(39/16)+T(2)^23)/2, T(2)^23)
x = T(7/16)
by = T(atan(big(x)))
@test abs(atan(x) - by)/eps(by) <= one(T)
x = prevfloat(T(7/16))
by = T(atan(big(x)))
@test abs(atan(x) - by)/eps(by) <= one(T)
x = nextfloat(T(7/16))
by = T(atan(big(x)))
@test abs(atan(x) - by)/eps(by) <= one(T)
end
# This case was used to find a bug, but it isn't special in itself
@test atan(1.7581305072934137) ≈ 1.053644580517088
end
end
@testset "atan" begin
for T in (Float32, Float64)
@test isnan_type(T, atan(T(NaN), T(NaN)))
@test isnan_type(T, atan(T(NaN), T(0.1)))