Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ are always available. They are listed here in alphabetical order.
single: NaN
single: Infinity

Return a floating-point number constructed from a number or a string.
Return a floating-point number constructed from a string, number, or bytes-like object, if possible.

Examples:

Expand All @@ -794,12 +794,26 @@ are always available. They are listed here in alphabetical order.
1000000.0
>>> float('-Infinity')
-inf
>>> float(b'3.2e2')
320.0
>>> float(bytearray(b'-2.5'))
-2.5
>>> float(memoryview(b'6.7'))
6.7
>>> import array
>>> float(array.array('b', [49, 50, 51]))
123.0

If the argument is a string, it should contain a decimal number, optionally
preceded by a sign, and optionally embedded in whitespace. The optional
sign may be ``'+'`` or ``'-'``; a ``'+'`` sign has no effect on the value
produced. The argument may also be a string representing a NaN
(not-a-number), or positive or negative infinity.

If the argument is a bytes-like object, its contents are parsed as a decimal
string. Therefore, the bytes-like object must contain a valid representation
of a floating-point number, otherwise :exc:`ValueError` is raised.

More precisely, the input must conform to the :token:`~float:floatvalue`
production rule in the following grammar, after leading and trailing
whitespace characters are removed:
Expand Down
4 changes: 2 additions & 2 deletions Objects/clinic/floatobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1560,12 +1560,12 @@ float.__new__ as float_new
x: object(c_default="NULL") = 0
/

Convert a string or number to a floating-point number, if possible.
Convert a string, number, or bytes-like object to a float, if possible.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the glossary, bytes-like is:

An object that supports the Buffer Protocol and can export a C-contiguous buffer.

That includes e.g. array.array(), which apparently not supported by the float constructor:

>>> import array
>>> array.array('i', [123])
array('i', [123])
>>> float(array.array('i', [123]))
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    float(array.array('i', [123]))
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: array('i', [123])

Please correct docstring, see the int()'s docstring as an example.

@Qanty228 Qanty228 Aug 11, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. A bytes-like object can be converted to a float if its contents are parsed as an ASCII
representation of a decimal number:

>>> import array
>>> array.array('b', [49, 50, 51])
array('b', [49, 50, 51])
>>> float(array.array('b', [49, 50, 51])) # '1', '2', '3'
123.0
>>> float(array.array('B', [32, 45, 49, 46, 49])) # ' ', '-', '1', '.', '1' 
-1.1
>>> float(array.array('B', [49, 101, 49])) # '1', 'e', '1'
10.0
>>> float(array.array('i', [49, 50, 51]))
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    float(array.array('i', [49, 50, 51]))
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: could not convert string to float: array('i', [49, 50, 51])

[clinic start generated code]*/

static PyObject *
float_new_impl(PyTypeObject *type, PyObject *x)
/*[clinic end generated code: output=ccf1e8dc460ba6ba input=55909f888aa0c8a6]*/
/*[clinic end generated code: output=ccf1e8dc460ba6ba input=27eb1f7c62226a39]*/
{
if (type != &PyFloat_Type) {
if (x == NULL) {
Expand Down
Loading