Skip to content

Commit

Permalink
used stagedfunctions for mathconst comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Oct 5, 2014
1 parent a7e9ff9 commit e39d4b2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
49 changes: 31 additions & 18 deletions base/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 0 additions & 2 deletions base/hashing2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e39d4b2

Please sign in to comment.