Skip to content
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

Merged
merged 8 commits into from
Sep 27, 2021

Conversation

petvana
Copy link
Member

@petvana petvana commented Sep 22, 2021

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 added one not to overflow at all. It seems there is no significant performance influence. Cc: @quinnj.

@petvana petvana changed the title Fix printf Fix Printf for typemin(<:Signed) Sep 22, 2021
@Seelengrab
Copy link
Contributor

Seelengrab commented Sep 22, 2021

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 != typemin(T) need adjustment of course..

one = oneunit(arg2)
neg = arg2 < 0
if typeof(arg2) <: Signed && !(typeof(arg2) <: BigInt)
x = neg ? unsigned(-(arg2+one))+unsigned(one) : unsigned(arg2)
Copy link
Contributor

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).

Copy link
Member Author

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.

@petvana petvana marked this pull request as ready for review September 22, 2021 12:15
@petvana petvana changed the title Fix Printf for typemin(<:Signed) Fix Printf for typemin(<:Base.BitSigned) Sep 22, 2021
@Seelengrab
Copy link
Contributor

Alright, other than possibly using abs instead of the ternary, LGTM.

@petvana
Copy link
Member Author

petvana commented Sep 22, 2021

Alright, other than possibly using abs instead of the ternary, LGTM.

An alternative would be to use both abs and ternary in one-liner code. ;-) I can update if preferred.
x = typeof(arg2) <: Base.BitSigned ? unsigned(abs(arg2)) : abs(arg2)

@JeffBezanson JeffBezanson added backport 1.7 bugfix This change fixes an existing bug display and printing Aesthetics and correctness of printed representations of objects. labels Sep 23, 2021
petvana and others added 2 commits September 23, 2021 20:50
Co-authored-by: Jeff Bezanson <jeff.bezanson@gmail.com>
@JeffBezanson JeffBezanson merged commit 82d8a36 into JuliaLang:master Sep 27, 2021
KristofferC pushed a commit that referenced this pull request Sep 29, 2021
Copy link
Member

@quinnj quinnj left a 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!

LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This change fixes an existing bug display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

printf prints typemin(<:Signed) incorrectly
5 participants