From 2343ba0c0c362f355f6841308d00228a13bcca06 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 3 Jan 2014 18:26:22 -0500 Subject: [PATCH] fix isless and cmp/lexcmp for floating point for now cmp() uses the total ordering, but we might change it to give a DomainError for NaN arguments --- base/float.jl | 33 +++++++++++++++++++++++++++++++-- base/mpfr.jl | 6 +----- base/operators.jl | 8 ++++---- base/promotion.jl | 2 -- test/mpfr.jl | 11 +++++++++-- test/numbers.jl | 7 +++++++ 6 files changed, 52 insertions(+), 15 deletions(-) diff --git a/base/float.jl b/base/float.jl index 12cf8518bf918..b8b0b8c13f70e 100644 --- a/base/float.jl +++ b/base/float.jl @@ -158,8 +158,37 @@ isequal(x::Float64, y::Float64) = fpiseq(unbox(Float64,x),unbox(Float64,y)) isless (x::Float32, y::Float32) = fpislt(unbox(Float32,x),unbox(Float32,y)) isless (x::Float64, y::Float64) = fpislt(unbox(Float64,x),unbox(Float64,y)) -isless (a::Integer, b::FloatingPoint) = (asignbit(b)))) +isless(a::Real, b::FloatingPoint) = (ay && return 1 + if isnan(x) + isnan(y) && return 0 + return 1 + end + isnan(y) && return -1 + return signbit(y) - signbit(x) +end + +function cmp(x::Real, y::FloatingPoint) + xy && return 1 + isnan(y) && return -1 + return signbit(y) - signbit(x) +end + +function cmp(x::FloatingPoint, y::Real) + xy && return 1 + isnan(x) && return 1 + return signbit(y) - signbit(x) +end ==(x::Float64, y::Int64 ) = eqfsi64(unbox(Float64,x),unbox(Int64,y)) ==(x::Float64, y::Uint64 ) = eqfui64(unbox(Float64,x),unbox(Uint64,y)) diff --git a/base/mpfr.jl b/base/mpfr.jl index c4302627803ea..5867fe2a5f652 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -8,7 +8,7 @@ export import Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely, - bessely0, bessely1, ceil, cmp, convert, copysign, degrees2radians, + bessely0, bessely1, ceil, convert, copysign, degrees2radians, exp, exp2, exponent, factorial, floor, hypot, isinteger, iround, isfinite, isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf, nextfloat, prevfloat, promote_rule, radians2degrees, rem, round, show, @@ -356,10 +356,6 @@ function -(x::BigFloat) return z end -function cmp(x::BigFloat, y::BigFloat) - ccall((:mpfr_cmp, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &x, &y) -end - function sqrt(x::BigFloat) z = BigFloat() ccall((:mpfr_sqrt, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Int32), &z, &x, ROUNDING_MODE[end]) diff --git a/base/operators.jl b/base/operators.jl index dc38a2d083426..4a66d39e62b82 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -28,10 +28,13 @@ isless(x::Real, y::Real) = x