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

printf prints typemin(<:Signed) incorrectly #41971

Closed
StefanKarpinski opened this issue Aug 23, 2021 · 2 comments · Fixed by #42341
Closed

printf prints typemin(<:Signed) incorrectly #41971

StefanKarpinski opened this issue Aug 23, 2021 · 2 comments · Fixed by #42341
Assignees
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects. domain:strings "Strings!" kind:bug Indicates an unexpected problem or unintended behavior kind:embarrassing-bugfix Whoops!

Comments

@StefanKarpinski
Copy link
Sponsor Member

julia> using Printf

julia> @sprintf "%4d" typemin(Int8)
"-/.("

julia> @sprintf "%4d" typemin(Int16)
"--.)*("

julia> @sprintf "%4d" typemin(Int32)
"-./,),(-*,("

julia> @sprintf "%4d" typemin(Int64)
"-'..--).0-*(+,))+(0("

julia> @sprintf "%4d" typemin(Int128)
"-/)0/,//(-,*0,*'.-/)-/*()-0-)/+((,/0+).("```
@StefanKarpinski StefanKarpinski added kind:bug Indicates an unexpected problem or unintended behavior domain:strings "Strings!" domain:display and printing Aesthetics and correctness of printed representations of objects. kind:embarrassing-bugfix Whoops! labels Aug 23, 2021
@Seelengrab
Copy link
Contributor

Seelengrab commented Aug 23, 2021

x, neg = arg2 < 0 ? (-arg2, true) : (arg2, false)

doesn't work for typemins:

julia> typemin(Int8)     
-128                     
                         
julia> -typemin(Int8)    
-128                     
                         
julia> abs(typemin(Int8))
-128                     

That line in Printf.jl should only be hit by integer types and floating point is handled somewhere else, right? If so, passing x through unsigned unconditionally should work, as x is assumed positive after this check anyway.

So maybe something as simple as this:

neg = arg2 < 0
x = unsigned(arg2)

since

julia> unsigned(typemin(Int8)) == 128
true

@bramtayl
Copy link
Contributor

That's just the cool way of writing it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:display and printing Aesthetics and correctness of printed representations of objects. domain:strings "Strings!" kind:bug Indicates an unexpected problem or unintended behavior kind:embarrassing-bugfix Whoops!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants