446
446
A right shift by *n* bits is equivalent to division by ``pow(2, n)``.
449
Additional Methods on Float
450
---------------------------
452
The float type has some additional methods.
454
.. method:: float.as_integer_ratio()
456
Return a pair of integers whose ratio is exactly equal to the
457
original float and with a positive denominator. Raises
458
:exc:`OverflowError` on infinities and a :exc:`ValueError` on
461
.. versionadded:: 2.6
463
Two methods support conversion to
464
and from hexadecimal strings. Since Python's floats are stored
465
internally as binary numbers, converting a float to or from a
466
*decimal* string usually involves a small rounding error. In
467
contrast, hexadecimal strings allow exact representation and
468
specification of floating-point numbers. This can be useful when
469
debugging, and in numerical work.
472
.. method:: float.hex()
474
Return a representation of a floating-point number as a hexadecimal
475
string. For finite floating-point numbers, this representation
476
will always include a leading ``0x`` and a trailing ``p`` and
479
.. versionadded:: 2.6
482
.. method:: float.fromhex(s)
484
Class method to return the float represented by a hexadecimal
485
string *s*. The string *s* may have leading and trailing
488
.. versionadded:: 2.6
491
Note that :meth:`float.hex` is an instance method, while
492
:meth:`float.fromhex` is a class method.
494
A hexadecimal string takes the form::
496
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
498
where the optional ``sign`` may by either ``+`` or ``-``, ``integer``
499
and ``fraction`` are strings of hexadecimal digits, and ``exponent``
500
is a decimal integer with an optional leading sign. Case is not
501
significant, and there must be at least one hexadecimal digit in
502
either the integer or the fraction. This syntax is similar to the
503
syntax specified in section 6.4.4.2 of the C99 standard, and also to
504
the syntax used in Java 1.5 onwards. In particular, the output of
505
:meth:`float.hex` is usable as a hexadecimal floating-point literal in
506
C or Java code, and hexadecimal strings produced by C's ``%a`` format
507
character or Java's ``Double.toHexString`` are accepted by
508
:meth:`float.fromhex`.
511
Note that the exponent is written in decimal rather than hexadecimal,
512
and that it gives the power of 2 by which to multiply the coefficient.
513
For example, the hexadecimal string ``0x3.a7p10`` represents the
514
floating-point number ``(3 + 10./16 + 7./16**2) * 2.0**10``, or
517
>>> float.fromhex('0x3.a7p10')
521
Applying the reverse conversion to ``3740.0`` gives a different
522
hexadecimal string representing the same number::
524
>>> float.hex(3740.0)
525
'0x1.d380000000000p+11'
1575
1654
.. versionchanged:: 2.6
1576
1655
Accepts multiple input iterables.
1578
.. method:: intersection(other)
1657
.. method:: intersection(other, ...)
1581
1660
Return a new set with elements common to both sets.
1583
.. method:: difference(other)
1586
Return a new set with elements in the set that are not in *other*.
1662
.. versionchanged:: 2.6
1663
Accepts multiple input iterables.
1665
.. method:: difference(other, ...)
1668
Return a new set with elements in the set that are not in the others.
1670
.. versionchanged:: 2.6
1671
Accepts multiple input iterables.
1588
1673
.. method:: symmetric_difference(other)
1639
1724
.. versionchanged:: 2.6
1640
1725
Accepts multiple input iterables.
1642
.. method:: intersection_update(other)
1727
.. method:: intersection_update(other, ...)
1645
1730
Update the set, keeping only elements found in it and *other*.
1647
.. method:: difference_update(other)
1650
Update the set, removing elements found in *other*.
1732
.. versionchanged:: 2.6
1733
Accepts multiple input iterables.
1735
.. method:: difference_update(other, ...)
1738
Update the set, removing elements found in others.
1740
.. versionchanged:: 2.6
1741
Accepts multiple input iterables.
1652
1743
.. method:: symmetric_difference_update(other)
2043
2134
files, like ttys, it makes sense to continue reading after an EOF is hit.) Note
2044
2135
that this method may call the underlying C function :cfunc:`fread` more than
2045
2136
once in an effort to acquire as close to *size* bytes as possible. Also note
2046
that when in non-blocking mode, less data than what was requested may be
2137
that when in non-blocking mode, less data than was requested may be
2047
2138
returned, even if no *size* parameter was given.
2141
This function is simply a wrapper for the underlying
2142
:cfunc:`fread` C function, and will behave the same in corner cases,
2143
such as whether the EOF value is cached.
2050
2146
.. method:: file.readline([size])