python / cpython

The Python programming language
https://www.python.org
Other
62.43k stars 29.97k forks source link

Make description of presentation types `f` and `e` in format spec more clear #123133

Open Prometheus3375 opened 1 month ago

Prometheus3375 commented 1 month ago

Some time ago I was reading docs of format specification mini-language and got a bit confused with the description for presentation type f. Because of my misunderstanding, I created #111125 thinking there is an error in docs. There I got an explanation what was actually meant.

Here is the current description of type f:

'f' -- Fixed-point notation. For a given precision p, formats the number as a decimal number with exactly p digits following the decimal point. With no precision given, uses a precision of 6 digits after the decimal point for float, and uses a precision large enough to show all coefficient digits for Decimal. If no digits follow the decimal point, the decimal point is also removed unless the # option is used.

The source of my confusion had come from the last sentence. I assumed that it refers to the case with no precision given mentioned in the previous sentence, but it actually refers to the case when p is zero. For comparison, the description of type g at first lists the case when no digits follow the decimal point, and then in a different paragraph lists the case with no precision given (I do not quote the description here because it is too large).

I suggest to rearrange two last sentences in descriptions of types f and e and maybe rephrase some parts. Here are two examples for type f.

Just rearrangement:

'f' -- Fixed-point notation. For a given precision p, formats the number as a decimal number with exactly p digits following the decimal point. If no digits follow the decimal point, the decimal point is also removed unless the # option is used. With no precision given, uses a precision of 6 digits after the decimal point for float, and uses a precision large enough to show all coefficient digits for Decimal.

Rearranged sentences with edited 2nd sentence.

'f' -- Fixed-point notation. For a given precision p, formats the number as a decimal number with exactly p digits following the decimal point. With a precision of 0, the decimal point is removed unless the # option is used. With no precision given, uses a precision of 6 digits after the decimal point for float, and uses a precision large enough to show all coefficient digits for Decimal.

skirpichev commented 1 month ago

The source of my confusion had come from the last sentence. I assumed that it refers to the case with no precision given mentioned in the previous sentence, but it actually refers to the case when p is zero.

I am not a native speaker of English, but for me it's pretty obvious from first two sentences, that "no digits follow the decimal point" case might correspond only to precision=0. "No precision given" means precision=6. Text seems very clear. Where is the source of confusion?

Prometheus3375 commented 1 month ago

Where is the source of confusion?

I though that case "If no digits follow the decimal point" is a subcase for "With no precision given". For example, 1 or 1. have no digits after the decimal point, so I thought f'{1:f}' would result in '1' and f'{1:#f}' in '1.'.

Prometheus3375 commented 1 month ago

The current description has the following structure: given precision -> no given precision -> specific given description. It would be more logical to not separate cases with given precision. I propose such structure: given precision -> specific given precision -> no given precision. Such structure is already present in the description of g type.

skirpichev commented 1 month ago

I though that case "If no digits follow the decimal point" is a subcase for "With no precision given".

You just ignored the first sentence. One told you that number of digits after the point is equal to precision.

The current description has the following structure: given precision -> no given precision -> specific given description.

Rather: full description of the precision setting (with defaults, 2nd sentence). Then specific behaviour for precision=0. I don't see what we gain, if discussion of defaults for precision will be the last sentence.