Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make π < π work as expected #27797

Merged
merged 2 commits into from
Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ float(::Type{<:AbstractIrrational}) = Float64
==(::Irrational{s}, ::Irrational{s}) where {s} = true
==(::AbstractIrrational, ::AbstractIrrational) = false

<(::Irrational{s}, ::Irrational{s}) where {s} = false
function <(x::AbstractIrrational, y::AbstractIrrational)
# If two different Irrationals round to the same Float64, then explicit
# < methods are needed for those inputs, as the below assertation will fail.
@assert Float64(x) != Float64(y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably throw some specific error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea. Something like

Float64(x) != Float64(y) || error("$x and $y have identical Float64 representations")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or a MethodError. The solution is to add a specific method, after all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a commit, changing the code to throw a MethodError.

return Float64(x) < Float64(y)
end

<=(::Irrational{s}, ::Irrational{s}) where {s} = true
<=(x::AbstractIrrational, y::AbstractIrrational) = x==y || x<y

# Irrationals, by definition, can't have a finite representation equal them exactly
==(x::AbstractIrrational, y::Real) = false
==(x::Real, y::AbstractIrrational) = false
Expand Down
13 changes: 13 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,19 @@ end
@test !(1 > NaN)
end

@testset "Irrationals compared with Irrationals" begin
for i in (π, ℯ, γ, catalan)
for j in (π, ℯ, γ, catalan)
@test isequal(i==j, Float64(i)==Float64(j))
@test isequal(i!=j, Float64(i)!=Float64(j))
@test isequal(i<=j, Float64(i)<=Float64(j))
@test isequal(i>=j, Float64(i)>=Float64(j))
@test isequal(i<j, Float64(i)<Float64(j))
@test isequal(i>j, Float64(i)>Float64(j))
end
end
end

@testset "Irrationals compared with Rationals and Floats" begin
@test Float64(pi,RoundDown) < pi
@test Float64(pi,RoundUp) > pi
Expand Down