'j' format specifier docs (#22928)

This is a small improvement on top of PR #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>
This commit is contained in:
Angel Ezquerra
2023-11-17 10:22:57 +01:00
committed by GitHub
parent 80ffbd4571
commit fbfd4decca
2 changed files with 11 additions and 5 deletions

View File

@@ -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({'+', '-'}):
@@ -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)

View File

@@ -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.
================= ====================================================