Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic isfinite method #36380

Merged
merged 4 commits into from
Sep 18, 2020
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
14 changes: 0 additions & 14 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -537,20 +537,6 @@ isnan(x::AbstractFloat) = (x != x)::Bool
isnan(x::Float16) = reinterpret(UInt16,x)&0x7fff > 0x7c00
isnan(x::Real) = false

"""
isfinite(f) -> Bool

Test whether a number is finite.

# Examples
```jldoctest
julia> isfinite(5)
true

julia> isfinite(NaN32)
false
```
"""
isfinite(x::AbstractFloat) = x - x == 0
omus marked this conversation as resolved.
Show resolved Hide resolved
isfinite(x::Float16) = reinterpret(UInt16,x)&0x7c00 != 0x7c00
isfinite(x::Real) = decompose(x)[3] != 0
Expand Down
16 changes: 16 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ true
"""
isone(x) = x == one(x) # fallback method

"""
isfinite(f) -> Bool

Test whether a number is finite.

# Examples
```jldoctest
julia> isfinite(5)
true

julia> isfinite(NaN32)
false
```
"""
isfinite(x::Number) = iszero(x - x)

size(x::Number) = ()
size(x::Number, d::Integer) = d < 1 ? throw(BoundsError()) : 1
axes(x::Number) = ()
Expand Down
1 change: 1 addition & 0 deletions stdlib/Dates/src/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Base.zero(::Union{Type{P},P}) where {P<:Period} = P(0)
Base.one(::Union{Type{P},P}) where {P<:Period} = 1 # see #16116
Base.typemin(::Type{P}) where {P<:Period} = P(typemin(Int64))
Base.typemax(::Type{P}) where {P<:Period} = P(typemax(Int64))
Base.isfinite(::Union{Type{P}, P}) where {P<:Period} = true
Copy link
Member Author

Choose a reason for hiding this comment

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

Following the lead of how isfinite is implemented for TimeType as that also supports taking the type


# Default values (as used by TimeTypes)
"""
Expand Down
2 changes: 2 additions & 0 deletions stdlib/Dates/test/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ end
@test Dates.string(Dates.Year(1)) == "1 year"
@test Dates.string(Dates.Year(-1)) == "-1 year"
@test Dates.string(Dates.Year(2)) == "2 years"
@test isfinite(Dates.Year)
@test isfinite(Dates.Year(0))
@test zero(Dates.Year) == Dates.Year(0)
@test zero(Dates.Year(10)) == Dates.Year(0)
@test zero(Dates.Month) == Dates.Month(0)
Expand Down
6 changes: 6 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2704,3 +2704,9 @@ end
@test all(m -> m.file == Symbol("deprecated.jl"),
collect(methods(T))[findall(R -> !(R<:T), Base.return_types(T))])
end

@testset "generic isfinite" begin
@test invoke(isfinite, Tuple{Number}, 0.0) == true
@test invoke(isfinite, Tuple{Number}, NaN) == false
@test invoke(isfinite, Tuple{Number}, Inf) == false
end