Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _floatobjects: |
| 4 | |
| 5 | Floating Point Objects |
| 6 | ---------------------- |
| 7 | |
| 8 | .. index:: object: floating point |
| 9 | |
| 10 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 11 | .. c:type:: PyFloatObject |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 12 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 13 | This subtype of :c:type:`PyObject` represents a Python floating point object. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 14 | |
| 15 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 16 | .. c:var:: PyTypeObject PyFloat_Type |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 17 | |
| 18 | .. index:: single: FloatType (in modules types) |
| 19 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 20 | This instance of :c:type:`PyTypeObject` represents the Python floating point |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 21 | type. This is the same object as ``float`` and ``types.FloatType``. |
| 22 | |
| 23 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 24 | .. c:function:: int PyFloat_Check(PyObject *p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 25 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 26 | Return true if its argument is a :c:type:`PyFloatObject` or a subtype of |
| 27 | :c:type:`PyFloatObject`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 28 | |
| 29 | .. versionchanged:: 2.2 |
| 30 | Allowed subtypes to be accepted. |
| 31 | |
| 32 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 33 | .. c:function:: int PyFloat_CheckExact(PyObject *p) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 34 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 35 | Return true if its argument is a :c:type:`PyFloatObject`, but not a subtype of |
| 36 | :c:type:`PyFloatObject`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 37 | |
| 38 | .. versionadded:: 2.2 |
| 39 | |
| 40 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 41 | .. c:function:: PyObject* PyFloat_FromString(PyObject *str, char **pend) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 42 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 43 | Create a :c:type:`PyFloatObject` object based on the string value in *str*, or |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 44 | *NULL* on failure. The *pend* argument is ignored. It remains only for |
| 45 | backward compatibility. |
| 46 | |
| 47 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 48 | .. c:function:: PyObject* PyFloat_FromDouble(double v) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 49 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 50 | Create a :c:type:`PyFloatObject` object from *v*, or *NULL* on failure. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 51 | |
| 52 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 53 | .. c:function:: double PyFloat_AsDouble(PyObject *pyfloat) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 54 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 55 | Return a C :c:type:`double` representation of the contents of *pyfloat*. If |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 56 | *pyfloat* is not a Python floating point object but has a :meth:`__float__` |
| 57 | method, this method will first be called to convert *pyfloat* into a float. |
Antoine Pitrou | c9e1800 | 2011-12-18 01:25:27 +0100 | [diff] [blame] | 58 | This method returns ``-1.0`` upon failure, so one should call |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 59 | :c:func:`PyErr_Occurred` to check for errors. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 60 | |
| 61 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 62 | .. c:function:: double PyFloat_AS_DOUBLE(PyObject *pyfloat) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 63 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 64 | Return a C :c:type:`double` representation of the contents of *pyfloat*, but |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 65 | without error checking. |
| 66 | |
| 67 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 68 | .. c:function:: PyObject* PyFloat_GetInfo(void) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 69 | |
| 70 | Return a structseq instance which contains information about the |
| 71 | precision, minimum and maximum values of a float. It's a thin wrapper |
| 72 | around the header file :file:`float.h`. |
| 73 | |
| 74 | .. versionadded:: 2.6 |
| 75 | |
| 76 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 77 | .. c:function:: double PyFloat_GetMax() |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 78 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 79 | Return the maximum representable finite float *DBL_MAX* as C :c:type:`double`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 80 | |
| 81 | .. versionadded:: 2.6 |
| 82 | |
| 83 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 84 | .. c:function:: double PyFloat_GetMin() |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 85 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 86 | Return the minimum normalized positive float *DBL_MIN* as C :c:type:`double`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 87 | |
| 88 | .. versionadded:: 2.6 |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 89 | |
| 90 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 91 | .. c:function:: int PyFloat_ClearFreeList() |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 92 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 93 | Clear the float free list. Return the number of items that could not |
| 94 | be freed. |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 95 | |
| 96 | .. versionadded:: 2.6 |
Eric Smith | c12781a | 2009-10-19 14:38:14 +0000 | [diff] [blame] | 97 | |
| 98 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 99 | .. c:function:: void PyFloat_AsString(char *buf, PyFloatObject *v) |
Eric Smith | c12781a | 2009-10-19 14:38:14 +0000 | [diff] [blame] | 100 | |
| 101 | Convert the argument *v* to a string, using the same rules as |
| 102 | :func:`str`. The length of *buf* should be at least 100. |
| 103 | |
| 104 | This function is unsafe to call because it writes to a buffer whose |
| 105 | length it does not know. |
| 106 | |
| 107 | .. deprecated:: 2.7 |
| 108 | Use :func:`PyObject_Str` or :func:`PyOS_double_to_string` instead. |
| 109 | |
| 110 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 111 | .. c:function:: void PyFloat_AsReprString(char *buf, PyFloatObject *v) |
Eric Smith | c12781a | 2009-10-19 14:38:14 +0000 | [diff] [blame] | 112 | |
| 113 | Same as PyFloat_AsString, except uses the same rules as |
| 114 | :func:`repr`. The length of *buf* should be at least 100. |
| 115 | |
| 116 | This function is unsafe to call because it writes to a buffer whose |
| 117 | length it does not know. |
| 118 | |
| 119 | .. deprecated:: 2.7 |
| 120 | Use :func:`PyObject_Repr` or :func:`PyOS_double_to_string` instead. |