blob: 741ca3bac5b5ef6780c54eb7c9c70a8caa2901ed [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
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200231 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200232 _PyArg_BadArgument("__getformat__", 0, "str", arg);
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200233 goto exit;
234 }
235 Py_ssize_t typestr_length;
236 typestr = PyUnicode_AsUTF8AndSize(arg, &typestr_length);
237 if (typestr == NULL) {
238 goto exit;
239 }
240 if (strlen(typestr) != (size_t)typestr_length) {
241 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200242 goto exit;
243 }
244 return_value = float___getformat___impl(type, typestr);
245
246exit:
247 return return_value;
248}
249
250PyDoc_STRVAR(float___set_format____doc__,
251"__set_format__($type, typestr, fmt, /)\n"
252"--\n"
253"\n"
254"You probably don\'t want to use this function.\n"
255"\n"
256" typestr\n"
257" Must be \'double\' or \'float\'.\n"
258" fmt\n"
259" Must be one of \'unknown\', \'IEEE, big-endian\' or \'IEEE, little-endian\',\n"
260" and in addition can only be one of the latter two if it appears to\n"
261" match the underlying C reality.\n"
262"\n"
263"It exists mainly to be used in Python\'s test suite.\n"
264"\n"
265"Override the automatic determination of C-level floating point type.\n"
266"This affects how floats are converted to and from binary strings.");
267
268#define FLOAT___SET_FORMAT___METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200269 {"__set_format__", (PyCFunction)(void(*)(void))float___set_format__, METH_FASTCALL|METH_CLASS, float___set_format____doc__},
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200270
271static PyObject *
272float___set_format___impl(PyTypeObject *type, const char *typestr,
273 const char *fmt);
274
275static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200276float___set_format__(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200277{
278 PyObject *return_value = NULL;
279 const char *typestr;
280 const char *fmt;
281
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200282 if (!_PyArg_CheckPositional("__set_format__", nargs, 2, 2)) {
283 goto exit;
284 }
285 if (!PyUnicode_Check(args[0])) {
286 _PyArg_BadArgument("__set_format__", 1, "str", args[0]);
287 goto exit;
288 }
289 Py_ssize_t typestr_length;
290 typestr = PyUnicode_AsUTF8AndSize(args[0], &typestr_length);
291 if (typestr == NULL) {
292 goto exit;
293 }
294 if (strlen(typestr) != (size_t)typestr_length) {
295 PyErr_SetString(PyExc_ValueError, "embedded null character");
296 goto exit;
297 }
298 if (!PyUnicode_Check(args[1])) {
299 _PyArg_BadArgument("__set_format__", 2, "str", args[1]);
300 goto exit;
301 }
302 Py_ssize_t fmt_length;
303 fmt = PyUnicode_AsUTF8AndSize(args[1], &fmt_length);
304 if (fmt == NULL) {
305 goto exit;
306 }
307 if (strlen(fmt) != (size_t)fmt_length) {
308 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200309 goto exit;
310 }
311 return_value = float___set_format___impl(type, typestr, fmt);
312
313exit:
314 return return_value;
315}
316
317PyDoc_STRVAR(float___format____doc__,
318"__format__($self, format_spec, /)\n"
319"--\n"
320"\n"
321"Formats the float according to format_spec.");
322
323#define FLOAT___FORMAT___METHODDEF \
324 {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
325
326static PyObject *
327float___format___impl(PyObject *self, PyObject *format_spec);
328
329static PyObject *
330float___format__(PyObject *self, PyObject *arg)
331{
332 PyObject *return_value = NULL;
333 PyObject *format_spec;
334
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200335 if (!PyUnicode_Check(arg)) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200336 _PyArg_BadArgument("__format__", 0, "str", arg);
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200337 goto exit;
338 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200339 if (PyUnicode_READY(arg) == -1) {
340 goto exit;
341 }
342 format_spec = arg;
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200343 return_value = float___format___impl(self, format_spec);
344
345exit:
346 return return_value;
347}
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200348/*[clinic end generated code: output=2631a60701a8f7d4 input=a9049054013a1b77]*/