GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing peps.python.org — PEP 682 – Format Specifier for Signed Zero | peps.python.org
PEP 682 – Format Specifier for Signed Zero
PEP 682 – Format Specifier for Signed Zero
AbstractThough MotivationHere is negative zero: >>> x = -0. >>> x -0.0 When formatting a number, negative zero can result from rounding. Assuming the user’s intention is truly to discard precision, the distinction between negative and positive zero of the rounded result might be considered an unwanted artifact: >>> for x in (.002, -.001, .060): ... print(f'{x: .1f}') 0.0 -0.0 0.1 There are various approaches to clearing the sign of a negative zero. It can be achieved without a conditional by adding positive zero: >>> x = -0. >>> x + 0. 0.0 To normalize negative zero when formatting, it is necessary to perform a redundant (and error-prone) pre-rounding of the input: >>> for x in (.002, -.001, .060): ... print(f'{round(x, 1) + 0.: .1f}') 0.0 0.0 0.1 There is ample evidence that, regardless of the language, programmers are often looking for a way to suppress negative zero, and landing on a variety of workarounds (pre-round, post-regex, etc.). A sampling:
What we would like instead is a first-class option to normalize negative zero, on top of everything else that numerical string formatting already offers. RationaleThere are use cases where negative zero is unwanted in formatted number output – arguably, not wanting it is more common. Expanding the format specification is the best way to support this because number formatting already incorporates rounding, and the normalization of negative zero must happen after rounding. While it is possible to pre-round and normalize a number before formatting, it’s tedious and prone to error if the rounding doesn’t precisely match that of the format spec. Furthermore, functions that wrap formatting would find themselves having to parse format specs to extract the precision information. For example, consider how this utility for formatting one-dimensional numerical arrays would be complicated by such pre-rounding: def format_vector(v, format_spec='8.2f'): """Format a vector (any iterable) using given per-term format string.""" return f"[{','.join(f'{term:{format_spec}}' for term in v)}]" To date, there doesn’t appear to be any other widely-used language or library
providing a formatting option for negative zero. However, the same When Rust developers debated whether to suppress negative zero in SpecificationAn optional, literal [[fill]align][sign][z][#][0][width][grouping_option][.precision][type] where When Synopsis: >>> x = -.00001 >>> f'{x:z.1f}' '0.0' >>> x = decimal.Decimal('-.00001') >>> '{:+z.1f}'.format(x) '+0.0' Design NotesThe solution must be opt-in, because we can’t change the behavior of programs that may be expecting or relying on negative zero when formatting numbers. The proposed extension is intentionally While f-strings, built-in C99 Backwards CompatibilityThe new formatting behavior is opt-in, so numerical formatting of existing programs will not be affected. How to Teach ThisA typical introductory Python course will not cover string formatting
in full detail. For such a course, no adjustments would need to be made.
For a course that does go into details of the string format specification,
a single example demonstrating the effect of the Reference ImplementationA reference implementation exists at pull request #30049. CopyrightThis document is placed in the public domain or under the CC0-1.0-Universal license, whichever is more permissive. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |