blob: 8ff2a2bebaa69259349b2e122773da5bca0ea52f [file] [log] [blame]
Serhiy Storchakab5c51d32017-03-11 09:21:05 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(float_is_integer__doc__,
6"is_integer($self, /)\n"
7"--\n"
8"\n"
9"Return True if the float is an integer.");
10
11#define FLOAT_IS_INTEGER_METHODDEF \
12 {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, float_is_integer__doc__},
13
14static PyObject *
15float_is_integer_impl(PyObject *self);
16
17static PyObject *
18float_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
19{
20 return float_is_integer_impl(self);
21}
22
23PyDoc_STRVAR(float___trunc____doc__,
24"__trunc__($self, /)\n"
25"--\n"
26"\n"
27"Return the Integral closest to x between 0 and x.");
28
29#define FLOAT___TRUNC___METHODDEF \
30 {"__trunc__", (PyCFunction)float___trunc__, METH_NOARGS, float___trunc____doc__},
31
32static PyObject *
33float___trunc___impl(PyObject *self);
34
35static PyObject *
36float___trunc__(PyObject *self, PyObject *Py_UNUSED(ignored))
37{
38 return float___trunc___impl(self);
39}
40
41PyDoc_STRVAR(float___round____doc__,
42"__round__($self, ndigits=None, /)\n"
43"--\n"
44"\n"
45"Return the Integral closest to x, rounding half toward even.\n"
46"\n"
47"When an argument is passed, work like built-in round(x, ndigits).");
48
49#define FLOAT___ROUND___METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020050 {"__round__", (PyCFunction)(void(*)(void))float___round__, METH_FASTCALL, float___round____doc__},
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020051
52static PyObject *
53float___round___impl(PyObject *self, PyObject *o_ndigits);
54
55static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020056float___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020057{
58 PyObject *return_value = NULL;
59 PyObject *o_ndigits = NULL;
60
Sylvain74453812017-06-10 06:51:48 +020061 if (!_PyArg_UnpackStack(args, nargs, "__round__",
62 0, 1,
63 &o_ndigits)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020064 goto exit;
65 }
66 return_value = float___round___impl(self, o_ndigits);
67
68exit:
69 return return_value;
70}
71
72PyDoc_STRVAR(float_conjugate__doc__,
73"conjugate($self, /)\n"
74"--\n"
75"\n"
76"Return self, the complex conjugate of any float.");
77
78#define FLOAT_CONJUGATE_METHODDEF \
79 {"conjugate", (PyCFunction)float_conjugate, METH_NOARGS, float_conjugate__doc__},
80
81static PyObject *
82float_conjugate_impl(PyObject *self);
83
84static PyObject *
85float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
86{
87 return float_conjugate_impl(self);
88}
89
90PyDoc_STRVAR(float_hex__doc__,
91"hex($self, /)\n"
92"--\n"
93"\n"
94"Return a hexadecimal representation of a floating-point number.\n"
95"\n"
96">>> (-0.1).hex()\n"
97"\'-0x1.999999999999ap-4\'\n"
98">>> 3.14159.hex()\n"
99"\'0x1.921f9f01b866ep+1\'");
100
101#define FLOAT_HEX_METHODDEF \
102 {"hex", (PyCFunction)float_hex, METH_NOARGS, float_hex__doc__},
103
104static PyObject *
105float_hex_impl(PyObject *self);
106
107static PyObject *
108float_hex(PyObject *self, PyObject *Py_UNUSED(ignored))
109{
110 return float_hex_impl(self);
111}
112
113PyDoc_STRVAR(float_fromhex__doc__,
114"fromhex($type, string, /)\n"
115"--\n"
116"\n"
117"Create a floating-point number from a hexadecimal string.\n"
118"\n"
119">>> float.fromhex(\'0x1.ffffp10\')\n"
120"2047.984375\n"
121">>> float.fromhex(\'-0x1p-1074\')\n"
122"-5e-324");
123
124#define FLOAT_FROMHEX_METHODDEF \
125 {"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, float_fromhex__doc__},
126
127PyDoc_STRVAR(float_as_integer_ratio__doc__,
128"as_integer_ratio($self, /)\n"
129"--\n"
130"\n"
131"Return integer ratio.\n"
132"\n"
133"Return a pair of integers, whose ratio is exactly equal to the original float\n"
134"and with a positive denominator.\n"
135"\n"
136"Raise OverflowError on infinities and a ValueError on NaNs.\n"
137"\n"
138">>> (10.0).as_integer_ratio()\n"
139"(10, 1)\n"
140">>> (0.0).as_integer_ratio()\n"
141"(0, 1)\n"
142">>> (-.25).as_integer_ratio()\n"
143"(-1, 4)");
144
145#define FLOAT_AS_INTEGER_RATIO_METHODDEF \
146 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, float_as_integer_ratio__doc__},
147
148static PyObject *
149float_as_integer_ratio_impl(PyObject *self);
150
151static PyObject *
152float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
153{
154 return float_as_integer_ratio_impl(self);
155}
156
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200157PyDoc_STRVAR(float_new__doc__,
158"float(x=0, /)\n"
159"--\n"
160"\n"
161"Convert a string or number to a floating point number, if possible.");
162
163static PyObject *
164float_new_impl(PyTypeObject *type, PyObject *x);
165
166static PyObject *
167float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
168{
169 PyObject *return_value = NULL;
Serhiy Storchakabae68812017-04-05 12:00:42 +0300170 PyObject *x = _PyLong_Zero;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200171
172 if ((type == &PyFloat_Type) &&
173 !_PyArg_NoKeywords("float", kwargs)) {
174 goto exit;
175 }
176 if (!PyArg_UnpackTuple(args, "float",
177 0, 1,
178 &x)) {
179 goto exit;
180 }
181 return_value = float_new_impl(type, x);
182
183exit:
184 return return_value;
185}
186
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200187PyDoc_STRVAR(float___getnewargs____doc__,
188"__getnewargs__($self, /)\n"
189"--\n"
190"\n");
191
192#define FLOAT___GETNEWARGS___METHODDEF \
193 {"__getnewargs__", (PyCFunction)float___getnewargs__, METH_NOARGS, float___getnewargs____doc__},
194
195static PyObject *
196float___getnewargs___impl(PyObject *self);
197
198static PyObject *
199float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
200{
201 return float___getnewargs___impl(self);
202}
203
204PyDoc_STRVAR(float___getformat____doc__,
205"__getformat__($type, typestr, /)\n"
206"--\n"
207"\n"
208"You probably don\'t want to use this function.\n"
209"\n"
210" typestr\n"
211" Must be \'double\' or \'float\'.\n"
212"\n"
213"It exists mainly to be used in Python\'s test suite.\n"
214"\n"
215"This function returns whichever of \'unknown\', \'IEEE, big-endian\' or \'IEEE,\n"
216"little-endian\' best describes the format of floating point numbers used by the\n"
217"C type named by typestr.");
218
219#define FLOAT___GETFORMAT___METHODDEF \
220 {"__getformat__", (PyCFunction)float___getformat__, METH_O|METH_CLASS, float___getformat____doc__},
221
222static PyObject *
223float___getformat___impl(PyTypeObject *type, const char *typestr);
224
225static PyObject *
226float___getformat__(PyTypeObject *type, PyObject *arg)
227{
228 PyObject *return_value = NULL;
229 const char *typestr;
230
231 if (!PyArg_Parse(arg, "s:__getformat__", &typestr)) {
232 goto exit;
233 }
234 return_value = float___getformat___impl(type, typestr);
235
236exit:
237 return return_value;
238}
239
240PyDoc_STRVAR(float___set_format____doc__,
241"__set_format__($type, typestr, fmt, /)\n"
242"--\n"
243"\n"
244"You probably don\'t want to use this function.\n"
245"\n"
246" typestr\n"
247" Must be \'double\' or \'float\'.\n"
248" fmt\n"
249" Must be one of \'unknown\', \'IEEE, big-endian\' or \'IEEE, little-endian\',\n"
250" and in addition can only be one of the latter two if it appears to\n"
251" match the underlying C reality.\n"
252"\n"
253"It exists mainly to be used in Python\'s test suite.\n"
254"\n"
255"Override the automatic determination of C-level floating point type.\n"
256"This affects how floats are converted to and from binary strings.");
257
258#define FLOAT___SET_FORMAT___METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200259 {"__set_format__", (PyCFunction)(void(*)(void))float___set_format__, METH_FASTCALL|METH_CLASS, float___set_format____doc__},
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200260
261static PyObject *
262float___set_format___impl(PyTypeObject *type, const char *typestr,
263 const char *fmt);
264
265static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200266float___set_format__(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200267{
268 PyObject *return_value = NULL;
269 const char *typestr;
270 const char *fmt;
271
Sylvain74453812017-06-10 06:51:48 +0200272 if (!_PyArg_ParseStack(args, nargs, "ss:__set_format__",
273 &typestr, &fmt)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200274 goto exit;
275 }
276 return_value = float___set_format___impl(type, typestr, fmt);
277
278exit:
279 return return_value;
280}
281
282PyDoc_STRVAR(float___format____doc__,
283"__format__($self, format_spec, /)\n"
284"--\n"
285"\n"
286"Formats the float according to format_spec.");
287
288#define FLOAT___FORMAT___METHODDEF \
289 {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
290
291static PyObject *
292float___format___impl(PyObject *self, PyObject *format_spec);
293
294static PyObject *
295float___format__(PyObject *self, PyObject *arg)
296{
297 PyObject *return_value = NULL;
298 PyObject *format_spec;
299
300 if (!PyArg_Parse(arg, "U:__format__", &format_spec)) {
301 goto exit;
302 }
303 return_value = float___format___impl(self, format_spec);
304
305exit:
306 return return_value;
307}
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200308/*[clinic end generated code: output=091dd499f5386a6c input=a9049054013a1b77]*/