From 7cda88746c6730840798d74c66ef8a5a6cdd0614 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 23 Oct 2014 00:29:55 -0400 Subject: [PATCH] deprecate functions inf() and nan() --- base/complex.jl | 22 +++++++++++----------- base/deprecated.jl | 5 +++++ base/exports.jl | 2 -- base/float.jl | 11 +---------- base/linalg/dense.jl | 2 +- base/mpfr.jl | 9 +++------ base/special/erf.jl | 12 ++++++------ base/special/trig.jl | 8 ++++---- base/test.jl | 4 ++-- test/arrayops.jl | 2 +- 10 files changed, 34 insertions(+), 43 deletions(-) diff --git a/base/complex.jl b/base/complex.jl index 58fa5dc87277c..5410737299c6c 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -338,12 +338,12 @@ function exp(z::Complex) if isnan(zr) Complex(zr, zi==0 ? zi : zr) elseif !isfinite(zi) - if zr == inf(zr) - Complex(-zr, nan(zr)) - elseif zr == -inf(zr) + if zr == Inf + Complex(-zr, oftype(zr,NaN)) + elseif zr == -Inf Complex(-zero(zr), copysign(zero(zi), zi)) else - Complex(nan(zr), nan(zi)) + Complex(oftype(zr,NaN), oftype(zi,NaN)) end else er = exp(zr) @@ -360,12 +360,12 @@ function expm1(z::Complex) if isnan(zr) Complex(zr, zi==0 ? zi : zr) elseif !isfinite(zi) - if zr == inf(zr) - Complex(-zr, nan(zr)) - elseif zr == -inf(zr) + if zr == Inf + Complex(-zr, oftype(zr,NaN)) + elseif zr == -Inf Complex(-one(zr), copysign(zero(zi), zi)) else - Complex(nan(zr), nan(zi)) + Complex(oftype(zr,NaN), oftype(zi,NaN)) end else erm1 = expm1(zr) @@ -391,9 +391,9 @@ function log1p{T}(z::Complex{T}) elseif isnan(zr) Complex(zr, zr) elseif isfinite(zi) - Complex(inf(T), copysign(zr > 0 ? zero(T) : convert(T, pi), zi)) + Complex(T(Inf), copysign(zr > 0 ? zero(T) : convert(T, pi), zi)) else - Complex(inf(T), nan(T)) + Complex(T(Inf), T(NaN)) end end @@ -636,7 +636,7 @@ function acosh(z::Complex) return Complex(oftype(zr, NaN), oftype(zi, NaN)) end elseif zr==-Inf && zi===-0.0 #Edge case is wrong - WHY? - return Complex(inf(zr), oftype(zi, -pi)) + return Complex(oftype(zr,Inf), oftype(zi, -pi)) end ξ = asinh(real(sqrt(conj(z-1))*sqrt(z+1))) η = 2atan2(imag(sqrt(z-1)),real(sqrt(z+1))) diff --git a/base/deprecated.jl b/base/deprecated.jl index 432da85b6deaf..8158e74a80f2c 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -205,3 +205,8 @@ end @deprecate itrunc{T<:Integer}(::Type{T}, n::Integer) (n % T) @deprecate oftype{T}(::Type{T},c) convert(T,c) + +@deprecate inf(x::FloatingPoint) oftype(x,Inf) +@deprecate nan(x::FloatingPoint) oftype(x,NaN) +@deprecate inf{T<:FloatingPoint}(::Type{T}) convert(T,Inf) +@deprecate nan{T<:FloatingPoint}(::Type{T}) convert(T,NaN) diff --git a/base/exports.jl b/base/exports.jl index ac9df7c3df390..e98d8954f3dc1 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -361,7 +361,6 @@ export iceil, ifloor, imag, - inf, int, int128, int16, @@ -402,7 +401,6 @@ export mod1, modf, mod2pi, - nan, nextfloat, nextpow, nextpow2, diff --git a/base/float.jl b/base/float.jl index a4cba309e8c41..52b460f456cc5 100644 --- a/base/float.jl +++ b/base/float.jl @@ -253,15 +253,6 @@ nextfloat(x::FloatingPoint) = nextfloat(x,1) prevfloat(x::FloatingPoint) = nextfloat(x,-1) @eval begin - inf(::Type{Float16}) = $Inf16 - nan(::Type{Float16}) = $NaN16 - inf(::Type{Float32}) = $Inf32 - nan(::Type{Float32}) = $NaN32 - inf(::Type{Float64}) = $Inf - nan(::Type{Float64}) = $NaN - inf{T<:FloatingPoint}(x::T) = inf(T) - nan{T<:FloatingPoint}(x::T) = nan(T) - issubnormal(x::Float32) = (abs(x) < $(box(Float32,unbox(Uint32,0x00800000)))) & (x!=0) issubnormal(x::Float64) = (abs(x) < $(box(Float64,unbox(Uint64,0x0010000000000000)))) & (x!=0) @@ -285,7 +276,7 @@ prevfloat(x::FloatingPoint) = nextfloat(x,-1) realmin() = realmin(Float64) realmax() = realmax(Float64) - eps(x::FloatingPoint) = isfinite(x) ? abs(x) >= realmin(x) ? ldexp(eps(typeof(x)),exponent(x)) : nextfloat(zero(x)) : nan(x) + eps(x::FloatingPoint) = isfinite(x) ? abs(x) >= realmin(x) ? ldexp(eps(typeof(x)),exponent(x)) : nextfloat(zero(x)) : oftype(x,NaN) eps(::Type{Float16}) = $(box(Float16,unbox(Uint16,0x1400))) eps(::Type{Float32}) = $(box(Float32,unbox(Uint32,0x34000000))) eps(::Type{Float64}) = $(box(Float64,unbox(Uint64,0x3cb0000000000000))) diff --git a/base/linalg/dense.jl b/base/linalg/dense.jl index d593d54bc2cc5..c105b18fcb479 100644 --- a/base/linalg/dense.jl +++ b/base/linalg/dense.jl @@ -445,7 +445,7 @@ function cond(A::StridedMatrix, p::Real=2) if p == 2 v = svdvals(A) maxv = maximum(v) - return maxv == 0.0 ? inf(typeof(real(A[1,1]))) : maxv / minimum(v) + return maxv == 0.0 ? oftype(real(A[1,1]),Inf) : maxv / minimum(v) elseif p == 1 || p == Inf chksquare(A) return cond(lufact(A), p) diff --git a/base/mpfr.jl b/base/mpfr.jl index b3bbc956663a6..23105f878e76d 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -16,7 +16,7 @@ import gamma, lgamma, digamma, erf, erfc, zeta, eta, log1p, airyai, iceil, ifloor, itrunc, eps, signbit, sin, cos, tan, sec, csc, cot, acos, asin, atan, cosh, sinh, tanh, sech, csch, coth, acosh, asinh, atanh, atan2, - serialize, deserialize, inf, nan, cbrt, typemax, typemin, + serialize, deserialize, cbrt, typemax, typemin, realmin, realmax, get_rounding, set_rounding, maxintfloat, widen, significand, frexp @@ -654,10 +654,7 @@ end isfinite(x::BigFloat) = !isinf(x) && !isnan(x) -@eval inf(::Type{BigFloat}) = $(BigFloat(Inf)) -@eval nan(::Type{BigFloat}) = $(BigFloat(NaN)) - -typemax(::Type{BigFloat}) = inf(BigFloat) +@eval typemax(::Type{BigFloat}) = $(BigFloat( Inf)) @eval typemin(::Type{BigFloat}) = $(BigFloat(-Inf)) function nextfloat(x::BigFloat) @@ -679,7 +676,7 @@ end eps(::Type{BigFloat}) = nextfloat(BigFloat(1)) - BigFloat(1) realmin(::Type{BigFloat}) = nextfloat(zero(BigFloat)) -realmax(::Type{BigFloat}) = prevfloat(inf(BigFloat)) +realmax(::Type{BigFloat}) = prevfloat(BigFloat(Inf)) function with_bigfloat_precision(f::Function, precision::Integer) old_precision = get_bigfloat_precision() diff --git a/base/special/erf.jl b/base/special/erf.jl index 02121e056035b..5b2ab18ab26c3 100644 --- a/base/special/erf.jl +++ b/base/special/erf.jl @@ -30,9 +30,9 @@ function erfinv(x::Float64) a = abs(x) if a >= 1.0 if x == 1.0 - return inf(Float64) + return Inf elseif x == -1.0 - return -inf(Float64) + return -Inf end throw(DomainError()) elseif a <= 0.75 # Table 17 in Blair et al. @@ -98,9 +98,9 @@ function erfinv(x::Float32) a = abs(x) if a >= 1.0f0 if x == 1.0f0 - return inf(Float32) + return Inf32 elseif x == -1.0f0 - return -inf(Float32) + return -Inf32 end throw(DomainError()) elseif a <= 0.75f0 # Table 10 in Blair et al. @@ -147,7 +147,7 @@ function erfcinv(y::Float64) return erfinv(1.0 - y) elseif y <= 0.0 if y == 0.0 - return inf(Float64) + return Inf end throw(DomainError()) elseif y >= 1e-100 # Table 57 in Blair et al. @@ -199,7 +199,7 @@ function erfcinv(y::Float32) return erfinv(1.0f0 - y) elseif y <= 0.0f0 if y == 0.0f0 - return inf(Float32) + return Inf32 end throw(DomainError()) else # Table 50 in Blair et al. diff --git a/base/special/trig.jl b/base/special/trig.jl index 24796f790160f..5486f412af6b9 100644 --- a/base/special/trig.jl +++ b/base/special/trig.jl @@ -100,7 +100,7 @@ function sinpi(x::Real) if isinf(x) return throw(DomainError()) elseif isnan(x) - return nan(x) + return oftype(x,NaN) end rx = copysign(rem(x,2),x) @@ -131,7 +131,7 @@ function cospi(x::Real) if isinf(x) return throw(DomainError()) elseif isnan(x) - return nan(x) + return oftype(x,NaN) end rx = abs(float(rem(x,2))) @@ -236,7 +236,7 @@ function sind(x::Real) if isinf(x) return throw(DomainError()) elseif isnan(x) - return nan(x) + return oftype(x,NaN) end rx = copysign(float(rem(x,360)),x) @@ -268,7 +268,7 @@ function cosd(x::Real) if isinf(x) return throw(DomainError()) elseif isnan(x) - return nan(x) + return oftype(x,NaN) end rx = abs(float(rem(x,360))) diff --git a/base/test.jl b/base/test.jl index 70e28bb59adeb..0e1b76bd082a2 100644 --- a/base/test.jl +++ b/base/test.jl @@ -137,8 +137,8 @@ function test_approx_eq(va, vb, Eps, astr, bstr) end end -array_eps{T}(a::AbstractArray{Complex{T}}) = eps(float(maximum(x->(isfinite(x) ? abs(x) : nan(T)), a))) -array_eps(a) = eps(float(maximum(x->(isfinite(x) ? abs(x) : nan(x)), a))) +array_eps{T}(a::AbstractArray{Complex{T}}) = eps(float(maximum(x->(isfinite(x) ? abs(x) : T(NaN)), a))) +array_eps(a) = eps(float(maximum(x->(isfinite(x) ? abs(x) : oftype(x,NaN)), a))) test_approx_eq(va, vb, astr, bstr) = test_approx_eq(va, vb, 1E4*length(va)*max(array_eps(va), array_eps(vb)), astr, bstr) diff --git a/test/arrayops.jl b/test/arrayops.jl index 5b8cb62b4a822..bce81a01b1371 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -211,7 +211,7 @@ let @test x == 11 x = get(A, (4,4), -12) @test x == -12 - X = get(A, -5:5, nan(Float32)) + X = get(A, -5:5, NaN32) @test eltype(X) == Float32 @test isnan(X) == [trues(6),falses(5)] @test X[7:11] == [1:5]