Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/IrrationalConstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export
log4π # log(4π)

include("stats.jl")
include("math.jl")

end # module
23 changes: 23 additions & 0 deletions src/math.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
if VERSION > v"1.6.0"
_one(x) = one(x)
else
_one(x) = true
end

for (a,b) in [
(:twoπ, :inv2π),
(:fourπ, :inv4π),
(:halfπ, :twoinvπ),
(:quartπ, :fourinvπ),
(:sqrt2π, :invsqrt2π),
(:sqrt2, :invsqrt2),
]
Ta = Irrational{a}
Tb = Irrational{b}
@eval Base.inv(::$Ta) = $b
@eval Base.literal_pow(::typeof(^), ::$Ta, ::Val{-1}) = $b
@eval Base.inv(::$Tb) = $a
@eval Base.literal_pow(::typeof(^), ::$Tb, ::Val{-1}) = $a
@eval Base.:(*)(::$Ta, ::$Tb) = _one($Ta)
@eval Base.:(*)(::$Tb, ::$Ta) = _one($Ta)
end
17 changes: 16 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ end
@test isapprox(4/pi, fourinvπ)
end

@testset "1/(k*pi)" begin
@testset "1/(k*pi)" begin
@test isapprox(1/(2pi), inv2π)
@test isapprox(1/(4pi), inv4π)
end
Expand All @@ -38,3 +38,18 @@ end
@test isapprox(log(4pi), log4π)
end

@testset "inv" begin
for (a,b) in [
(twoπ, inv2π),
(fourπ, inv4π),
(halfπ, twoinvπ),
(quartπ, fourinvπ),
(sqrt2π, invsqrt2π),
(sqrt2, invsqrt2),
]
@test a^-1 == inv(a) == b
@test b^-1 == inv(b) == a
@test a*b == 1
@test b*a == 1
end
end