Skip to content
Merged
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 <solver@mac.com>"]
version = "0.0.2"
version = "0.1.0"

[compat]
julia = "1"
Expand Down
2 changes: 1 addition & 1 deletion src/Infinities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Infinities

import Base: angle, isone, iszero, isinf, isfinite, abs, one, zero, isless,
+, -, *, ==, <, ≤, >, ≥, fld, cld, div, mod, min, max, sign, signbit,
string, show, promote_rule, convert
string, show, promote_rule, convert, getindex

export ∞, ℵ₀, ℵ₁, RealInfinity, ComplexInfinity, InfiniteCardinal, NotANumber
# The following is commented out for now to avoid conflicts with Infinity.jl
Expand Down
23 changes: 22 additions & 1 deletion src/cardinality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ isinf(::InfiniteCardinal) = true
isfinite(::InfiniteCardinal) = false

Integer(::Infinity) = InfiniteCardinal{0}()
function Integer(x::RealInfinity)
signbit(x) && throw(InexactError(:Integer, Integer, x))
ℵ₀
end
function Integer(x::ComplexInfinity)
iszero(angle(x)) || throw(InexactError(:Integer, Integer, x))
ℵ₀
end

==(::InfiniteCardinal{N}, ::InfiniteCardinal{N}) where N = true
==(::InfiniteCardinal, ::InfiniteCardinal)= false
Expand Down Expand Up @@ -187,4 +195,17 @@ Base.Checked.checked_mul(::InfiniteCardinal{0}, x::Integer) = sign(x)*∞
##

Base.hash(::InfiniteCardinal{0}) = 0x775431eef01bca90 # made up
Base.hash(::InfiniteCardinal{1}) = 0x55437c69b794f8ce # made up
Base.hash(::InfiniteCardinal{1}) = 0x55437c69b794f8ce # made up


# avoid stack overflow
getindex(A::Array, i1::InfiniteCardinal{0}, I::Integer...) = throw(BoundsError(A, i1, I...))

Base._unsafe_getindex(::IndexStyle, A::AbstractArray, I::InfiniteCardinal{0}) = error("Overload getindex(::$(typeof(A)), ::InfiniteCardinal{0})")

# Avoid too-strict restrictions in SubArray
function getindex(V::SubArray{T,N}, I::Vararg{InfiniteCardinal{0},N}) where {T,N}
@boundscheck checkbounds(V, I...)
@inbounds r = V.parent[Base.reindex(V.indices, I)...]
r
end
9 changes: 9 additions & 0 deletions test/test_cardinality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ using Infinities, Base64, Base.Checked, Test
@test zero(ℵ₀) ≡ 0
@test one(ℵ₀) ≡ 1
@test isinf(ℵ₀) && !isfinite(ℵ₀)
@test Integer(RealInfinity()) ≡ Integer(ComplexInfinity()) ≡ ℵ₀
@test_throws InexactError Integer(-∞)
@test_throws InexactError Integer(exp(0.1im)*∞)
end

@testset "equality" begin
Expand Down Expand Up @@ -125,4 +128,10 @@ using Infinities, Base64, Base.Checked, Test
@test checked_sub(5, ℵ₀) ≡ -∞
@test checked_mul(-5, ℵ₀) ≡ checked_mul(ℵ₀, -5) ≡ -∞
end

@testset "indexing" begin
@test_throws BoundsError randn(3)[ℵ₀]
@test_throws ErrorException Base._unsafe_getindex(IndexCartesian(),permutedims(1:3)',ℵ₀)
@test_throws BoundsError view(randn(3),1:2)[ℵ₀]
end
end