blob: 4251d63c247b9f3758664ee943b8c43a05705cce [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
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020061 if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020062 goto exit;
63 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020064 if (nargs < 1) {
65 goto skip_optional;
66 }
67 o_ndigits = args[0];
68skip_optional:
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020069 return_value = float___round___impl(self, o_ndigits);
70
71exit:
72 return return_value;
73}
74
75PyDoc_STRVAR(float_conjugate__doc__,
76"conjugate($self, /)\n"
77"--\n"
78"\n"
79"Return self, the complex conjugate of any float.");
80
81#define FLOAT_CONJUGATE_METHODDEF \
82 {"conjugate", (PyCFunction)float_conjugate, METH_NOARGS, float_conjugate__doc__},
83
84static PyObject *
85float_conjugate_impl(PyObject *self);
86
87static PyObject *
88float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
89{
90 return float_conjugate_impl(self);
91}
92
93PyDoc_STRVAR(float_hex__doc__,
94"hex($self, /)\n"
95"--\n"
96"\n"
97"Return a hexadecimal representation of a floating-point number.\n"
98"\n"
99">>> (-0.1).hex()\n"
100"\'-0x1.999999999999ap-4\'\n"
101">>> 3.14159.hex()\n"
102"\'0x1.921f9f01b866ep+1\'");
103
104#define FLOAT_HEX_METHODDEF \
105 {"hex", (PyCFunction)float_hex, METH_NOARGS, float_hex__doc__},
106
107static PyObject *
108float_hex_impl(PyObject *self);
109
110static PyObject *
111float_hex(PyObject *self, PyObject *Py_UNUSED(ignored))
112{
113 return float_hex_impl(self);
114}
115
116PyDoc_STRVAR(float_fromhex__doc__,
117"fromhex($type, string, /)\n"
118"--\n"
119"\n"
120"Create a floating-point number from a hexadecimal string.\n"
121"\n"
122">>> float.fromhex(\'0x1.ffffp10\')\n"
123"2047.984375\n"
124">>> float.fromhex(\'-0x1p-1074\')\n"
125"-5e-324");
126
127#define FLOAT_FROMHEX_METHODDEF \
128 {"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, float_fromhex__doc__},
129
130PyDoc_STRVAR(float_as_integer_ratio__doc__,
131"as_integer_ratio($self, /)\n"
132"--\n"
133"\n"
134"Return integer ratio.\n"
135"\n"
136"Return a pair of integers, whose ratio is exactly equal to the original float\n"
137"and with a positive denominator.\n"
138"\n"
139"Raise OverflowError on infinities and a ValueError on NaNs.\n"
140"\n"
141">>> (10.0).as_integer_ratio()\n"
142"(10, 1)\n"
143">>> (0.0).as_integer_ratio()\n"
144"(0, 1)\n"
145">>> (-.25).as_integer_ratio()\n"
146"(-1, 4)");
147
148#define FLOAT_AS_INTEGER_RATIO_METHODDEF \
149 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, float_as_integer_ratio__doc__},
150
151static PyObject *
152float_as_integer_ratio_impl(PyObject *self);
153
154static PyObject *
155float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
156{
157 return float_as_integer_ratio_impl(self);
158}
159
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200160PyDoc_STRVAR(float_new__doc__,
161"float(x=0, /)\n"
162"--\n"
163"\n"
164"Convert a string or number to a floating point number, if possible.");
165
166static PyObject *
167float_new_impl(PyTypeObject *type, PyObject *x);
168
169static PyObject *
170float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
171{
172 PyObject *return_value = NULL;
Serhiy Storchakabae68812017-04-05 12:00:42 +0300173 PyObject *x = _PyLong_Zero;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200174
175 if ((type == &PyFloat_Type) &&
176 !_PyArg_NoKeywords("float", kwargs)) {
177 goto exit;
178 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200179 if (!_PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1)) {
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200180 goto exit;
181 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200182 if (PyTuple_GET_SIZE(args) < 1) {
183 goto skip_optional;
184 }
185 x = PyTuple_GET_ITEM(args, 0);
186skip_optional:
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200187 return_value = float_new_impl(type, x);
188
189exit:
190 return return_value;
191}
192
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200193PyDoc_STRVAR(float___getnewargs____doc__,
194"__getnewargs__($self, /)\n"
195"--\n"
196"\n");
197
198#define FLOAT___GETNEWARGS___METHODDEF \
199 {"__getnewargs__", (PyCFunction)float___getnewargs__, METH_NOARGS, float___getnewargs____doc__},
200
201static PyObject *
202float___getnewargs___impl(PyObject *self);
203
204static PyObject *
205float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
206{
207 return float___getnewargs___impl(self);
208}
209
210PyDoc_STRVAR(float___getformat____doc__,
211"__getformat__($type, typestr, /)\n"
212"--\n"
213"\n"
214"You probably don\'t want to use this function.\n"
215"\n"
216" typestr\n"
217" Must be \'double\' or \'float\'.\n"
218"\n"
219"It exists mainly to be used in Python\'s test suite.\n"
220"\n"
221"This function returns whichever of \'unknown\', \'IEEE, big-endian\' or \'IEEE,\n"
222"little-endian\' best describes the format of floating point numbers used by the\n"
223"C type named by typestr.");
224
225#define FLOAT___GETFORMAT___METHODDEF \
226 {"__getformat__", (PyCFunction)float___getformat__, METH_O|METH_CLASS, float___getformat____doc__},
227
228static PyObject *
229float___getformat___impl(PyTypeObject *type, const char *typestr);
230
231static PyObject *
232float___getformat__(PyTypeObject *type, PyObject *arg)
233{
234 PyObject *return_value = NULL;
235 const char *typestr;
236
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200237 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200238 _PyArg_BadArgument("__getformat__", 0, "str", arg);
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200239 goto exit;
240 }
241 Py_ssize_t typestr_length;
242 typestr = PyUnicode_AsUTF8AndSize(arg, &typestr_length);
243 if (typestr == NULL) {
244 goto exit;
245 }
246 if (strlen(typestr) != (size_t)typestr_length) {
247 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200248 goto exit;
249 }
250 return_value = float___getformat___impl(type, typestr);
251
252exit:
253 return return_value;
254}
255
256PyDoc_STRVAR(float___set_format____doc__,
257"__set_format__($type, typestr, fmt, /)\n"
258"--\n"
259"\n"
260"You probably don\'t want to use this function.\n"
261"\n"
262" typestr\n"
263" Must be \'double\' or \'float\'.\n"
264" fmt\n"
265" Must be one of \'unknown\', \'IEEE, big-endian\' or \'IEEE, little-endian\',\n"
266" and in addition can only be one of the latter two if it appears to\n"
267" match the underlying C reality.\n"
268"\n"
269"It exists mainly to be used in Python\'s test suite.\n"
270"\n"
271"Override the automatic determination of C-level floating point type.\n"
272"This affects how floats are converted to and from binary strings.");
273
274#define FLOAT___SET_FORMAT___METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200275 {"__set_format__", (PyCFunction)(void(*)(void))float___set_format__, METH_FASTCALL|METH_CLASS, float___set_format____doc__},
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200276
277static PyObject *
278float___set_format___impl(PyTypeObject *type, const char *typestr,
279 const char *fmt);
280
281static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200282float___set_format__(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200283{
284 PyObject *return_value = NULL;
285 const char *typestr;
286 const char *fmt;
287
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200288 if (!_PyArg_CheckPositional("__set_format__", nargs, 2, 2)) {
289 goto exit;
290 }
291 if (!PyUnicode_Check(args[0])) {
292 _PyArg_BadArgument("__set_format__", 1, "str", args[0]);
293 goto exit;
294 }
295 Py_ssize_t typestr_length;
296 typestr = PyUnicode_AsUTF8AndSize(args[0], &typestr_length);
297 if (typestr == NULL) {
298 goto exit;
299 }
300 if (strlen(typestr) != (size_t)typestr_length) {
301 PyErr_SetString(PyExc_ValueError, "embedded null character");
302 goto exit;
303 }
304 if (!PyUnicode_Check(args[1])) {
305 _PyArg_BadArgument("__set_format__", 2, "str", args[1]);
306 goto exit;
307 }
308 Py_ssize_t fmt_length;
309 fmt = PyUnicode_AsUTF8AndSize(args[1], &fmt_length);
310 if (fmt == NULL) {
311 goto exit;
312 }
313 if (strlen(fmt) != (size_t)fmt_length) {
314 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200315 goto exit;
316 }
317 return_value = float___set_format___impl(type, typestr, fmt);
318
319exit:
320 return return_value;
321}
322
323PyDoc_STRVAR(float___format____doc__,
324"__format__($self, format_spec, /)\n"
325"--\n"
326"\n"
327"Formats the float according to format_spec.");
328
329#define FLOAT___FORMAT___METHODDEF \
330 {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
331
332static PyObject *
333float___format___impl(PyObject *self, PyObject *format_spec);
334
335static PyObject *
336float___format__(PyObject *self, PyObject *arg)
337{
338 PyObject *return_value = NULL;
339 PyObject *format_spec;
340
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200341 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200342 _PyArg_BadArgument("__format__", 0, "str", arg);
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200343 goto exit;
344 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200345 if (PyUnicode_READY(arg) == -1) {
346 goto exit;
347 }
348 format_spec = arg;
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200349 return_value = float___format___impl(self, format_spec);
350
351exit:
352 return return_value;
353}
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200354/*[clinic end generated code: output=c183029d87dd41fa input=a9049054013a1b77]*/