bpo-45995: add "z" format specifer to coerce negative 0 to zero (GH-30049)

Add "z" format specifier to coerce negative 0 to zero.

See https://github.com/python/cpython/issues/90153 (originally https://bugs.python.org/issue45995) for discussion.
This covers `str.format()` and f-strings.  Old-style string interpolation is not supported.

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
This commit is contained in:
John Belmonte 2022-04-11 23:34:18 +09:00 committed by GitHub
parent dd207a6ac5
commit b0b836b20c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 368 additions and 43 deletions

View file

@ -14372,7 +14372,7 @@ formatfloat(PyObject *v, struct unicode_format_arg_t *arg,
double x;
Py_ssize_t len;
int prec;
int dtoa_flags;
int dtoa_flags = 0;
x = PyFloat_AsDouble(v);
if (x == -1.0 && PyErr_Occurred())
@ -14383,9 +14383,9 @@ formatfloat(PyObject *v, struct unicode_format_arg_t *arg,
prec = 6;
if (arg->flags & F_ALT)
dtoa_flags = Py_DTSF_ALT;
else
dtoa_flags = 0;
dtoa_flags |= Py_DTSF_ALT;
if (arg->flags & F_NO_NEG_0)
dtoa_flags |= Py_DTSF_NO_NEG_0;
p = PyOS_double_to_string(x, arg->ch, prec, dtoa_flags, NULL);
if (p == NULL)
return -1;