Skip to content

Support binary/hexadecimal string output #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2024
Merged

Conversation

skirpichev
Copy link
Collaborator

@skirpichev skirpichev commented Jun 28, 2023

Adapted from #414.

Closes #345

Co-authored-by: Sergey B Kirpichev [email protected]

  • this version doesn't touch argument names (dps->ps)
  • no support for float.hex()-style binary exponents

PS: from_str() part was implemented in #703. @asmeurer, I would appreciate your review, as you've opened #345.

@skirpichev skirpichev force-pushed the hex branch 2 times, most recently from 0933759 to 94ca8ab Compare October 7, 2023 03:34
@skirpichev skirpichev marked this pull request as draft October 7, 2023 03:35
Adapted from mpmath#414.

Partial fix for mpmath#345

Co-authored-by: Sergey B Kirpichev <[email protected]>

* this version doesn't touch argument names (dps->ps)
* no support for float.hex()-style binary exponents
@skirpichev skirpichev marked this pull request as ready for review February 22, 2024 09:10
@skirpichev skirpichev force-pushed the hex branch 2 times, most recently from 7ff54f0 to 4a3eae2 Compare February 22, 2024 09:47
@skirpichev skirpichev changed the title Support binary/octal/hexadecimal string output Support binary/hexadecimal string input/output Feb 23, 2024
@skirpichev skirpichev changed the title Support binary/hexadecimal string input/output Support binary/hexadecimal string output Feb 23, 2024
@skirpichev
Copy link
Collaborator Author

This is ready for review. I would like to merge this in few days to use that for mpf.__format__().

@oscarbenjamin
Copy link

How would you use this functionality with mpf rather than _mpf_?

>>> import mpmath
>>> from mpmath.libmp import to_str, from_str
>>> f = mpmath.sqrt(2)
>>> to_str(f, 10, base=2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/oscar/current/active/mpmath/mpmath/libmp/libmpf.py", line 1128, in to_str
    if not s[1]:
           ~^^^
TypeError: 'mpf' object is not subscriptable
>>> to_str(f._mpf_, 10, base=2)
'0b1.0110101'

It looks like the mpf constructor accepts it:

>>> mpmath.mpf('0b1.0110101')
mpf('1.4140625')

@skirpichev
Copy link
Collaborator Author

How would you use this functionality with mpf rather than mpf?

First, we could provide __format__() dunders to support "new-style" formatting of mpf/mpc's (issue #337). See e.g. gmpy2: https://gmpy2.readthedocs.io/en/latest/mpfr.html#gmpy2.mpfr.__format__ I'm planning to do this like python/cpython#113805 (i.e. "x" will be used to format numbers in the hexadecimal notation). Another option (as we don't support old-style string formatting) may be using "a" letter for this (in printf-style, like the MPFR). Support for optional rounding mode (gmpy2-like) also does make sense for me (see #757). Maybe we should also support formatting binary floats ("b"?).

It looks like the mpf constructor accepts it

Yes, strings with prefixes should be accepted automatically. Most important case is hexadecimal floats, of course - this will allow us to pass exact floating point values to the mpf constructor (see also CPython pr: python/cpython#114668).

But to handle arbitrary bases we should add base keyword argument (default=0) to mpf/mpc constructors (as gmpy2 does).

@skirpichev skirpichev merged commit 22859b0 into mpmath:master Feb 27, 2024
@skirpichev skirpichev deleted the hex branch February 27, 2024 02:28
@skirpichev skirpichev mentioned this pull request Mar 30, 2024
@skirpichev skirpichev added this to the 1.4 milestone May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hex representation
3 participants