forked from JuliaMath/DoubleFloats.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic.jl
95 lines (80 loc) · 3.31 KB
/
arithmetic.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
@testset "Arithmetic" begin
x = rand(Complex{Double64})
y = rand(Double64)
@test x + y isa Complex{Double64}
@test y + x isa Complex{Double64}
@test x - y isa Complex{Double64}
@test y - x isa Complex{Double64}
@test x * y isa Complex{Double64}
@test y * x isa Complex{Double64}
@test x / y isa Complex{Double64}
@test y / x isa Complex{Double64}
@test x ^ y isa Complex{Double64}
@test y ^ x isa Complex{Double64}
@test x ^ x isa Complex{Double64}
@test y ^ y isa Double64
x, y = rand(2)
@test add2(x, y) == (Double64(x) + Double64(y))
@test sub2(x, y) == (Double64(x) - Double64(y))
@test mul2(x, y) == (Double64(x) * Double64(y))
@test div2(x, y) == (Double64(x) / Double64(y))
@test x ⊕ y == (Double64(x) + Double64(y))
@test x ⊖ y == (Double64(x) - Double64(y))
@test x ⊗ y == (Double64(x) * Double64(y))
@test x ⊘ y == (Double64(x) / Double64(y))
end
@testset "div,rem,.." begin
a = 17.0
b = 5.0
da = Double64(a)
db = Double64(b)
@test div(da, db) == div(a, b)
@test cld(da, db) == cld(a, b)
@test fld(da, db) == fld(a, b)
@test rem(da, db) == rem(a, b)
@test mod(da, db) == mod(a, b)
@test divrem(da, db) == divrem(a, b)
@test fldmod(da, db) == fldmod(a, b)
end
@testset "Trig functions" begin
x = rand(Double64)
sinx, cosx = sincos(x)
@test sinx ≈ sin(x)
@test cosx ≈ cos(x)
end
@testset "promotion" begin
@test df64"123" - df64"123" * df64"0.1" * df64"10.0" == 0.0
@test df64"123" - 123 * df64"0.1" * df64"10.0" == 0.0
end
@testset "modpi" begin
@test iszero(DoubleFloats.mod1pi(Double64(pi)))
@test iszero(DoubleFloats.mod2pi(2*Double64(pi)))
@test iszero(DoubleFloats.modhalfpi(0.5*Double64(pi)))
@test iszero(DoubleFloats.modqrtrpi(0.25*Double64(pi)))
@test iszero(DoubleFloats.mod1pi(Double32(pi)))
@test iszero(DoubleFloats.mod2pi(2*Double32(pi)))
@test iszero(DoubleFloats.modhalfpi(0.5*Double32(pi)))
@test iszero(DoubleFloats.modqrtrpi(0.25*Double32(pi)))
@test 0 < DoubleFloats.mod1pi(Double64(pi)*10) < eps(Double64(pi)*10)
@test 0 < DoubleFloats.mod2pi(Double64(pi)*10) < eps(2*Double64(pi)*10)
@test 0 < DoubleFloats.modhalfpi(Double64(pi)*10) < eps(0.5*Double64(pi)*10)
@test 0 < DoubleFloats.modqrtrpi(0.25*Double64(pi)*10) < eps(0.25*Double64(pi)*10)
@test 0 < DoubleFloats.mod1pi(Double32(pi)*10) < eps(Double32(pi)*10)
@test 0 < DoubleFloats.mod2pi(Double32(pi)*10) < eps(2*Double32(pi)*10)
@test 0 < DoubleFloats.modhalfpi(Double32(pi)*10) < eps(0.5*Double32(pi)*10)
@test 0 < DoubleFloats.modqrtrpi(0.25*Double32(pi)*10) < eps(0.25*Double32(pi)*10)
end
@testset "rempi" begin
@test iszero(DoubleFloats.rem1pi(Double64(pi)))
@test iszero(DoubleFloats.rem2pi(2*Double64(pi)))
@test iszero(DoubleFloats.remhalfpi(0.5*Double64(pi)))
@test iszero(DoubleFloats.remqrtrpi(0.25*Double64(pi)))
@test iszero(DoubleFloats.rem1pi(Double32(pi)))
@test iszero(DoubleFloats.rem2pi(2*Double32(pi)))
@test iszero(DoubleFloats.remhalfpi(0.5*Double32(pi)))
@test iszero(DoubleFloats.remqrtrpi(0.25*Double32(pi)))
end
@testset "hypot" begin
@test DoubleFloats.hypot(Double64(1), Double64(1))^2 == 2
@test sum(DoubleFloats.normalize(Double64(1), Double64(1)))^2 == 2
end