You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unsupported format string passed to mpf.__format__ #915
$ python
Python 3.12.3 (main, Feb 4 2025, 14:48:35) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mpmath
>>> mpmath.__version__
'1.3.0'
>>> val = mpmath.mpf('0.1')
>>> f"{val:.016f}"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to mpf.__format__
>>>
In the realm of arbitrary precision numbers, I think there is no such thing as meaningless leading or trailing zero.
A leading or trailing zero is supposed to be a correctly computed digit.
For example, 1.000000000000000 is different from 1.000000000000000123463563456.
Presentation is important for readability and clarity.
Leading or trailing zeros can be used to justify a table of numbers left or right respectively.
For example, to me the following table could be improved. +---------+--------------------+ | float | mpf | +---------+--------------------+ | -1.00 | 0.3678794411714423 | | -0.75 | 0.4723665527410147 | | -0.50 | 0.6065306597126334 | | -0.25 | 0.7788007830714049 | | 0.00 | 1.000000000000000 | | 0.25 | 1.284025416687741 | | 0.50 | 1.648721270700128 | | 0.75 | 2.117000016612675 | +---------+--------------------+
Should be +---------+--------------------+ | float | mpf | +---------+--------------------+ | -1.00 | 0.3678794411714423 | | -0.75 | 0.4723665527410147 | | -0.50 | 0.6065306597126334 | | -0.25 | 0.7788007830714049 | | 0.00 | 1.0000000000000000 | | 0.25 | 1.284025416687741X | | 0.50 | 1.648721270700128Y | | 0.75 | 2.117000016612675Z | +---------+--------------------+
where X, Y, and Y are the relevant digits in each row.
Activity
skirpichev commentedon Feb 27, 2025
Could you reproduce this bug on development version of the mpmath?
WarrenWeckesser commentedon Feb 27, 2025
Here's what I get with the development version and Python 3.12.8:
The format works with a Python float:
ant1sir commentedon Feb 27, 2025
Are you satisfied with Warren's answer?
Relax requirements on precision format, allow 0-prefixes
skirpichev commentedon Feb 28, 2025
@WarrenWeckesser, thanks (I asked from phone). PR is ready: #916
skirpichev commentedon Feb 28, 2025
But see python/cpython#130662
Maybe we shouldn't replicate this Python feature.
Relax requirements on precision format, allow 0-prefixes
Relax requirements on precision format, allow 0-prefixes
Relax requirements on precision format, allow 0-prefixes
ant1sir commentedon Feb 28, 2025
A leading or trailing zero is supposed to be a correctly computed digit.
For example, 1.000000000000000 is different from 1.000000000000000123463563456.
Leading or trailing zeros can be used to justify a table of numbers left or right respectively.
For example, to me the following table could be improved.
+---------+--------------------+| float | mpf |+---------+--------------------+| -1.00 | 0.3678794411714423 || -0.75 | 0.4723665527410147 || -0.50 | 0.6065306597126334 || -0.25 | 0.7788007830714049 || 0.00 | 1.000000000000000 || 0.25 | 1.284025416687741 || 0.50 | 1.648721270700128 || 0.75 | 2.117000016612675 |+---------+--------------------+Should be
+---------+--------------------+| float | mpf |+---------+--------------------+| -1.00 | 0.3678794411714423 || -0.75 | 0.4723665527410147 || -0.50 | 0.6065306597126334 || -0.25 | 0.7788007830714049 || 0.00 | 1.0000000000000000 || 0.25 | 1.284025416687741X || 0.50 | 1.648721270700128Y || 0.75 | 2.117000016612675Z |+---------+--------------------+where X, Y, and Y are the relevant digits in each row.
9 remaining items
skirpichev commentedon Mar 4, 2025
Reopened, until python/cpython#130662 will be fixed. Meanwhile 849e185 was reverted in #936.
Revert "Revert 849e185 and (partially) f4cef9c (same for with field)"