blob: b7554832b5a8a268a0e766db02621d6f030c4ce0 [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
Batuhan Taşkayacb8b9462019-12-16 01:00:28 +030041PyDoc_STRVAR(float___floor____doc__,
42"__floor__($self, /)\n"
43"--\n"
44"\n"
45"Return the floor as an Integral.");
46
47#define FLOAT___FLOOR___METHODDEF \
48 {"__floor__", (PyCFunction)float___floor__, METH_NOARGS, float___floor____doc__},
49
50static PyObject *
51float___floor___impl(PyObject *self);
52
53static PyObject *
54float___floor__(PyObject *self, PyObject *Py_UNUSED(ignored))
55{
56 return float___floor___impl(self);
57}
58
59PyDoc_STRVAR(float___ceil____doc__,
60"__ceil__($self, /)\n"
61"--\n"
62"\n"
63"Return the ceiling as an Integral.");
64
65#define FLOAT___CEIL___METHODDEF \
66 {"__ceil__", (PyCFunction)float___ceil__, METH_NOARGS, float___ceil____doc__},
67
68static PyObject *
69float___ceil___impl(PyObject *self);
70
71static PyObject *
72float___ceil__(PyObject *self, PyObject *Py_UNUSED(ignored))
73{
74 return float___ceil___impl(self);
75}
76
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020077PyDoc_STRVAR(float___round____doc__,
78"__round__($self, ndigits=None, /)\n"
79"--\n"
80"\n"
81"Return the Integral closest to x, rounding half toward even.\n"
82"\n"
83"When an argument is passed, work like built-in round(x, ndigits).");
84
85#define FLOAT___ROUND___METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020086 {"__round__", (PyCFunction)(void(*)(void))float___round__, METH_FASTCALL, float___round____doc__},
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020087
88static PyObject *
89float___round___impl(PyObject *self, PyObject *o_ndigits);
90
91static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020092float___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020093{
94 PyObject *return_value = NULL;
Serhiy Storchaka279f4462019-09-14 12:24:05 +030095 PyObject *o_ndigits = Py_None;
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020096
Serhiy Storchaka2a39d252019-01-11 18:01:42 +020097 if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020098 goto exit;
99 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200100 if (nargs < 1) {
101 goto skip_optional;
102 }
103 o_ndigits = args[0];
104skip_optional:
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200105 return_value = float___round___impl(self, o_ndigits);
106
107exit:
108 return return_value;
109}
110
111PyDoc_STRVAR(float_conjugate__doc__,
112"conjugate($self, /)\n"
113"--\n"
114"\n"
115"Return self, the complex conjugate of any float.");
116
117#define FLOAT_CONJUGATE_METHODDEF \
118 {"conjugate", (PyCFunction)float_conjugate, METH_NOARGS, float_conjugate__doc__},
119
120static PyObject *
121float_conjugate_impl(PyObject *self);
122
123static PyObject *
124float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
125{
126 return float_conjugate_impl(self);
127}
128
129PyDoc_STRVAR(float_hex__doc__,
130"hex($self, /)\n"
131"--\n"
132"\n"
133"Return a hexadecimal representation of a floating-point number.\n"
134"\n"
135">>> (-0.1).hex()\n"
136"\'-0x1.999999999999ap-4\'\n"
137">>> 3.14159.hex()\n"
138"\'0x1.921f9f01b866ep+1\'");
139
140#define FLOAT_HEX_METHODDEF \
141 {"hex", (PyCFunction)float_hex, METH_NOARGS, float_hex__doc__},
142
143static PyObject *
144float_hex_impl(PyObject *self);
145
146static PyObject *
147float_hex(PyObject *self, PyObject *Py_UNUSED(ignored))
148{
149 return float_hex_impl(self);
150}
151
152PyDoc_STRVAR(float_fromhex__doc__,
153"fromhex($type, string, /)\n"
154"--\n"
155"\n"
156"Create a floating-point number from a hexadecimal string.\n"
157"\n"
158">>> float.fromhex(\'0x1.ffffp10\')\n"
159"2047.984375\n"
160">>> float.fromhex(\'-0x1p-1074\')\n"
161"-5e-324");
162
163#define FLOAT_FROMHEX_METHODDEF \
164 {"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, float_fromhex__doc__},
165
166PyDoc_STRVAR(float_as_integer_ratio__doc__,
167"as_integer_ratio($self, /)\n"
168"--\n"
169"\n"
170"Return integer ratio.\n"
171"\n"
172"Return a pair of integers, whose ratio is exactly equal to the original float\n"
173"and with a positive denominator.\n"
174"\n"
175"Raise OverflowError on infinities and a ValueError on NaNs.\n"
176"\n"
177">>> (10.0).as_integer_ratio()\n"
178"(10, 1)\n"
179">>> (0.0).as_integer_ratio()\n"
180"(0, 1)\n"
181">>> (-.25).as_integer_ratio()\n"
182"(-1, 4)");
183
184#define FLOAT_AS_INTEGER_RATIO_METHODDEF \
185 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, float_as_integer_ratio__doc__},
186
187static PyObject *
188float_as_integer_ratio_impl(PyObject *self);
189
190static PyObject *
191float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
192{
193 return float_as_integer_ratio_impl(self);
194}
195
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200196PyDoc_STRVAR(float_new__doc__,
197"float(x=0, /)\n"
198"--\n"
199"\n"
200"Convert a string or number to a floating point number, if possible.");
201
202static PyObject *
203float_new_impl(PyTypeObject *type, PyObject *x);
204
205static PyObject *
206float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
207{
208 PyObject *return_value = NULL;
Serhiy Storchakabae68812017-04-05 12:00:42 +0300209 PyObject *x = _PyLong_Zero;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200210
211 if ((type == &PyFloat_Type) &&
212 !_PyArg_NoKeywords("float", kwargs)) {
213 goto exit;
214 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200215 if (!_PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1)) {
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200216 goto exit;
217 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200218 if (PyTuple_GET_SIZE(args) < 1) {
219 goto skip_optional;
220 }
221 x = PyTuple_GET_ITEM(args, 0);
222skip_optional:
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200223 return_value = float_new_impl(type, x);
224
225exit:
226 return return_value;
227}
228
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200229PyDoc_STRVAR(float___getnewargs____doc__,
230"__getnewargs__($self, /)\n"
231"--\n"
232"\n");
233
234#define FLOAT___GETNEWARGS___METHODDEF \
235 {"__getnewargs__", (PyCFunction)float___getnewargs__, METH_NOARGS, float___getnewargs____doc__},
236
237static PyObject *
238float___getnewargs___impl(PyObject *self);
239
240static PyObject *
241float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
242{
243 return float___getnewargs___impl(self);
244}
245
246PyDoc_STRVAR(float___getformat____doc__,
247"__getformat__($type, typestr, /)\n"
248"--\n"
249"\n"
250"You probably don\'t want to use this function.\n"
251"\n"
252" typestr\n"
253" Must be \'double\' or \'float\'.\n"
254"\n"
255"It exists mainly to be used in Python\'s test suite.\n"
256"\n"
257"This function returns whichever of \'unknown\', \'IEEE, big-endian\' or \'IEEE,\n"
258"little-endian\' best describes the format of floating point numbers used by the\n"
259"C type named by typestr.");
260
261#define FLOAT___GETFORMAT___METHODDEF \
262 {"__getformat__", (PyCFunction)float___getformat__, METH_O|METH_CLASS, float___getformat____doc__},
263
264static PyObject *
265float___getformat___impl(PyTypeObject *type, const char *typestr);
266
267static PyObject *
268float___getformat__(PyTypeObject *type, PyObject *arg)
269{
270 PyObject *return_value = NULL;
271 const char *typestr;
272
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200273 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200274 _PyArg_BadArgument("__getformat__", "argument", "str", arg);
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200275 goto exit;
276 }
277 Py_ssize_t typestr_length;
278 typestr = PyUnicode_AsUTF8AndSize(arg, &typestr_length);
279 if (typestr == NULL) {
280 goto exit;
281 }
282 if (strlen(typestr) != (size_t)typestr_length) {
283 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200284 goto exit;
285 }
286 return_value = float___getformat___impl(type, typestr);
287
288exit:
289 return return_value;
290}
291
292PyDoc_STRVAR(float___set_format____doc__,
293"__set_format__($type, typestr, fmt, /)\n"
294"--\n"
295"\n"
296"You probably don\'t want to use this function.\n"
297"\n"
298" typestr\n"
299" Must be \'double\' or \'float\'.\n"
300" fmt\n"
301" Must be one of \'unknown\', \'IEEE, big-endian\' or \'IEEE, little-endian\',\n"
302" and in addition can only be one of the latter two if it appears to\n"
303" match the underlying C reality.\n"
304"\n"
305"It exists mainly to be used in Python\'s test suite.\n"
306"\n"
307"Override the automatic determination of C-level floating point type.\n"
308"This affects how floats are converted to and from binary strings.");
309
310#define FLOAT___SET_FORMAT___METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200311 {"__set_format__", (PyCFunction)(void(*)(void))float___set_format__, METH_FASTCALL|METH_CLASS, float___set_format____doc__},
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200312
313static PyObject *
314float___set_format___impl(PyTypeObject *type, const char *typestr,
315 const char *fmt);
316
317static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200318float___set_format__(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200319{
320 PyObject *return_value = NULL;
321 const char *typestr;
322 const char *fmt;
323
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200324 if (!_PyArg_CheckPositional("__set_format__", nargs, 2, 2)) {
325 goto exit;
326 }
327 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200328 _PyArg_BadArgument("__set_format__", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200329 goto exit;
330 }
331 Py_ssize_t typestr_length;
332 typestr = PyUnicode_AsUTF8AndSize(args[0], &typestr_length);
333 if (typestr == NULL) {
334 goto exit;
335 }
336 if (strlen(typestr) != (size_t)typestr_length) {
337 PyErr_SetString(PyExc_ValueError, "embedded null character");
338 goto exit;
339 }
340 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200341 _PyArg_BadArgument("__set_format__", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200342 goto exit;
343 }
344 Py_ssize_t fmt_length;
345 fmt = PyUnicode_AsUTF8AndSize(args[1], &fmt_length);
346 if (fmt == NULL) {
347 goto exit;
348 }
349 if (strlen(fmt) != (size_t)fmt_length) {
350 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200351 goto exit;
352 }
353 return_value = float___set_format___impl(type, typestr, fmt);
354
355exit:
356 return return_value;
357}
358
359PyDoc_STRVAR(float___format____doc__,
360"__format__($self, format_spec, /)\n"
361"--\n"
362"\n"
363"Formats the float according to format_spec.");
364
365#define FLOAT___FORMAT___METHODDEF \
366 {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
367
368static PyObject *
369float___format___impl(PyObject *self, PyObject *format_spec);
370
371static PyObject *
372float___format__(PyObject *self, PyObject *arg)
373{
374 PyObject *return_value = NULL;
375 PyObject *format_spec;
376
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200377 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200378 _PyArg_BadArgument("__format__", "argument", "str", arg);
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200379 goto exit;
380 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200381 if (PyUnicode_READY(arg) == -1) {
382 goto exit;
383 }
384 format_spec = arg;
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200385 return_value = float___format___impl(self, format_spec);
386
387exit:
388 return return_value;
389}
Batuhan Taşkayacb8b9462019-12-16 01:00:28 +0300390/*[clinic end generated code: output=25fbbe253f44e2df input=a9049054013a1b77]*/