Skip to content

Invalid description for '' (None) formatting type for floats #123560

@skirpichev

Description

@skirpichev

Docs says, that if format type is not specified:
"For float this is the same as 'g', except that when fixed-point notation is used to format the result, it always includes at least one digit past the decimal point. The precision used is as large as needed to represent the given value faithfully.

[...]

The overall effect is to match the output of str() as altered by the other format modifiers."

On another hand, for 'g' presentation type it says:
"General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude. A precision of 0 is treated as equivalent to a precision of 1.

The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then, if -4 <= exp < p [...], the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it, unless the '#' option is used.

With no precision given, uses a precision of 6 significant digits for float."

Lets consider case, when precision is specified. Obviously, it's not "same as 'g':

Python 3.14.0a0 (heads/main:58ce131037, Aug 29 2024, 16:19:25) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> format(1.0, '.1')
'1e+00'
>>> format(1.0, '.1g')
'1'
>>> format(12.34, '.2')
'1.2e+01'
>>> format(12.34, '.2g')
'12'

Here exponential format is used instead of fixed-point, as for 'g' type. IIUC, this coming from #50114. It seems, that 'g' type comparison should be changed here (for '' presentation type) to -4 <= exp < p-1 instead. I'll work on a patch.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions