Fix Printf for typemin(<:Base.BitSigned)#42341
Fix Printf for typemin(<:Base.BitSigned)#42341JeffBezanson merged 8 commits intoJuliaLang:masterfrom
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.
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.
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)
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.onenot to overflow at all.