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

Conversation

perrutquist
Copy link
Contributor

Methods are added for < and <= comparing Irrationals. (The corresponding
methods for > and >= are then handled by existing fallback methods.)

Currently, expressions like ℯ < π result in promotion to Float64.
This works most of the time. However, π < π does not result in
promotion and throws an error. This commit introduces methods
for comparing an Irrational to itself.

This commit also introduces a test to make sure that incorrect results
are not being returned due to different user-defined irrationals
rounding to the same Float64.

Methods are added for < and <= comparing Irrationals. (The corresponding
> and >= are then handled by existing fallback methods.)

Currently, expressions like `ℯ < π` result in promotion to Float64.
This works most of the time. However, `π < π` does not result in
promotion and throws an error. This commit introduces a method
for comparing an Irrational to itself.

This commit also introduces a test to make sure that incorrect results
are not being returned due to different user-defined irrationals
rounding to the same Float64.
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.

@mbauman
Copy link
Sponsor Member

mbauman commented Jun 26, 2018

Did this work at one point? #8411 (comment)

@simonbyrne
Copy link
Contributor

I thought it did, but I just tried various versions back to 0.4 and apparently not!

In the (probably rare) case where a user defines two Irrationals that do
not differ in their Float64 representation, comparisons will now
throw a MethodError to inform the user that he needs to define the <
method himself.
@JeffBezanson JeffBezanson merged commit 3a27c52 into JuliaLang:master Jun 27, 2018
@waldyrious
Copy link
Contributor

Cross-referencing the Discourse thread where this issue was raised: https://discourse.julialang.org/t/comparison-of-same-irrationals-by-throws-error/1342/

jrevels pushed a commit that referenced this pull request Jul 2, 2018
Methods are added for < and <= comparing Irrationals. (The corresponding
> and >= are then handled by existing fallback methods.)

Currently, expressions like `ℯ < π` result in promotion to Float64.
This works most of the time. However, `π < π` does not result in
promotion and throws an error. This commit introduces a method
for comparing an Irrational to itself.

This commit also introduces a test to make sure that incorrect results
are not being returned due to different user-defined irrationals
rounding to the same Float64.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants