Skip to content

Commit

Permalink
Support arithmetic with floats and InfiniteCardinal (#14)
Browse files Browse the repository at this point in the history
* Support arithmetic with floats and InfiniteCardinal

* one(InfiniteCardinal)

* convert to float for RealInfinity, oneunit overrides

* increase coverage
  • Loading branch information
dlfivefifty authored Sep 2, 2021
1 parent 792229a commit 622c39e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
version:
- '1.0'
- '1'
- '^1.6.0-0'
- '^1.7.0-0'
os:
- ubuntu-latest
- macOS-latest
Expand Down
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.0"
version = "0.1.1"

[compat]
julia = "1"
Expand Down
14 changes: 13 additions & 1 deletion src/Infinities.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Infinities

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

Expand Down Expand Up @@ -42,6 +42,8 @@ sign(y::Infinity) = 1
angle(x::Infinity) = 0

one(::Type{Infinity}) = 1
oneunit(::Type{Infinity}) = 1
oneunit(::Infinity) = 1
zero(::Infinity) = 0

isinf(::Infinity) = true
Expand Down Expand Up @@ -139,6 +141,16 @@ isfinite(::RealInfinity) = false
promote_rule(::Type{Infinity}, ::Type{RealInfinity}) = RealInfinity
convert(::Type{RealInfinity}, ::Infinity) = RealInfinity(false)

convert(::Type{Float64}, x::RealInfinity) = sign(x)*Inf64
convert(::Type{Float32}, x::RealInfinity) = sign(x)*Inf32
convert(::Type{Float16}, x::RealInfinity) = sign(x)*Inf16
Base.Float64(x::RealInfinity) = convert(Float64, x)
Base.Float32(x::RealInfinity) = convert(Float32, x)
Base.Float16(x::RealInfinity) = convert(Float16, x)
Base.BigFloat(x::RealInfinity) = sign(x)*BigFloat(Inf)
convert(::Type{AF}, x::RealInfinity) where AF<:AbstractFloat = sign(x)*convert(AF, Inf)


signbit(y::RealInfinity) = y.signbit
sign(y::RealInfinity) = 1-2signbit(y)
angle(x::RealInfinity) = π*signbit(x)
Expand Down
25 changes: 17 additions & 8 deletions src/cardinality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ sign(::InfiniteCardinal) = 1
angle(::InfiniteCardinal) = 0
abs(a::InfiniteCardinal) = a
zero(::InfiniteCardinal) = 0
one(::InfiniteCardinal) = 1
one(::Type{<:InfiniteCardinal}) = 1
oneunit(::Type{<:InfiniteCardinal}) = 1
oneunit(::InfiniteCardinal) = 1

isinf(::InfiniteCardinal) = true
isfinite(::InfiniteCardinal) = false
Expand Down Expand Up @@ -153,11 +155,18 @@ end
-(::InfiniteCardinal) = -
-(x::Integer, ::InfiniteCardinal) = x -

for op in (:+, :-)
@eval begin
$op(x::Number, ::InfiniteCardinal) = $op(x, ∞)
$op(::InfiniteCardinal, x::Number) = $op(∞, x)
end
end

for OP in (:fld,:cld,:div)
@eval begin
$OP(x::InfiniteCardinal, ::Real) = x
$OP(::InfiniteCardinal, ::InfiniteCardinal) = NotANumber()
end
@eval begin
$OP(x::InfiniteCardinal, ::Real) = x
$OP(::InfiniteCardinal, ::InfiniteCardinal) = NotANumber()
end
end

div(::T, ::InfiniteCardinal) where T<:Real = zero(T)
Expand Down Expand Up @@ -205,7 +214,7 @@ Base._unsafe_getindex(::IndexStyle, A::AbstractArray, I::InfiniteCardinal{0}) =

# Avoid too-strict restrictions in SubArray
function getindex(V::SubArray{T,N}, i1::InfiniteCardinal{0}, I::Integer...) where {T,N}
@boundscheck checkbounds(V, i1, I...)
@inbounds r = V.parent[Base.reindex(V.indices, tuple(i1, I...))...]
r
@boundscheck checkbounds(V, i1, I...)
@inbounds r = V.parent[Base.reindex(V.indices, tuple(i1, I...))...]
r
end
9 changes: 8 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Infinities: Infinity
@test*
@test-isa NotANumber

@test one(∞) 1
@test one(∞) one(Infinity) oneunit(∞) oneunit(Infinity) 1
@test zero(∞) 0

@test !isone(∞)
Expand Down Expand Up @@ -191,6 +191,13 @@ import Infinities: Infinity
@test max(5, RealInfinity()) max(RealInfinity(), 5) RealInfinity()
@test max(5, -∞) max(-∞, 5) 5
end

@testset "convert" begin
@test convert(Float64, -∞) Float64(-∞) -Inf
@test convert(Float32, -∞) Float32(-∞) -Inf32
@test convert(Float16, -∞) Float16(-∞) -Inf16
@test convert(BigFloat, -∞)::BigFloat == BigFloat(-∞)::BigFloat == -BigFloat(Inf)
end
end

@testset "ComplexInfinity" begin
Expand Down
7 changes: 6 additions & 1 deletion test/test_cardinality.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using Infinities, Base64, Base.Checked, Test
@test Integer(∞) convert(Integer,∞) Integer(ℵ₀) convert(Integer, ℵ₀) ℵ₀
@test abs(ℵ₀) ℵ₀
@test zero(ℵ₀) 0
@test one(ℵ₀) 1
@test one(ℵ₀) one(InfiniteCardinal{0}) oneunit(ℵ₀) oneunit(InfiniteCardinal{0}) 1
@test isinf(ℵ₀) && !isfinite(ℵ₀)
@test Integer(RealInfinity()) Integer(ComplexInfinity()) ℵ₀
@test_throws InexactError Integer(-∞)
Expand Down Expand Up @@ -94,6 +94,11 @@ using Infinities, Base64, Base.Checked, Test
@test 5 * ℵ₀ ℵ₀ * 5 ℵ₀
@test 5.0ℵ₀ ℵ₀*5.0 RealInfinity()
@test_throws ArgumentError (-5) * ℵ₀

@test ℵ₀ - 5.1
@test ℵ₀ + 5.1
@test 5.1 + ℵ₀
@test 5.1 - ℵ₀ -
end

@testset "fld/cld/div/mod" begin
Expand Down

2 comments on commit 622c39e

@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/44034

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.1 -m "<description of version>" 622c39ee9756cc65714ada1476212da924c08dae
git push origin v0.1.1

Please sign in to comment.