Skip to content

Commit

Permalink
Trac #24568: Deprecate sage.rings.real_mpfr.RealNumber.__hex__
Browse files Browse the repository at this point in the history
This has more or less the same issue as #24514.  Even Python `float`s
didn't implement `__hex__`, which it seems really only should be
implemented for int-like classes.

Also fixes the hex method for Python 3 to properly return a `str`
instead of `bytes`.

URL: https://trac.sagemath.org/24568
Reported by: embray
Ticket author(s): Erik Bray
Reviewer(s): Jeroen Demeyer
  • Loading branch information
Release Manager authored and vbraun committed Feb 15, 2018
2 parents 77bcb47 + 9985ee8 commit c273e63
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/sage/rings/real_mpfr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ TESTS::
sage: -1e30
-1.00000000000000e30
sage: hex(-1. + 2^-52)
sage: (-1. + 2^-52).hex()
'-0xf.ffffffffffffp-4'
Make sure we don't have a new field for every new literal::
Expand Down Expand Up @@ -131,6 +131,7 @@ from sage.libs.gmp.mpz cimport *
from sage.libs.mpfr cimport *
from sage.misc.randstate cimport randstate, current_randstate
from sage.cpython.string cimport char_to_str
from sage.misc.superseded import deprecation

from sage.structure.element cimport RingElement, Element, ModuleElement
from sage.structure.richcmp cimport rich_to_bool_sgn
Expand Down Expand Up @@ -2041,31 +2042,31 @@ cdef class RealNumber(sage.structure.element.RingElement):

return z

def __hex__(self):
def hex(self):
"""
Return a hexadecimal floating-point representation of ``self``, in the
style of C99 hexadecimal floating-point constants.
EXAMPLES::
sage: hex(RR(-1/3))
sage: RR(-1/3).hex()
'-0x5.5555555555554p-4'
sage: hex(Reals(100)(123.456e789))
sage: Reals(100)(123.456e789).hex()
'0xf.721008e90630c8da88f44dd2p+2624'
sage: hex((-0.))
sage: (-0.).hex()
'-0x0p+0'
::
sage: [(hex(a), float(a).hex()) for a in [.5, 1., 2., 16.]]
sage: [(a.hex(), float(a).hex()) for a in [.5, 1., 2., 16.]]
[('0x8p-4', '0x1.0000000000000p-1'),
('0x1p+0', '0x1.0000000000000p+0'),
('0x2p+0', '0x1.0000000000000p+1'),
('0x1p+4', '0x1.0000000000000p+4')]
Special values::
sage: [hex(RR(s)) for s in ['+inf', '-inf', 'nan']]
sage: [RR(s).hex() for s in ['+inf', '-inf', 'nan']]
['inf', '-inf', 'nan']
"""
cdef char *s
Expand All @@ -2075,10 +2076,23 @@ cdef class RealNumber(sage.structure.element.RingElement):
sig_off()
if r < 0: # MPFR free()s its buffer itself in this case
raise RuntimeError("unable to convert an mpfr number to a string")
t = str(s)
t = char_to_str(s)
mpfr_free_str(s)
return t

def __hex__(self):
"""
TESTS::
sage: hex(RR(-1/3)) # py2
doctest:...:
DeprecationWarning: use the method .hex instead
See http://trac.sagemath.org/24568 for details.
'-0x5.5555555555554p-4'
"""
deprecation(24568, 'use the method .hex instead')
return self.hex()

def __copy__(self):
"""
Return copy of ``self`` - since ``self`` is immutable, we just return
Expand Down

0 comments on commit c273e63

Please sign in to comment.