-
-
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
isapprox
can give absurd result in case of integer overflow
#50380
Comments
yes |
OK. I'm not sure how to handle overflow though. Using function isapprox(x::Integer, y::Integer;
atol::Real=0, rtol::Real=rtoldefault(x,y,atol),
nans::Bool=false, norm::Function=abs)
if norm === abs && atol < 1 && rtol == 0
return x == y
else
return norm(widen(x) - widen(y)) <= max(atol, rtol*max(norm(widen(x)), norm(widen(y))))
end
end Unfortunately not all integer types define This method will also have to be used/adapted for cases mixing integers and floats. That case is more complex as |
can't you just check use a subtract with overflow checking? |
Well that's no enough. First, The second issue is what to do if overflow happens. In general that means that |
I guess part of the confusion here is that (except for exactly typemin as shown in the first answer) most of the answers actually are close, in the mod Int8 arithmetic base that is being expressed here.
|
Yes, but do we really want |
Here's another overflow: julia> isapprox(UInt8(60), UInt8(61), atol=1)
false
julia> isapprox(UInt8(60), UInt8(59), atol=1)
true Technically, the overflow behavior described in this thread is correct because the docstring defines the semantics in terms of the implementation:
But I still think some of this behavior is bad enough to warrant changing (and changing the documentation to reflect that) |
Ensure that `isapprox` gives correct results when comparing an integer with another integer or with a float. For comparison between integers, the fix only works when keeping default values for `rtol` and `norm`, and with `atol < 1`. It is not possible to handle the (atypical) case where `norm !== abs`, but that's OK since the user is responsible for providing a safe function. It would be possible to handle the case where `rtol > 0` or `atol >= 1`, but with complex code which would check for overflow and handle all possible corner cases; it would work only for types defined in Base and would not be extensible by packages. So I'm not sure that's worth it. At least with PR fixes the most common case. Fixes #50380.
#50730 does fix the case described in the last comment as it only covers |
Ensure that `isapprox` gives correct results when comparing an integer with another integer or with a float. For comparison between integers, the fix only works when keeping default values for `rtol` and `norm`, and with `atol < 1`. It is not possible to handle the (atypical) case where `norm !== abs`, but that's OK since the user is responsible for providing a safe function. It would be possible to handle the case where `rtol > 0` or `atol >= 1`, but with complex code which would check for overflow and handle all possible corner cases; it would work only for types defined in Base and would not be extensible by packages. So I'm not sure that's worth it. At least with PR fixes the most common case. Fixes #50380. (cherry picked from commit 5f03a18)
Ensure that `isapprox` gives correct results when comparing an integer with another integer or with a float. For comparison between integers, the fix only works when keeping default values for `rtol` and `norm`, and with `atol < 1`. It is not possible to handle the (atypical) case where `norm !== abs`, but that's OK since the user is responsible for providing a safe function. It would be possible to handle the case where `rtol > 0` or `atol >= 1`, but with complex code which would check for overflow and handle all possible corner cases; it would work only for types defined in Base and would not be extensible by packages. So I'm not sure that's worth it. At least with PR fixes the most common case. Fixes #50380. (cherry picked from commit 5f03a18)
Ensure that `isapprox` gives correct results when comparing an integer with another integer or with a float. For comparison between integers, the fix only works when keeping default values for `rtol` and `norm`, and with `atol < 1`. It is not possible to handle the (atypical) case where `norm !== abs`, but that's OK since the user is responsible for providing a safe function. It would be possible to handle the case where `rtol > 0` or `atol >= 1`, but with complex code which would check for overflow and handle all possible corner cases; it would work only for types defined in Base and would not be extensible by packages. So I'm not sure that's worth it. At least with PR fixes the most common case. Fixes #50380. (cherry picked from commit 5f03a18)
Ensure that `isapprox` gives correct results when comparing an integer with another integer or with a float. For comparison between integers, the fix only works when keeping default values for `rtol` and `norm`, and with `atol < 1`. It is not possible to handle the (atypical) case where `norm !== abs`, but that's OK since the user is responsible for providing a safe function. It would be possible to handle the case where `rtol > 0` or `atol >= 1`, but with complex code which would check for overflow and handle all possible corner cases; it would work only for types defined in Base and would not be extensible by packages. So I'm not sure that's worth it. At least with PR fixes the most common case. Fixes #50380. (cherry picked from commit 5f03a18)
I was surprised to realize this:
This is because of the way the
isapprox
fallback is defined, and more precisely to:Shouldn't we define a custom method for integers that is robust to overflow?
The text was updated successfully, but these errors were encountered: