-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-84978: Add float.from_number() and complex.from_number() #26827
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
Changes from 6 commits
f3e83a4
7946ef1
b9e1473
01204b7
4eb72c4
d912784
dd35752
a0ef263
c90cae7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -624,6 +624,23 @@ Additional Methods on Float | |
The float type implements the :class:`numbers.Real` :term:`abstract base | ||
class`. float also has the following additional methods. | ||
|
||
.. classmethod:: float.from_number(x) | ||
|
||
Class method to return a floating point number constructed from a number *x*. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Super-nitpick: I prefer the spelling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This descriptions was simply copied from the "floating-point" occurs 118 time, "floating point" occurs at least 159 times (in several cases it is split between lines, so it is not easy to get accurate number). So for now I leave "floating point" for consistency with the constructor description. We will solve this in a separate issue. |
||
|
||
If the argument is an integer or a floating point number, a | ||
floating point number with the same value (within Python's floating point | ||
precision) is returned. If the argument is outside the range of a Python | ||
float, an :exc:`OverflowError` will be raised. | ||
|
||
For a general Python object ``x``, ``float.from_number(x)`` delegates to | ||
``x.__float__()``. | ||
If :meth:`~object.__float__` is not defined then it falls back | ||
to :meth:`~object.__index__`. | ||
|
||
.. versionadded:: 3.13 | ||
|
||
|
||
.. method:: float.as_integer_ratio() | ||
|
||
Return a pair of integers whose ratio is exactly equal to the | ||
|
@@ -702,6 +719,25 @@ hexadecimal string representing the same number:: | |
'0x1.d380000000000p+11' | ||
|
||
|
||
Additional Methods on Complex | ||
----------------------------- | ||
|
||
The :class:`!complex` type implements the :class:`numbers.Complex` | ||
:term:`abstract base class`. | ||
:class:`!complex` also has the following additional methods. | ||
|
||
.. classmethod:: complex.from_number(x) | ||
|
||
Class method to convert a number to a complex number. | ||
|
||
For a general Python object ``x``, ``complex.from_number(x)`` delegates to | ||
``x.__complex__()``. If :meth:`~object.__complex__` is not defined then it falls back | ||
to :meth:`~object.__float__`. If :meth:`!__float__` is not defined then it falls back | ||
to :meth:`~object.__index__`. | ||
|
||
.. versionadded:: 3.13 | ||
|
||
|
||
.. _numeric-hash: | ||
|
||
Hashing of numeric types | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add class methods :meth:`float.from_number` and :meth:`complex.from_number`. |
Uh oh!
There was an error while loading. Please reload this page.