From 0ceff46f43b6dba16bcebd9081a1ce3daa8cb535 Mon Sep 17 00:00:00 2001 From: Mus M Date: Tue, 18 Oct 2016 16:05:55 -0400 Subject: [PATCH 1/3] hardware indep. isinf for hardware floats --- base/float.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/base/float.jl b/base/float.jl index 4eafa39353b21..67da6135652e3 100644 --- a/base/float.jl +++ b/base/float.jl @@ -504,6 +504,8 @@ isfinite(x::Real) = decompose(x)[3] != 0 isfinite(x::Integer) = true isinf(x::Real) = !isnan(x) & !isfinite(x) +isinf{T<:Union{Float16,Float32,Float64}}(x::T) = (reinterpret(Unsigned, x) & ~sign_mask(T)) == exponent_mask(T) + ## hashing small, built-in numeric types ## From ed845a41278e4e9fb37ce771a96613e57005ae3a Mon Sep 17 00:00:00 2001 From: Mus M Date: Wed, 19 Oct 2016 02:02:05 -0400 Subject: [PATCH 2/3] Add isnan functions --- base/float.jl | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/base/float.jl b/base/float.jl index 67da6135652e3..66217b601dd9b 100644 --- a/base/float.jl +++ b/base/float.jl @@ -2,6 +2,8 @@ ## floating point traits ## +typealias IEEEFloat Union{Float16,Float32,Float64} + """ Inf16 @@ -494,8 +496,8 @@ abs(x::Float64) = box(Float64,abs_float(unbox(Float64,x))) Test whether a floating point number is not a number (NaN). """ +isnan{T<:IEEEFloat}(x::T) = (reinterpret(Unsigned, x) & ~sign_mask(T)) > exponent_mask(T) isnan(x::AbstractFloat) = x != x -isnan(x::Float16) = reinterpret(UInt16,x)&0x7fff > 0x7c00 isnan(x::Real) = false isfinite(x::AbstractFloat) = x - x == 0 @@ -503,8 +505,13 @@ isfinite(x::Float16) = reinterpret(UInt16,x)&0x7c00 != 0x7c00 isfinite(x::Real) = decompose(x)[3] != 0 isfinite(x::Integer) = true +""" + isinf(f) -> Bool + +Test whether a floating point number is an infinity value (positive infinity or negative infinity). +""" +isinf{T<:IEEEFloat}(x::T) = (reinterpret(Unsigned, x) & ~sign_mask(T)) == exponent_mask(T) isinf(x::Real) = !isnan(x) & !isfinite(x) -isinf{T<:Union{Float16,Float32,Float64}}(x::T) = (reinterpret(Unsigned, x) & ~sign_mask(T)) == exponent_mask(T) ## hashing small, built-in numeric types ## From f02dd4e9eb739defde02cadd69287c4818811ffa Mon Sep 17 00:00:00 2001 From: Mus M Date: Wed, 19 Oct 2016 02:12:04 -0400 Subject: [PATCH 3/3] Add hard indep isfinite --- base/float.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/base/float.jl b/base/float.jl index 66217b601dd9b..f413200549c73 100644 --- a/base/float.jl +++ b/base/float.jl @@ -500,8 +500,13 @@ isnan{T<:IEEEFloat}(x::T) = (reinterpret(Unsigned, x) & ~sign_mask(T)) > exponen isnan(x::AbstractFloat) = x != x isnan(x::Real) = false +""" + isfinite(f) -> Bool + +Test whether a floating point number is finite, i.e. that `f` is neither infinite or not a number. +""" +isfinite{T<:IEEEFloat}(x::T) = (reinterpret(Unsigned, x) & exponent_mask(T)) != exponent_mask(T) isfinite(x::AbstractFloat) = x - x == 0 -isfinite(x::Float16) = reinterpret(UInt16,x)&0x7c00 != 0x7c00 isfinite(x::Real) = decompose(x)[3] != 0 isfinite(x::Integer) = true