Skip to content

Commit d1d48b0

Browse files
committed
base cmp for floats on isless instead of throwing errors
part of #5290
1 parent c3a76c0 commit d1d48b0

File tree

5 files changed

+17
-29
lines changed

5 files changed

+17
-29
lines changed

base/float.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -460,22 +460,6 @@ for op in (:<, :<=, :isless)
460460
@eval ($op)(a::Float16, b::Float16) = ($op)(Float32(a), Float32(b))
461461
end
462462

463-
function cmp(x::AbstractFloat, y::AbstractFloat)
464-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
465-
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
466-
ifelse(x<y, -1, ifelse(x>y, 1, 0))
467-
end
468-
469-
function cmp(x::Real, y::AbstractFloat)
470-
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
471-
ifelse(x<y, -1, ifelse(x>y, 1, 0))
472-
end
473-
474-
function cmp(x::AbstractFloat, y::Real)
475-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
476-
ifelse(x<y, -1, ifelse(x>y, 1, 0))
477-
end
478-
479463
# Exact Float (Tf) vs Integer (Ti) comparisons
480464
# Assumes:
481465
# - typemax(Ti) == 2^n-1

base/gmp.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ cmp(x::BigInt, y::CulongMax) = MPZ.cmp_ui(x, y)
492492
cmp(x::BigInt, y::Integer) = cmp(x, big(y))
493493
cmp(x::Integer, y::BigInt) = -cmp(y, x)
494494

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

498498
isqrt(x::BigInt) = MPZ.sqrt(x)

base/mpfr.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,23 +676,23 @@ end
676676
>(x::BigFloat, y::BigFloat) = ccall((:mpfr_greater_p, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigFloat}), x, y) != 0
677677

678678
function cmp(x::BigFloat, y::BigInt)
679-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
679+
isnan(x) && return 1
680680
ccall((:mpfr_cmp_z, :libmpfr), Int32, (Ref{BigFloat}, Ref{BigInt}), x, y)
681681
end
682682
function cmp(x::BigFloat, y::ClongMax)
683-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
683+
isnan(x) && return 1
684684
ccall((:mpfr_cmp_si, :libmpfr), Int32, (Ref{BigFloat}, Clong), x, y)
685685
end
686686
function cmp(x::BigFloat, y::CulongMax)
687-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
687+
isnan(x) && return 1
688688
ccall((:mpfr_cmp_ui, :libmpfr), Int32, (Ref{BigFloat}, Culong), x, y)
689689
end
690690
cmp(x::BigFloat, y::Integer) = cmp(x,big(y))
691691
cmp(x::Integer, y::BigFloat) = -cmp(y,x)
692692

693693
function cmp(x::BigFloat, y::CdoubleMax)
694-
isnan(x) && throw(DomainError(x, "`x` cannot be NaN."))
695-
isnan(y) && throw(DomainError(y, "`y` cannot be NaN."))
694+
isnan(x) && return isnan(y) ? 0 : 1
695+
isnan(y) && return -1
696696
ccall((:mpfr_cmp_d, :libmpfr), Int32, (Ref{BigFloat}, Cdouble), x, y)
697697
end
698698
cmp(x::CdoubleMax, y::BigFloat) = -cmp(y,x)

base/rational.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,12 @@ end
285285
for rel in (:<,:<=,:cmp)
286286
for (Tx,Ty) in ((Rational,AbstractFloat), (AbstractFloat,Rational))
287287
@eval function ($rel)(x::$Tx, y::$Ty)
288-
if isnan(x) || isnan(y)
289-
$(rel == :cmp ? :(throw(DomainError((x,y), "Inputs cannot be NaN."))) :
288+
if isnan(x)
289+
$(rel == :cmp ? :(return isnan(y) ? 0 : 1) :
290+
:(return false))
291+
end
292+
if isnan(y)
293+
$(rel == :cmp ? :(return -1) :
290294
:(return false))
291295
end
292296

test/mpfr.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,11 @@ end
492492
@test isless(big(1.0), big(NaN))
493493

494494
# cmp
495-
@test cmp(big(-0.0), big(0.0)) == 0
496-
@test cmp(big(0.0), big(-0.0)) == 0
497-
@test_throws DomainError cmp(big(1.0), big(NaN))
498-
@test_throws DomainError cmp(big(NaN), big(NaN))
499-
@test_throws DomainError cmp(big(NaN), big(1.0))
495+
@test cmp(big(-0.0), big(0.0)) == -1
496+
@test cmp(big(0.0), big(-0.0)) == 1
497+
@test cmp(big(1.0), big(NaN)) == -1
498+
@test cmp(big(NaN), big(NaN)) == 0
499+
@test cmp(big(NaN), big(1.0)) == 1
500500
end
501501
@testset "signbit" begin
502502
@test signbit(BigFloat(-1.0)) == 1

0 commit comments

Comments
 (0)