-
-
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
Fix Printf for typemin(<:Base.BitSigned) #42341
Conversation
Thanks for the PR! Overflow of signed types is well defined in julia - it results in wraparound behavior. See the docs on Overflow Behavior for details. scratched bit about unsigned - numbers |
stdlib/Printf/src/Printf.jl
Outdated
one = oneunit(arg2) | ||
neg = arg2 < 0 | ||
if typeof(arg2) <: Signed && !(typeof(arg2) <: BigInt) | ||
x = neg ? unsigned(-(arg2+one))+unsigned(one) : unsigned(arg2) |
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.
What about x = unsigned(abs(arg2))
? That fixes the problematic case and avoids any conversion problems, since the typemin
is handled by unsigned
and all other negative values are handled by abs
(since they're guaranteed to still fit into the original type).
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.
Thanks for the comment, I have updated to x = unsigned(neg ? -arg2 : arg2)
to be closer to the original code with the same meaning.
Alright, other than possibly using |
An alternative would be to use both |
Co-authored-by: Jeff Bezanson <jeff.bezanson@gmail.com>
(cherry picked from commit 82d8a36)
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.
Sorry for not reviewing this sooner; lgtm. Thanks @petvana!
PR aims to fix #41971 . I've utilized the idea from @Seelengrab to use
unsigned()
.Since overflow is not well-defined for signed types, I've addedIt seems there is no significant performance influence. Cc: @quinnj.one
not to overflow at all.