blob: 057ff522516a3aed1b24caba5f591600f8aec841 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _floatobjects:
4
5Floating Point Objects
6----------------------
7
8.. index:: object: floating point
9
10
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:type:: PyFloatObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000012
Georg Brandl60203b42010-10-06 10:11:56 +000013 This subtype of :c:type:`PyObject` represents a Python floating point object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
15
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:var:: PyTypeObject PyFloat_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000017
Georg Brandl60203b42010-10-06 10:11:56 +000018 This instance of :c:type:`PyTypeObject` represents the Python floating point
Georg Brandl2aff3352010-10-17 10:59:41 +000019 type. This is the same object as :class:`float` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21
Georg Brandl60203b42010-10-06 10:11:56 +000022.. c:function:: int PyFloat_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
Georg Brandl60203b42010-10-06 10:11:56 +000024 Return true if its argument is a :c:type:`PyFloatObject` or a subtype of
25 :c:type:`PyFloatObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000026
27
Georg Brandl60203b42010-10-06 10:11:56 +000028.. c:function:: int PyFloat_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
Georg Brandl60203b42010-10-06 10:11:56 +000030 Return true if its argument is a :c:type:`PyFloatObject`, but not a subtype of
31 :c:type:`PyFloatObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
33
Georg Brandl60203b42010-10-06 10:11:56 +000034.. c:function:: PyObject* PyFloat_FromString(PyObject *str)
Georg Brandl54a3faa2008-01-20 09:30:57 +000035
Georg Brandl60203b42010-10-06 10:11:56 +000036 Create a :c:type:`PyFloatObject` object based on the string value in *str*, or
Georg Brandl54a3faa2008-01-20 09:30:57 +000037 *NULL* on failure.
38
39
Georg Brandl60203b42010-10-06 10:11:56 +000040.. c:function:: PyObject* PyFloat_FromDouble(double v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000041
Georg Brandl60203b42010-10-06 10:11:56 +000042 Create a :c:type:`PyFloatObject` object from *v*, or *NULL* on failure.
Georg Brandl54a3faa2008-01-20 09:30:57 +000043
44
Georg Brandl60203b42010-10-06 10:11:56 +000045.. c:function:: double PyFloat_AsDouble(PyObject *pyfloat)
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
Georg Brandl60203b42010-10-06 10:11:56 +000047 Return a C :c:type:`double` representation of the contents of *pyfloat*. If
Georg Brandl54a3faa2008-01-20 09:30:57 +000048 *pyfloat* is not a Python floating point object but has a :meth:`__float__`
49 method, this method will first be called to convert *pyfloat* into a float.
Serhiy Storchakabdbad712019-06-02 00:05:48 +030050 If ``__float__()`` is not defined then it falls back to :meth:`__index__`.
Antoine Pitrou07b1c872011-12-18 01:25:27 +010051 This method returns ``-1.0`` upon failure, so one should call
52 :c:func:`PyErr_Occurred` to check for errors.
Georg Brandl54a3faa2008-01-20 09:30:57 +000053
Serhiy Storchakabdbad712019-06-02 00:05:48 +030054 .. versionchanged:: 3.8
55 Use :meth:`__index__` if available.
56
Georg Brandl54a3faa2008-01-20 09:30:57 +000057
Georg Brandl60203b42010-10-06 10:11:56 +000058.. c:function:: double PyFloat_AS_DOUBLE(PyObject *pyfloat)
Georg Brandl54a3faa2008-01-20 09:30:57 +000059
Georg Brandl60203b42010-10-06 10:11:56 +000060 Return a C :c:type:`double` representation of the contents of *pyfloat*, but
Georg Brandl54a3faa2008-01-20 09:30:57 +000061 without error checking.
62
63
Georg Brandl60203b42010-10-06 10:11:56 +000064.. c:function:: PyObject* PyFloat_GetInfo(void)
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
66 Return a structseq instance which contains information about the
67 precision, minimum and maximum values of a float. It's a thin wrapper
68 around the header file :file:`float.h`.
69
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: double PyFloat_GetMax()
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
Georg Brandl60203b42010-10-06 10:11:56 +000073 Return the maximum representable finite float *DBL_MAX* as C :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000074
75
Georg Brandl60203b42010-10-06 10:11:56 +000076.. c:function:: double PyFloat_GetMin()
Georg Brandl54a3faa2008-01-20 09:30:57 +000077
Georg Brandl60203b42010-10-06 10:11:56 +000078 Return the minimum normalized positive float *DBL_MIN* as C :c:type:`double`.
Christian Heimes15ebc882008-02-04 18:48:49 +000079
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: int PyFloat_ClearFreeList()
Christian Heimes15ebc882008-02-04 18:48:49 +000081
Georg Brandl2ee470f2008-07-16 12:55:28 +000082 Clear the float free list. Return the number of items that could not
83 be freed.