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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Infinities"
uuid = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
authors = ["Sheehan Olver <[email protected]>"]
version = "0.1.12"
version = "0.1.13"

[compat]
Aqua = "0.8"
Expand Down
9 changes: 6 additions & 3 deletions src/Infinities.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Infinities

import Base: angle, isone, iszero, isinf, isfinite, abs, one, oneunit, zero, isless, inv,
+, -, *, ==, <, ≤, >, ≥, fld, cld, div, mod, min, max, sign, signbit,
+, -, *, /, \, ==, <, ≤, >, ≥, fld, cld, div, mod, min, max, sign, signbit,
string, show, promote_rule, convert, getindex,
Bool, Integer
Bool, Integer, round, float, conj

export ∞, ℵ₀, ℵ₁, RealInfinity, ComplexInfinity, InfiniteCardinal, NotANumber
# The following is commented out for now to avoid conflicts with Infinity.jl
Expand Down Expand Up @@ -112,10 +112,13 @@ convert(::Type{ComplexInfinity{T}}, ::Infinity) where T = ComplexInfinity{T}()
convert(::Type{ComplexInfinity}, ::Infinity) = ComplexInfinity()
convert(::Type{ComplexInfinity{T}}, x::RealInfinity) where T = ComplexInfinity{T}(x)
convert(::Type{ComplexInfinity}, x::RealInfinity) = ComplexInfinity(x)
float(x::ComplexInfinity) = exp(im*angle(x)) * Inf


sign(y::ComplexInfinity{<:Integer}) = mod(y.signbit,2) == 0 ? 1 : -1
angle(x::ComplexInfinity) = π*x.signbit
angle(x::ComplexInfinity) = π*mod(x.signbit, 2)
conj(x::ComplexInfinity{Bool}) = x
conj(x::ComplexInfinity) = ComplexInfinity(-x.signbit)

show(io::IO, x::ComplexInfinity) = print(io, "exp($(x.signbit)*im*π)∞")

Expand Down
14 changes: 13 additions & 1 deletion src/algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
*(x::AllInfinities, y::AllInfinities) = _mul(x, y)
*(x::InfiniteCardinal, y::InfiniteCardinal) = max(x, y)

/(x::AllInfinities, y::Number) = x * inv(y)
/(x::AllInfinities, y::Rational) = x * inv(y)
/(x::AllInfinities, y::Complex) = x * inv(y)
/(x::Number, y::AllInfinities) = x / float(y)
/(x::Complex, y::AllInfinities) = x / float(y)
/(x::Rational, y::Union{Infinity,InfiniteCardinal}) = zero(x)
/(x::Rational, y::Union{RealInfinity,ComplexInfinity}) = x/ float(y)
/(x::AllInfinities, y::AllInfinities) = x * inv(y)

# just conventions somehow
*(::Infinity, y::InfiniteCardinal) = y
*(x::InfiniteCardinal, ::Infinity) = x
Expand Down Expand Up @@ -93,4 +102,7 @@ end
# inv
inv(::Union{Infinity,InfiniteCardinal}) = 0
inv(x::RealInfinity) = inv(float(x))
inv(x::ComplexInfinity) = zero(ComplexF64)
inv(x::ComplexInfinity) = zero(ComplexF64)

# round
round(x::AllInfinities, mode::RoundingMode; kwds...) = round(float(x), mode; kwds...)
33 changes: 33 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ using Aqua
end

@test Base.to_index(RealInfinity()) ≡ ℵ₀


@testset "round" begin
@test round(+∞, RoundNearest) ≡ Inf
@test round(-∞, RoundNearest) ≡ -Inf
end
end

@testset "ComplexInfinity" begin
Expand Down Expand Up @@ -288,6 +294,11 @@ using Aqua

@test signbit(ComplexInfinity(3))
@test !signbit(ComplexInfinity(100))

@test conj(exp(im)∞) ≡ exp(-im)∞
@test conj(ComplexInfinity(1)) == ComplexInfinity(1)
@test ComplexInfinity(1) == -∞
@test ComplexInfinity(-1) == -∞
end

@testset "Set" begin
Expand Down Expand Up @@ -324,6 +335,28 @@ using Aqua
@test zero(-∞) ≡ zero(RealInfinity) ≡ 0.0
@test zero(exp(0.1im)∞) ≡ zero(ComplexInfinity) ≡ 0.0+0.0im
end

@testset "division" begin
@test (+∞) / 2.0 ≡ 2.0 \ (+∞) ≡ (+∞) / (2 //3) ≡ +∞
@test (+∞) / (1+im) ≡ (1+im) \ (+∞) ≡ exp(-im*π/4)*∞
@test (+∞) / (-2.0) ≡ (-2.0) \ (+∞) ≡ -∞
@test (-∞) / 2.0 ≡ (2.0) \ (-∞) ≡ -∞
@test (-∞) / (-2.0) ≡ (-2.0) \ (-∞) ≡ +∞

@test +∞ / 0 ≡ +∞ / 0.0 ≡ -∞ / (-0.0) ≡ +∞
@test -∞ / 0 ≡ -∞ / 0.0 ≡ +∞ / (-0.0) ≡ -∞

@test 2 / (+∞) ≡ 2.0 / (+∞) ≡ -2.0 / (-∞) ≡ 0.0
@test 2 / (-∞) ≡ 2.0 / (-∞) ≡ -2.0 / (+∞) ≡ -0.0
@test (1+im) / (+∞) ≡ (+∞) \ (1+im) ≡ 0.0+0.0im
@test (1+im) / (exp(im)∞) ≡ (1+im) / (exp(im)Inf) ≡ 0.0-0.0im
@test (exp(im)∞) \ (1+im) ≡ (exp(im)Inf) \ (1+im) ≡ 0.0+0.0im
@test (2//3) / ∞ ≡ 0 // 1
@test (2//3) / (+∞) ≡ 0.0
@test (2//3) / (exp(im*π/4)∞) ≡ 0.0 - 0.0im

@test_throws ArgumentError +∞ / +∞
end
end


Expand Down
Loading