julia icon indicating copy to clipboard operation
julia copied to clipboard

Printf for complex numbers

Open aplavin opened this issue 6 months ago • 4 comments

Currently, @sprintf "%.2f" (1+2im) errors, but it would make sense for it to produce "1.00 + 2.00im" I think. Currently, printfing a complex number is not really convenient, requiring a manual split into real + complex, and keeping the format string in sync between them.

aplavin avatar Jun 03 '25 12:06 aplavin

https://github.com/JuliaLang/julia/issues/346 for previous discussion

gbaraldi avatar Jun 03 '25 15:06 gbaraldi

Maybe after 13 years the perception changed... :)

aplavin avatar Jun 03 '25 15:06 aplavin

I had a bunch of objections written up but gbaraldi's link (a triple-digit issue -- wow!) already addresses all of them. In particular, once we do Complex, should we also support formatted printing Tuple, Array, Rational, etc? I don't see where it ends.

printf just doesn't feel like the tool for the job. It already has an interface that is largely shared across many (most?) languages and we aren't doing anyone any favors by punning it for every type we can imagine.

I think a more durable solution would be to add a Printf.Format argument to show/print methods that can be applied to individual entries.

mikmoore avatar Jun 03 '25 15:06 mikmoore

In particular, once we do Complex, should we also support formatted printing Tuple, Array, Rational, etc? I don't see where it ends.

I personally have all numbers in mind, and fail to see the connection to containers like tuple or arrays. They are quite a different beast.

aplavin avatar Jun 03 '25 15:06 aplavin