diff options
author | Mark Dickinson <dickinsm@gmail.com> | 2008-07-16 11:30:51 (GMT) |
---|---|---|
committer | Mark Dickinson <dickinsm@gmail.com> | 2008-07-16 11:30:51 (GMT) |
commit | 65fe25e59710eada7eb41bddb45bdca8b61b5668 (patch) | |
tree | 9120eb8e01c582ba51b4732cdb21451ebe84b637 /Doc | |
parent | 0c474d01a19e1237940a049a981ddc38588cde58 (diff) | |
download | cpython-65fe25e59710eada7eb41bddb45bdca8b61b5668.zip cpython-65fe25e59710eada7eb41bddb45bdca8b61b5668.tar.gz cpython-65fe25e59710eada7eb41bddb45bdca8b61b5668.tar.bz2 |
Merged revisions 64974 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64974 | mark.dickinson | 2008-07-15 20:08:33 +0100 (Tue, 15 Jul 2008) | 3 lines
Issue #3008: add instance method float.hex and class method float.fromhex
to convert floats to and from hexadecimal strings respectively.
........
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/stdtypes.rst | 65 | ||||
-rw-r--r-- | Doc/whatsnew/2.6.rst | 5 |
2 files changed, 70 insertions, 0 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index a97a519..586770a 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -423,6 +423,71 @@ Notes: .. _typeiter: + +Additional Methods on Float +--------------------------- + +The float type has some additional methods to support conversion to +and from hexadecimal strings. Since Python's floats are stored +internally as binary numbers, converting a float to or from a +*decimal* string usually involves a small rounding error. In +contrast, hexadecimal strings allow exact representation and +specification of floating-point numbers. This can be useful when +debugging, and in numerical work. + + +.. method:: float.hex() + + Return a representation of a floating-point number as a hexadecimal + string. For finite floating-point numbers, this representation + will always include a leading ``0x`` and a trailing ``p`` and + exponent. + + +.. method:: float.fromhex(s) + + Class method to return the float represented by a hexadecimal + string *s*. The string *s* may have leading and trailing + whitespace. + + +Note that :meth:`float.hex` is an instance method, while +:meth:`float.fromhex` is a class method. + +A hexadecimal string takes the form:: + + [sign] ['0x'] integer ['.' fraction] ['p' exponent] + +where the optional ``sign`` may by either ``+`` or ``-``, ``integer`` +and ``fraction`` are strings of hexadecimal digits, and ``exponent`` +is a decimal integer with an optional leading sign. Case is not +significant, and there must be at least one hexadecimal digit in +either the integer or the fraction. This syntax is similar to the +syntax specified in section 6.4.4.2 of the C99 standard, and also to +the syntax used in Java 1.5 onwards. In particular, the output of +:meth:`float.hex` is usable as a hexadecimal floating-point literal in +C or Java code, and hexadecimal strings produced by C's ``%a`` format +character or Java's ``Double.toHexString`` are accepted by +:meth:`float.fromhex`. + + +Note that the exponent is written in decimal rather than hexadecimal, +and that it gives the power of 2 by which to multiply the coefficient. +For example, the hexadecimal string ``0x3.a7p10`` represents the +floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or +``3740.0``:: + + >>> float.fromhex('0x3.a7p10') + 3740.0 + + +Applying the reverse conversion to ``3740.0`` gives a different +hexadecimal string representing the same number:: + + >>> float.hex(3740.0) + '0x1.d380000000000p+11' + + Iterator Types ============== diff --git a/Doc/whatsnew/2.6.rst b/Doc/whatsnew/2.6.rst index df5ccb6..264ec2e 100644 --- a/Doc/whatsnew/2.6.rst +++ b/Doc/whatsnew/2.6.rst @@ -1397,6 +1397,11 @@ Here are all of the changes that Python 2.6 makes to the core Python language. :func:`isnan`, return true if their floating-point argument is infinite or Not A Number. (:issue:`1640`) + The float type has a new instance method :meth:`float.hex` and a + corresponding new class method :meth:`float.fromhex` to convert + floating-point numbers to and from hexadecimal strings, + respectively. (:issue:`3008`) + * The :mod:`math` module has a number of new functions, and the existing functions have been improved to give more consistent behaviour across platforms, especially with respect to handling of |