blob: 28b24d0a093d2ce46ade3705c58fcacc94ef6c89 [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)) {
232 _PyArg_BadArgument("__getformat__", "str", arg);
233 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
Sylvain74453812017-06-10 06:51:48 +0200282 if (!_PyArg_ParseStack(args, nargs, "ss:__set_format__",
283 &typestr, &fmt)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200284 goto exit;
285 }
286 return_value = float___set_format___impl(type, typestr, fmt);
287
288exit:
289 return return_value;
290}
291
292PyDoc_STRVAR(float___format____doc__,
293"__format__($self, format_spec, /)\n"
294"--\n"
295"\n"
296"Formats the float according to format_spec.");
297
298#define FLOAT___FORMAT___METHODDEF \
299 {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
300
301static PyObject *
302float___format___impl(PyObject *self, PyObject *format_spec);
303
304static PyObject *
305float___format__(PyObject *self, PyObject *arg)
306{
307 PyObject *return_value = NULL;
308 PyObject *format_spec;
309
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200310 if (!PyUnicode_Check(arg)) {
311 _PyArg_BadArgument("__format__", "str", arg);
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200312 goto exit;
313 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200314 if (PyUnicode_READY(arg) == -1) {
315 goto exit;
316 }
317 format_spec = arg;
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200318 return_value = float___format___impl(self, format_spec);
319
320exit:
321 return return_value;
322}
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200323/*[clinic end generated code: output=e8f8be828462d58b input=a9049054013a1b77]*/