-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make π < π work as expected #27797
Conversation
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.
base/irrationals.jl
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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")
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
Did this work at one point? #8411 (comment) |
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.
Cross-referencing the Discourse thread where this issue was raised: https://discourse.julialang.org/t/comparison-of-same-irrationals-by-throws-error/1342/ |
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.
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 inpromotion 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.