diff --git a/base/constants.jl b/base/constants.jl index 34727f23e60a8f..d3e3ddc7e68eae 100644 --- a/base/constants.jl +++ b/base/constants.jl @@ -21,6 +21,37 @@ end =={s}(::MathConst{s}, ::MathConst{s}) = true ==(::MathConst, ::MathConst) = false +# MathConsts are irrational, so unequal to everything else +==(x::MathConst, y::Real) = false +==(x::Real, y::MathConst) = false + +# MathConst vs FloatingPoint +<{T<:FloatingPoint}(x::MathConst, y::T) = T(x,RoundUp) <= y +<{T<:FloatingPoint}(x::T, y::MathConst) = x <= T(y,RoundDown) + +<=(x::MathConst,y::FloatingPoint) = x < y +<=(x::FloatingPoint,y::MathConst) = x < y + +# MathConst vs Rational +stagedfunction <{T}(x::MathConst, y::Rational{T}) + bx = big(x()) + bx < 0 && T <: Unsigned && return true + rx = rationalize(T,bx,tol=0) + rx < bx ? :($rx < y) : :($rx <= y) +end +stagedfunction <{T}(x::Rational{T}, y::MathConst) + by = big(y()) + by < 0 && T <: Unsigned && return false + ry = rationalize(T,by,tol=0) + ry < by ? :(x <= $ry) : :(x < $ry) +end +<(x::MathConst, y::Rational{BigInt}) = big(x) < y +<(x::Rational{BigInt}, y::MathConst) = x < big(y) + +<=(x::MathConst,y::Rational) = x < y +<=(x::Rational,y::MathConst) = x < y + + hash(x::MathConst, h::UInt) = hash(object_id(x), h) -(x::MathConst) = -float64(x) diff --git a/test/numbers.jl b/test/numbers.jl index f2210642896ecf..b876c2622b1968 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -814,6 +814,19 @@ end @test !(1//3 == NaN) @test !(1//3 > NaN) +@test Float64(pi,RoundDown) < pi +@test Float64(pi,RoundUp) > pi +@test !(Float64(pi,RoundDown) > pi) +@test !(Float64(pi,RoundUp) < pi) +@test Float64(pi,RoundDown) <= pi +@test Float64(pi,RoundUp) >= pi +@test Float64(pi,RoundDown) != pi +@test Float64(pi,RoundUp) != pi + +@test 2646693125139304345//842468587426513207 < pi +@test !(2646693125139304345//842468587426513207 > pi) +@test 2646693125139304345//842468587426513207 != pi + @test sqrt(2) == 1.4142135623730951 @test 1+1.5 == 2.5