From aa5c45f2cb39efa2208ddc782f8734adf145ad45 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 22 Jun 2020 10:44:34 -0500 Subject: [PATCH 1/4] Add generic isfinite method --- base/float.jl | 14 -------------- base/number.jl | 16 ++++++++++++++++ test/numbers.jl | 8 ++++++++ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/base/float.jl b/base/float.jl index 7f4c181276640..36d49dc2bfa2b 100644 --- a/base/float.jl +++ b/base/float.jl @@ -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 isfinite(x::Float16) = reinterpret(UInt16,x)&0x7c00 != 0x7c00 isfinite(x::Real) = decompose(x)[3] != 0 diff --git a/base/number.jl b/base/number.jl index f6abff0f25bb7..d74e9f5119529 100644 --- a/base/number.jl +++ b/base/number.jl @@ -134,6 +134,22 @@ julia> abs2(-3) """ abs2(x::Real) = x*x +""" + isfinite(f) -> Bool + +Test whether a number is finite. + +# Examples +```jldoctest +julia> isfinite(5) +true + +julia> isfinite(NaN32) +false +``` +""" +isfinite(x) = iszero(x - x) + """ flipsign(x, y) diff --git a/test/numbers.jl b/test/numbers.jl index 607402e24125f..8de90b922e5a4 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2704,3 +2704,11 @@ 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 isfinite('a') == true + + @test invoke(isfinite, Tuple{Any}, 0.0) == true + @test invoke(isfinite, Tuple{Any}, NaN) == false + @test invoke(isfinite, Tuple{Any}, Inf) == false +end From 04d4f26e21b5ef91b5ec7096688212069108a12d Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 22 Jun 2020 10:45:08 -0500 Subject: [PATCH 2/4] Test isfinite for Period instances --- stdlib/Dates/test/periods.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/Dates/test/periods.jl b/stdlib/Dates/test/periods.jl index c93e7368f30d2..b726594d43c82 100644 --- a/stdlib/Dates/test/periods.jl +++ b/stdlib/Dates/test/periods.jl @@ -30,6 +30,7 @@ using Test @test sign(t) == sign(t2) == 1 @test sign(-t) == sign(-t2) == -1 @test sign(Dates.Year(0)) == 0 + @test isfinite(t) == true end @testset "div/mod/gcd/lcm/rem" begin @test Dates.Year(10) % Dates.Year(4) == Dates.Year(2) From b70a923d737dc5e6f162a2a64d07a2ce28f22420 Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 14 Sep 2020 10:13:36 -0500 Subject: [PATCH 3/4] Add isfinite method for Period --- stdlib/Dates/src/periods.jl | 1 + stdlib/Dates/test/periods.jl | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/stdlib/Dates/src/periods.jl b/stdlib/Dates/src/periods.jl index 529de24b80830..9d99645718167 100644 --- a/stdlib/Dates/src/periods.jl +++ b/stdlib/Dates/src/periods.jl @@ -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 # Default values (as used by TimeTypes) """ diff --git a/stdlib/Dates/test/periods.jl b/stdlib/Dates/test/periods.jl index b726594d43c82..13f3ff4b3c3e5 100644 --- a/stdlib/Dates/test/periods.jl +++ b/stdlib/Dates/test/periods.jl @@ -30,7 +30,6 @@ using Test @test sign(t) == sign(t2) == 1 @test sign(-t) == sign(-t2) == -1 @test sign(Dates.Year(0)) == 0 - @test isfinite(t) == true end @testset "div/mod/gcd/lcm/rem" begin @test Dates.Year(10) % Dates.Year(4) == Dates.Year(2) @@ -227,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) From 45a131a2b9c6bc50ff1ada2d906a9dba968237be Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Mon, 14 Sep 2020 14:56:19 -0500 Subject: [PATCH 4/4] Generic isfinite only for Number subtypes --- base/number.jl | 32 ++++++++++++++++---------------- test/numbers.jl | 8 +++----- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/base/number.jl b/base/number.jl index d74e9f5119529..142796d3903ac 100644 --- a/base/number.jl +++ b/base/number.jl @@ -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) = () @@ -134,22 +150,6 @@ julia> abs2(-3) """ abs2(x::Real) = x*x -""" - isfinite(f) -> Bool - -Test whether a number is finite. - -# Examples -```jldoctest -julia> isfinite(5) -true - -julia> isfinite(NaN32) -false -``` -""" -isfinite(x) = iszero(x - x) - """ flipsign(x, y) diff --git a/test/numbers.jl b/test/numbers.jl index 8de90b922e5a4..15ca861b1f86d 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2706,9 +2706,7 @@ end end @testset "generic isfinite" begin - @test isfinite('a') == true - - @test invoke(isfinite, Tuple{Any}, 0.0) == true - @test invoke(isfinite, Tuple{Any}, NaN) == false - @test invoke(isfinite, Tuple{Any}, Inf) == false + @test invoke(isfinite, Tuple{Number}, 0.0) == true + @test invoke(isfinite, Tuple{Number}, NaN) == false + @test invoke(isfinite, Tuple{Number}, Inf) == false end