Skip to content

Commit

Permalink
base cmp for floats on isless instead of throwing errors
Browse files Browse the repository at this point in the history
part of #5290
  • Loading branch information
JeffBezanson committed Jan 4, 2018
1 parent 45b6766 commit f0879ad
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
16 changes: 0 additions & 16 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -460,22 +460,6 @@ for op in (:<, :<=, :isless)
@eval ($op)(a::Float16, b::Float16) = ($op)(Float32(a), Float32(b))
end

function cmp(x::AbstractFloat, y::AbstractFloat)
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
ifelse(x<y, -1, ifelse(x>y, 1, 0))
end

function cmp(x::Real, y::AbstractFloat)
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
ifelse(x<y, -1, ifelse(x>y, 1, 0))
end

function cmp(x::AbstractFloat, y::Real)
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
ifelse(x<y, -1, ifelse(x>y, 1, 0))
end

# Exact Float (Tf) vs Integer (Ti) comparisons
# Assumes:
# - typemax(Ti) == 2^n-1
Expand Down
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ cmp(x::BigInt, y::CulongMax) = MPZ.cmp_ui(x, y)
cmp(x::BigInt, y::Integer) = cmp(x, big(y))
cmp(x::Integer, y::BigInt) = -cmp(y, x)

cmp(x::BigInt, y::CdoubleMax) = isnan(y) ? throw(DomainError(y, "`y` cannot be NaN.")) : MPZ.cmp_d(x, y)
cmp(x::BigInt, y::CdoubleMax) = isnan(y) ? -1 : MPZ.cmp_d(x, y)
cmp(x::CdoubleMax, y::BigInt) = -cmp(y, x)

isqrt(x::BigInt) = MPZ.sqrt(x)
Expand Down
10 changes: 5 additions & 5 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -676,23 +676,23 @@ end
>(x::BigFloat, y::BigFloat) = ccall((:mpfr_greater_p, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}), x, y) != 0

function cmp(x::BigFloat, y::BigInt)
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
isnan(x) && return 1
ccall((:mpfr_cmp_z, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigInt}), x, y)
end
function cmp(x::BigFloat, y::ClongMax)
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
isnan(x) && return 1
ccall((:mpfr_cmp_si, :libmpfr), Int32, (Ref{BigFloat}, Clong), x, y)
end
function cmp(x::BigFloat, y::CulongMax)
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
isnan(x) && return 1
ccall((:mpfr_cmp_ui, :libmpfr), Int32, (Ref{BigFloat}, Culong), x, y)
end
cmp(x::BigFloat, y::Integer) = cmp(x,big(y))
cmp(x::Integer, y::BigFloat) = -cmp(y,x)

function cmp(x::BigFloat, y::CdoubleMax)
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
isnan(x) && return isnan(y) ? 0 : 1
isnan(y) && return -1
ccall((:mpfr_cmp_d, :libmpfr), Int32, (Ref{BigFloat}, Cdouble), x, y)
end
cmp(x::CdoubleMax, y::BigFloat) = -cmp(y,x)
Expand Down
8 changes: 6 additions & 2 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,12 @@ end
for rel in (:<,:<=,:cmp)
for (Tx,Ty) in ((Rational,AbstractFloat), (AbstractFloat,Rational))
@eval function ($rel)(x::$Tx, y::$Ty)
if isnan(x) || isnan(y)
$(rel == :cmp ? :(throw(DomainError((x,y), "Inputs cannot be NaN."))) :
if isnan(x)
$(rel == :cmp ? :(return isnan(y) ? 0 : 1) :
:(return false))
end
if isnan(y)
$(rel == :cmp ? :(return -1) :
:(return false))
end

Expand Down
6 changes: 3 additions & 3 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ end
# cmp
@test cmp(big(-0.0), big(0.0)) == 0
@test cmp(big(0.0), big(-0.0)) == 0
@test_throws DomainError cmp(big(1.0), big(NaN))
@test_throws DomainError cmp(big(NaN), big(NaN))
@test_throws DomainError cmp(big(NaN), big(1.0))
@test cmp(big(1.0), big(NaN)) == -1
@test cmp(big(NaN), big(NaN)) == 0
@test cmp(big(NaN), big(1.0)) == 1
end
@testset "signbit" begin
@test signbit(BigFloat(-1.0)) == 1
Expand Down

0 comments on commit f0879ad

Please sign in to comment.