-
-
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
x < min(x, y)
#53836
Comments
I would consider this a "correctness bug"
and
I understand that the types are promoted to enable comparison, but that shouldn't mean the return type changes granted, that would harm the type-stability of |
Related to #52355. We currently provide that an It's hard to say that we should (or even realistically could) do something better for I guess this particular inspiring example |
for whatever it's worth, my opinion here and in #52355 is that they should definitely both throw documentation is probably a second-best solution. I am not so optimistic about the other suggestion with |
I would accept an However, it would be very breaking to have I don't see a way to fix |
for exactly the reasons you stated, I would have expected that |
It is quirky (and undocumented) that min and max promote for This dates way back to #212, when unions were indeed "nasty". Seems far less necessary these days. |
This highlights the core problem: the mixed-type comparison is itself ill-formed without explicit confirmation form the programmer. Promotion implicitly confirms that rounding error is acceptable; unless unless one wants to throw an error for all |
surely it is at least "reasonable" (though up for debate whether "optimal") to do runtime checks if underflow has occurred and error if so, otherwise allow the comparison I don't understand why this has only the maybe there is no path in sight to fixing it, but surely this is a bug! |
(Sidenote: this behavior also occurs in I don't see this as a bug. I see this as a simple consequence of Julia's ubiquitous promotion behavior. We still return the value of the minimum argument, just not necessarily with the original type. That conversion to floats can return an unequal value is a decision that was made long ago. But the existing implementation still satisfies arithmetic properties like I think that even ignoring the status-quo motivator, there are compelling arguments for promoting min(0, 0e0)
min(0, -0e0)
min(Inf32, Inf64)
min(NaN32, NaN64) # note: it's already unspecified which of two NaNs are returned even when the same type All of these examples except These are not entirely meaningless distinctions. Operations like If some specific situation requires a |
I see three somewhat-feasible options, from least- to most-breaking:
The round-to-nearest behaviors of But this can also happen in other situations where it may not be feel as obviously buggy as the julia> 1//10 < min(1//10, Inf)
true
julia> pi > max(pi, 0.0)
true At the same time, we're quite lucky that |
This seems to be the most robust choice. One might include in said documentation that for all things related to order, promotions are performed so as to return a type-stable result, at risk of loss of precision. If the programmer needs to perform heterogeneous order comparisons between floating point numbers, rational numbers and integers, then it should be their responsibility, as it is almost certain that the correct action (as @milkmore's and @mbauman's examples demonstrate) will depend on context. In other words, homogeneous order comparison will have the behavior inherent to numbers outside a computer. For heterogeneous order comparisons, promotion is the default and if a programmer needs otherwise, they need to deal with it. This makes it straightforward to reason about ordering when dealing with two items of type edit: credit to Matt's examples as well. |
The minimum of
x
and anything should not be greater thanx
.I found this issue when I got a trunc error on
floor(T, min(typemax(T), x))
withT = UInt
andx
a float grater thantypemax(UInt)
.This is due to promotion changing the value of
x
to something not equal tox
, and afaict it's not feasibe to fix this in the implementation, though it should be documented.If someone has an idea for how to the behavior, though, that would be better.
The text was updated successfully, but these errors were encountered: