From 52784f32bbb6c764eb67a1abdbbbd9f54dc6030e Mon Sep 17 00:00:00 2001 From: Angel Ezquerra Date: Fri, 10 Nov 2023 05:29:55 +0100 Subject: [PATCH] Add strformat support for Complex numbers (#22924) Before this change strformat used the generic formatValue function for Complex numbers. This meant that it was not possible to control the format of the real and imaginary components of the complex numbers. With this change this now works: ```nim import std/[complex, strformat] let c = complex(1.05000001, -2.000003) echo &"{c:g}" # You now get: (1.05, -2) # while before you'd get a ValueError exception (invalid type in format string for string, expected 's', but got g) ``` The only small drawback of this change is that I had to import complex from strformat. I hope that is not a problem. --------- Co-authored-by: Angel Ezquerra --- lib/pure/complex.nim | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/pure/complex.nim b/lib/pure/complex.nim index 5304d09305..87b0670d1e 100644 --- a/lib/pure/complex.nim +++ b/lib/pure/complex.nim @@ -36,7 +36,7 @@ runnableExamples: {.push checks: off, line_dir: off, stack_trace: off, debugger: off.} # the user does not want to trace a part of the standard library! -import std/math +import std/[math, strformat] type Complex*[T: SomeFloat] = object @@ -399,4 +399,39 @@ func `$`*(z: Complex): string = result = "(" & $z.re & ", " & $z.im & ")" +proc formatValueAsTuple(result: var string; value: Complex; specifier: string) = + ## Format implementation for `Complex` representing the value as a (real, imaginary) tuple. + result.add "(" + formatValue(result, value.re, specifier) + result.add ", " + formatValue(result, value.im, specifier) + result.add ")" + +proc formatValueAsComplexNumber(result: var string; value: Complex; specifier: string) = + ## Format implementation for `Complex` representing the value as a (RE+IMj) number + result.add "(" + formatValue(result, value.re, specifier) + if value.im >= 0 and not specifier.contains({'+', '-'}): + result.add "+" + formatValue(result, value.im, specifier) + result.add "j)" + +proc formatValue*(result: var string; value: Complex; specifier: string) = + ## Standard format implementation for `Complex`. It makes little + ## sense to call this directly, but it is required to exist + ## by the `&` macro. + ## For complex numbers, we add a specific 'j' specifier, which formats + ## the value as (A+Bj) like in mathematics. + 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) + {.pop.}