Skip to content

gh-113804: Add 'x' format to float.format()#119945

Closed
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:float_hex
Closed

gh-113804: Add 'x' format to float.format()#119945
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:float_hex

Conversation

@vstinner

@vstinner vstinner commented Jun 2, 2024

Copy link
Copy Markdown
Member

@vstinner

vstinner commented Jun 3, 2024

Copy link
Copy Markdown
Member Author

Not supported (raise ValueError):

  • Precision
  • '=' alignment

@vstinner

vstinner commented Jun 3, 2024

Copy link
Copy Markdown
Member Author

Comment thread Doc/library/string.rst
Comment on lines +574 to +576
| ``'x'`` | For :class:`float`, hex format. Outputs the number in |
| | base 16, using lower-case letters for the digits above 9.|
| | The alternate form adds ``'0x'`` prefix. |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| ``'x'`` | For :class:`float`, hex format. Outputs the number in |
| | base 16, using lower-case letters for the digits above 9.|
| | The alternate form adds ``'0x'`` prefix. |
| ``'x'`` | For :class:`float`, hex format, supported by |
| | :meth:`~float.fromhex`. Represent the number by a |
| | hexadecimal string in the form ``[±][0x]h[.hhh]p±d``, |
| | using using lower-case letters for the digits above 9. |
| | The exponent ``d`` is written in decimal, it always |
| | contains at least one digit, and it gives the power of 2 |
| | by which to multiply the coefficient. |
| | |
| | The alternate form adds ``'0x'`` prefix. |
  1. maybe the first sentence + remark about lower-case is enough
  2. maybe we should document also 1-normalisation & case of subnormals.

Comment thread Doc/library/string.rst Outdated
| | base 16, using lower-case letters for the digits above 9.|
| | The alternate form adds ``'0x'`` prefix. |
| | |
| | Precision and ``'='`` alignment are not supported. |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what's wrong with =? This seems to be supported on my branch:

>>> f"{-1.1:30x}"
'           -1.199999999999ap+0'
>>> f"{-1.1:=30x}"
'-           1.199999999999ap+0'
>>> f"{-1.111111:=30f}"
'-                     1.111111'
>>> f"{-1.111111:30f}"
'                     -1.111111'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just too lazy to implement it. If it matters, I can change my implementation to support the "=" alignment as well.

@serhiy-storchaka

Copy link
Copy Markdown
Member

Why not simply use float.hex()?

@vstinner

vstinner commented Jun 3, 2024

Copy link
Copy Markdown
Member Author

Why not simply use float.hex()?

Do you mean: instead of adding this format? This format gives more formatting options: sign, 0x prefix, alignment.

@serhiy-storchaka

Copy link
Copy Markdown
Member

It is not so difficult to process the result of float.hex(). And I think that this is a pretty rare case, it is not worth the cost of added code.

@vstinner

vstinner commented Jun 7, 2024

Copy link
Copy Markdown
Member Author

@mdickinson @tim-one: What do you think of this approach? Do you agree with @serhiy-storchaka that the existing float.hex() method is enough?

@mdickinson

Copy link
Copy Markdown
Member

@vstinner For me having x support for float formatting would be no more than a minor convenience - I'd use it primarily for the padding, alignment and sign-handling options. But yes, I could do all that with custom functionality around float.hex. So call me +0 on this.

I do think = should be supported, if we implement this.

@vstinner

Copy link
Copy Markdown
Member Author

@mdickinson:

But yes, I could do all that with custom functionality around float.hex. So call me +0 on this. I do think = should be supported, if we implement this.

Ok, I rewrote my PR to implement the "=" alignment (and simplify the implementation).

@vstinner

vstinner commented Jul 2, 2024

Copy link
Copy Markdown
Member Author

It was decided to not pursue this approach, since float.hex() exists and it's enough for most usages, and we don't want to make str.format() more complicated.

@vstinner vstinner closed this Jul 2, 2024
@vstinner vstinner deleted the float_hex branch July 2, 2024 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants