Skip to content

Commit

Permalink
Integer(::RealInfinity) and Integer(::ComplexInfinity) (#11)
Browse files Browse the repository at this point in the history
* Integer(::RealInfinity) and Integer(::ComplexInfinity)

* Fix tests

* AVoid StackOverflows in array indexing
  • Loading branch information
dlfivefifty authored Mar 16, 2021
1 parent a0536ea commit 7b084a9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
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.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

2 comments on commit 7b084a9

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/32073

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 7b084a981103ccf0759016edf27522bb9cad2d90
git push origin v0.1.0

Please sign in to comment.