Currently, it seems Inf is only defined for Float*. I am wondering if it possible to extend this to other data types, such as Integer? Then just like zero and one, we can have
julia> infinity(1.0)
Inf
julia> infinity(1)
# An `Inf` of type `Int`.
Currently, I mimic the behavior using typemin and typemax.
function infinity(::T) where T<:Real
return typemax(T)
end
function isinfinity(n::T) where T<:Real
return n == infinity(n)
end
Edited: I really have to narrow Number to Real since the infinity of Complex is undefined?
Edited 2: After read all suggestions, I come up following solution: just use a Union type. Thanks for all!
struct MyPhysParameter{T<:Real}
value_type::T
allowed_min::Union{T, typeof(Inf)}
allowed_max::Union{T, typeof(Inf)}
end
@Tamas_Papp’s answer and others proposing/implementing infinity types are a good ones.
If you want to avoid the Union…I started working on a NaNIntegers package, but never finished it. Here’s the start: https://gist.github.com/timholy/569475b24763d1fbd36a42634192cb74. It uses the second-from-top-bit to indicate NaN. You could modify this to use the third-from-top-bit to indicate infinity. Note you have to write your own rules of mathematics, so there’s still a bit of work to do.
I’d generally recommend against mimicking infinity with typemax:
julia> i = typemax(Int)
9223372036854775807
julia> i+1 == i
false
julia> 2*i == i
false
julia> i-1 == i
false
Any infinity that doesn’t obey those properties is not really infinity.
Great suggestions. However, my usage of infinity is not as ambitious as yours. I only intend to use it as a indicator to mark the infinity, that is to say I will never do any math operations on it.
Here is what I want to do. I want to define a type which describes a physical parameter. Now, with this type I can dispatch some physical computation on it.
struct MyPhysParameter{T<:Real}
description::String
variable_name::String
ascii_label::String
allowed_min::T # the minimum value allowed for this parameter, can be negative infinity
allowed_max::T # the maximum value allowed for this parameter, can be positive infinity
end
So I will need a type stable way to define the infinity.
Typical dispatch case
function compute(::MyPhysParameter, data)
# use the bounds to `rand` or check data validity, etc.
end
If you want to encode the min/max, why not just use typemin/typemax?
Edit:
I only intend to use it as a indicator to mark the infinity, that is to say I will never do any math operations on it.
The problem I foresee with calling this Inf is, what happens when you come back to this after six months away and then forget that you can’t do math operations on it? Calling it Inf is a slippery slope…