From e39d4b2be45a6f1887e135ba0e6758077e3f96c1 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Sun, 5 Oct 2014 16:11:45 +0100 Subject: [PATCH] used stagedfunctions for mathconst comparisons --- base/constants.jl | 49 ++++++++++++++++++++++++++++++----------------- base/hashing2.jl | 2 -- test/numbers.jl | 5 +++++ 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/base/constants.jl b/base/constants.jl index bf550d924ce7e..4d167e97010fb 100644 --- a/base/constants.jl +++ b/base/constants.jl @@ -15,43 +15,58 @@ convert{T<:Integer}(::Type{Rational{T}}, x::MathConst) = convert(Rational{T}, fl =={s}(::MathConst{s}, ::MathConst{s}) = true ==(::MathConst, ::MathConst) = false +==(::Real, ::MathConst) = false +==(::MathConst, ::Real) = false <(x::MathConst, y::MathConst) = float(x) < float(y) <=(x::MathConst, y::MathConst) = float(x) <= float(y) -<(x::Real, y::MathConst) = x <= prevfloat(y) -<(x::MathConst, y::Real) = nextfloat(x) <= y +<{T<:FloatingPoint}(x::T, y::MathConst) = x <= prevfloat(T,y) +<{T<:FloatingPoint}(x::MathConst, y::T) = nextfloat(T,x) <= y + +<(x::Integer, y::MathConst) = x < float(y) +<(x::MathConst, y::Integer) = float(x) < y + function <(x::BigFloat, c::MathConst) p = precision(x) - y = with_bigfloat_precision(p) do + y = with_bigfloat_precision(p+32) do big(c) end - while x == y - p += 16 - y = with_bigfloat_precision(p) do - big(c) - end - end x < y end function <(c::MathConst, x::BigFloat) p = precision(x) - y = with_bigfloat_precision(p) do + y = with_bigfloat_precision(p+32) do big(c) end - while x == y - p += 16 - y = with_bigfloat_precision(p) do - big(c) - end - end y < x end <=(x::Real, y::MathConst) = x < y <=(x::MathConst, y::Real) = x < y +# temporary fix until #8505 +untype{T}(::Type{Type{T}}) = T + +stagedfunction prevfloat{T<:FloatingPoint,s}(t::Type{T},c::MathConst{s}) + bc = big(c()) + f = with_rounding(BigFloat,RoundDown) do + convert(untype(t),bc) + end + :($f) +end +stagedfunction nextfloat{T<:FloatingPoint,s}(t::Type{T},c::MathConst{s}) + bc = big(c()) + f = with_rounding(BigFloat,RoundUp) do + convert(untype(t),bc) + end + :($f) +end +nextfloat(c::MathConst) = nextfloat(Float64,c) +prevfloat(c::MathConst) = prevfloat(Float64,c) + + # TODO: Rationals hash(x::MathConst, h::Uint) = hash(object_id(x), h) @@ -80,8 +95,6 @@ macro math_const(sym, val, def) $bigconvert Base.convert(::Type{Float64}, ::MathConst{$qsym}) = $val # TODO: fix this - Base.prevfloat(::MathConst{$qsym}) = $val - Base.nextfloat(::MathConst{$qsym}) = $val Base.convert(::Type{Float32}, ::MathConst{$qsym}) = $(float32(val)) @assert isa(big($esym), BigFloat) @assert float64($esym) == float64(big($esym)) diff --git a/base/hashing2.jl b/base/hashing2.jl index 45d406f313af2..f3264f6d1a300 100644 --- a/base/hashing2.jl +++ b/base/hashing2.jl @@ -275,8 +275,6 @@ function cmp(x::Real, y::Real) return c end end - - function ==(x::Real, y::Real) (isnan(x) || isnan(y)) && return false diff --git a/test/numbers.jl b/test/numbers.jl index a38a14cf042d1..8a6a671ea0f1b 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -799,6 +799,11 @@ end @test !(1//3 == NaN) @test !(1//3 > NaN) +@test float(pi) < pi +@test !(float(pi) > pi) +@test float(pi) <= pi +@test !(float(pi) >= pi) +@test float(pi) != pi @test sqrt(2) == 1.4142135623730951