Skip to content

Commit 7b084a9

Browse files
authored
Integer(::RealInfinity) and Integer(::ComplexInfinity) (#11)
* Integer(::RealInfinity) and Integer(::ComplexInfinity) * Fix tests * AVoid StackOverflows in array indexing
1 parent a0536ea commit 7b084a9

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Infinities"
22
uuid = "e1ba4f0e-776d-440f-acd9-e1d2e9742647"
33
authors = ["Sheehan Olver <[email protected]>"]
4-
version = "0.0.2"
4+
version = "0.1.0"
55

66
[compat]
77
julia = "1"

src/Infinities.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Infinities
22

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

77
export ∞, ℵ₀, ℵ₁, RealInfinity, ComplexInfinity, InfiniteCardinal, NotANumber
88
# The following is commented out for now to avoid conflicts with Infinity.jl

src/cardinality.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ isinf(::InfiniteCardinal) = true
3434
isfinite(::InfiniteCardinal) = false
3535

3636
Integer(::Infinity) = InfiniteCardinal{0}()
37+
function Integer(x::RealInfinity)
38+
signbit(x) && throw(InexactError(:Integer, Integer, x))
39+
ℵ₀
40+
end
41+
function Integer(x::ComplexInfinity)
42+
iszero(angle(x)) || throw(InexactError(:Integer, Integer, x))
43+
ℵ₀
44+
end
3745

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

189197
Base.hash(::InfiniteCardinal{0}) = 0x775431eef01bca90 # made up
190-
Base.hash(::InfiniteCardinal{1}) = 0x55437c69b794f8ce # made up
198+
Base.hash(::InfiniteCardinal{1}) = 0x55437c69b794f8ce # made up
199+
200+
201+
# avoid stack overflow
202+
getindex(A::Array, i1::InfiniteCardinal{0}, I::Integer...) = throw(BoundsError(A, i1, I...))
203+
204+
Base._unsafe_getindex(::IndexStyle, A::AbstractArray, I::InfiniteCardinal{0}) = error("Overload getindex(::$(typeof(A)), ::InfiniteCardinal{0})")
205+
206+
# Avoid too-strict restrictions in SubArray
207+
function getindex(V::SubArray{T,N}, I::Vararg{InfiniteCardinal{0},N}) where {T,N}
208+
@boundscheck checkbounds(V, I...)
209+
@inbounds r = V.parent[Base.reindex(V.indices, I)...]
210+
r
211+
end

test/test_cardinality.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ using Infinities, Base64, Base.Checked, Test
1111
@test zero(ℵ₀) 0
1212
@test one(ℵ₀) 1
1313
@test isinf(ℵ₀) && !isfinite(ℵ₀)
14+
@test Integer(RealInfinity()) Integer(ComplexInfinity()) ℵ₀
15+
@test_throws InexactError Integer(-∞)
16+
@test_throws InexactError Integer(exp(0.1im)*∞)
1417
end
1518

1619
@testset "equality" begin
@@ -125,4 +128,10 @@ using Infinities, Base64, Base.Checked, Test
125128
@test checked_sub(5, ℵ₀) -
126129
@test checked_mul(-5, ℵ₀) checked_mul(ℵ₀, -5) -
127130
end
131+
132+
@testset "indexing" begin
133+
@test_throws BoundsError randn(3)[ℵ₀]
134+
@test_throws ErrorException Base._unsafe_getindex(IndexCartesian(),permutedims(1:3)',ℵ₀)
135+
@test_throws BoundsError view(randn(3),1:2)[ℵ₀]
136+
end
128137
end

0 commit comments

Comments
 (0)