Skip to content

Commit

Permalink
'j' format specifier docs (nim-lang#22928)
Browse files Browse the repository at this point in the history
This is a small improvement on top of PR nim-lang#22924, which documents the new
'j' format specifier for Complex numbers. In addition to that it moves
the handling of the j specifier into the function that actually
implements it (formatValueAsComplexNumber), which seems a little
cleaner.

---------

Co-authored-by: Angel Ezquerra <angel_ezquerra@keysight.com>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent 80ffbd4 commit fbfd4de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/pure/complex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ proc formatValueAsTuple(result: var string; value: Complex; specifier: string) =

proc formatValueAsComplexNumber(result: var string; value: Complex; specifier: string) =
## Format implementation for `Complex` representing the value as a (RE+IMj) number
## By default, the real and imaginary parts are formatted using the general ('g') format
let specifier = if specifier.contains({'e', 'E', 'f', 'F', 'g', 'G'}):
specifier.replace("j")
else:
specifier.replace('j', 'g')
result.add "("
formatValue(result, value.re, specifier)
if value.im >= 0 and not specifier.contains({'+', '-'}):
Expand All @@ -425,11 +430,6 @@ proc formatValue*(result: var string; value: Complex; specifier: string) =
if specifier.len == 0:
result.add $value
elif 'j' in specifier:
let specifier = if specifier.contains({'e', 'E', 'f', 'F', 'g', 'G'}):
specifier.replace("j")
else:
# 'The 'j' format defaults to 'g'
specifier.replace('j', 'g')
formatValueAsComplexNumber(result, value, specifier)
else:
formatValueAsTuple(result, value, specifier)
Expand Down
6 changes: 6 additions & 0 deletions lib/pure/strformat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ The available floating point presentation types are:
exponent notation.
`G` General format. Same as `g` except it switches to `E`
if the number gets to large.
`i` Complex General format. This is only supported for
complex numbers, which it prints using the mathematical
(RE+IMj) format. The real and imaginary parts are printed
using the general format `g` by default, but it is
possible to combine this format with one of the other
formats (e.g `jf`).
(None) Similar to `g`, except that it prints at least one
digit after the decimal point.
================= ====================================================
Expand Down

0 comments on commit fbfd4de

Please sign in to comment.