Skip to content

Commit 288a841

Browse files
committed
add PTYQoLIrrationalConstantsExt
1 parent e4c0001 commit 288a841

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module PTYQoLIrrationalConstantsExt
2+
3+
using IrrationalConstants
4+
import Base: +, -, *, /, inv, sqrt, exp, log
5+
6+
# x+x=2x, 2x-x=x, x/2x=1/2, 2x/x=2
7+
for (x, halfx) in ((halfπ,quartπ), (π,halfπ), (twoπ,π), (fourπ,twoπ), (inv2π,inv4π), (invπ,inv2π), (twoinvπ,invπ), (fourinvπ,twoinvπ), (sqrt2,invsqrt2), (sqrt2π,sqrthalfπ), (sqrt4π,sqrtπ))
8+
@eval +(::$(typeof(halfx)), ::$(typeof(halfx))) = $x
9+
@eval -(::$(typeof(x)), ::$(typeof(halfx))) = $halfx
10+
@eval /(::$(typeof(halfx)), ::$(typeof(x))) = 1/2
11+
@eval /(::$(typeof(x)), ::$(typeof(halfx))) = 2
12+
end
13+
14+
# x*inv(x)=1
15+
for (x,invx) in ((quartπ,fourinvπ), (halfπ,twoinvπ), (π,invπ), (twoπ,inv2π), (fourπ,inv4π), (sqrt2,invsqrt2), (sqrtπ,invsqrtπ), (sqrt2π,invsqrt2π))
16+
@eval inv(::$(typeof(x))) = $invx
17+
@eval inv(::$(typeof(invx))) = $x
18+
@eval *(::$(typeof(x)), ::$(typeof(invx))) = 1
19+
@eval *(::$(typeof(invx)), ::$(typeof(x))) = 1
20+
end
21+
22+
# sqrt(x)*sqrt(x)=x, x/sqrt(x)=sqrt(x)
23+
for (x,sqrtx) in ((π,sqrtπ), (twoπ,sqrt2π), (fourπ,sqrt4π), (halfπ,sqrthalfπ), (invπ,invsqrtπ), (inv2π,invsqrt2π))
24+
@eval sqrt(::$(typeof(x))) = $sqrtx
25+
@eval *(::$(typeof(sqrtx)), ::$(typeof(sqrtx))) = $x
26+
@eval /(::$(typeof(x)), ::$(typeof(sqrtx))) = $sqrtx
27+
@eval /(::$(typeof(sqrtx)), ::$(typeof(x))) = inv($sqrtx)
28+
end
29+
30+
# exp(log(x))=x
31+
for (x,logx) in ((π,logπ), (twoπ,log2π), (fourπ,log4π))
32+
@eval log(::$(typeof(x))) = $logx
33+
@eval exp(::$(typeof(logx))) = $x
34+
end
35+
36+
end # module

test/runtests.jl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,32 @@ end
119119
@testset "AlgebraicNumbers" begin
120120
using AlgebraicNumbers
121121
end
122-
@testset "Infinities" begin
123-
using Infinities
122+
@testset "IrrationalConstants" begin
123+
using IrrationalConstants
124+
@testset "half" begin
125+
for (x, halfx) in ((halfπ,quartπ), (π,halfπ), (twoπ,π), (fourπ,twoπ), (inv2π,inv4π), (invπ,inv2π), (twoinvπ,invπ), (fourinvπ,twoinvπ), (sqrt2,invsqrt2), (sqrt2π,sqrthalfπ), (sqrt4π,sqrtπ))
126+
@test halfx + halfx === x
127+
@test x - halfx === halfx
128+
@test halfx / x === 0.5
129+
@test x / halfx === 2
130+
end
131+
end
132+
@testset "inv" begin
133+
for (x,invx) in ((quartπ,fourinvπ), (halfπ,twoinvπ), (π,invπ), (twoπ,inv2π), (fourπ,inv4π), (sqrt2,invsqrt2), (sqrtπ,invsqrtπ), (sqrt2π,invsqrt2π))
134+
@test inv(x) === invx
135+
@test inv(invx) === x
136+
@test x*invx === 1
137+
@test invx*x === 1
138+
end
139+
end
140+
@testset "sqrt" begin
141+
for (x,sqrtx) in ((π,sqrtπ), (twoπ,sqrt2π), (fourπ,sqrt4π), (halfπ,sqrthalfπ), (invπ,invsqrtπ), (inv2π,invsqrt2π))
142+
@test sqrt(x) === sqrtx
143+
@test sqrtx*sqrtx === x
144+
@test x/sqrtx === sqrtx
145+
@test sqrtx/x === inv(sqrtx)
146+
end
147+
end
124148
end
125149
end
126150

0 commit comments

Comments
 (0)