1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9
10
11 PyDoc_STRVAR(float_is_integer__doc__,
12 "is_integer($self, /)\n"
13 "--\n"
14 "\n"
15 "Return True if the float is an integer.");
16
17 #define FLOAT_IS_INTEGER_METHODDEF \
18 {"is_integer", (PyCFunction)float_is_integer, METH_NOARGS, float_is_integer__doc__},
19
20 static PyObject *
21 float_is_integer_impl(PyObject *self);
22
23 static PyObject *
24 float_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
25 {
26 return float_is_integer_impl(self);
27 }
28
29 PyDoc_STRVAR(float___trunc____doc__,
30 "__trunc__($self, /)\n"
31 "--\n"
32 "\n"
33 "Return the Integral closest to x between 0 and x.");
34
35 #define FLOAT___TRUNC___METHODDEF \
36 {"__trunc__", (PyCFunction)float___trunc__, METH_NOARGS, float___trunc____doc__},
37
38 static PyObject *
39 float___trunc___impl(PyObject *self);
40
41 static PyObject *
42 float___trunc__(PyObject *self, PyObject *Py_UNUSED(ignored))
43 {
44 return float___trunc___impl(self);
45 }
46
47 PyDoc_STRVAR(float___floor____doc__,
48 "__floor__($self, /)\n"
49 "--\n"
50 "\n"
51 "Return the floor as an Integral.");
52
53 #define FLOAT___FLOOR___METHODDEF \
54 {"__floor__", (PyCFunction)float___floor__, METH_NOARGS, float___floor____doc__},
55
56 static PyObject *
57 float___floor___impl(PyObject *self);
58
59 static PyObject *
60 float___floor__(PyObject *self, PyObject *Py_UNUSED(ignored))
61 {
62 return float___floor___impl(self);
63 }
64
65 PyDoc_STRVAR(float___ceil____doc__,
66 "__ceil__($self, /)\n"
67 "--\n"
68 "\n"
69 "Return the ceiling as an Integral.");
70
71 #define FLOAT___CEIL___METHODDEF \
72 {"__ceil__", (PyCFunction)float___ceil__, METH_NOARGS, float___ceil____doc__},
73
74 static PyObject *
75 float___ceil___impl(PyObject *self);
76
77 static PyObject *
78 float___ceil__(PyObject *self, PyObject *Py_UNUSED(ignored))
79 {
80 return float___ceil___impl(self);
81 }
82
83 PyDoc_STRVAR(float___round____doc__,
84 "__round__($self, ndigits=None, /)\n"
85 "--\n"
86 "\n"
87 "Return the Integral closest to x, rounding half toward even.\n"
88 "\n"
89 "When an argument is passed, work like built-in round(x, ndigits).");
90
91 #define FLOAT___ROUND___METHODDEF \
92 {"__round__", _PyCFunction_CAST(float___round__), METH_FASTCALL, float___round____doc__},
93
94 static PyObject *
95 float___round___impl(PyObject *self, PyObject *o_ndigits);
96
97 static PyObject *
98 float___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
99 {
100 PyObject *return_value = NULL;
101 PyObject *o_ndigits = Py_None;
102
103 if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) {
104 goto exit;
105 }
106 if (nargs < 1) {
107 goto skip_optional;
108 }
109 o_ndigits = args[0];
110 skip_optional:
111 return_value = float___round___impl(self, o_ndigits);
112
113 exit:
114 return return_value;
115 }
116
117 PyDoc_STRVAR(float_conjugate__doc__,
118 "conjugate($self, /)\n"
119 "--\n"
120 "\n"
121 "Return self, the complex conjugate of any float.");
122
123 #define FLOAT_CONJUGATE_METHODDEF \
124 {"conjugate", (PyCFunction)float_conjugate, METH_NOARGS, float_conjugate__doc__},
125
126 static PyObject *
127 float_conjugate_impl(PyObject *self);
128
129 static PyObject *
130 float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
131 {
132 return float_conjugate_impl(self);
133 }
134
135 PyDoc_STRVAR(float_hex__doc__,
136 "hex($self, /)\n"
137 "--\n"
138 "\n"
139 "Return a hexadecimal representation of a floating-point number.\n"
140 "\n"
141 ">>> (-0.1).hex()\n"
142 "\'-0x1.999999999999ap-4\'\n"
143 ">>> 3.14159.hex()\n"
144 "\'0x1.921f9f01b866ep+1\'");
145
146 #define FLOAT_HEX_METHODDEF \
147 {"hex", (PyCFunction)float_hex, METH_NOARGS, float_hex__doc__},
148
149 static PyObject *
150 float_hex_impl(PyObject *self);
151
152 static PyObject *
153 float_hex(PyObject *self, PyObject *Py_UNUSED(ignored))
154 {
155 return float_hex_impl(self);
156 }
157
158 PyDoc_STRVAR(float_fromhex__doc__,
159 "fromhex($type, string, /)\n"
160 "--\n"
161 "\n"
162 "Create a floating-point number from a hexadecimal string.\n"
163 "\n"
164 ">>> float.fromhex(\'0x1.ffffp10\')\n"
165 "2047.984375\n"
166 ">>> float.fromhex(\'-0x1p-1074\')\n"
167 "-5e-324");
168
169 #define FLOAT_FROMHEX_METHODDEF \
170 {"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, float_fromhex__doc__},
171
172 PyDoc_STRVAR(float_as_integer_ratio__doc__,
173 "as_integer_ratio($self, /)\n"
174 "--\n"
175 "\n"
176 "Return a pair of integers, whose ratio is exactly equal to the original float.\n"
177 "\n"
178 "The ratio is in lowest terms and has a positive denominator. Raise\n"
179 "OverflowError on infinities and a ValueError on NaNs.\n"
180 "\n"
181 ">>> (10.0).as_integer_ratio()\n"
182 "(10, 1)\n"
183 ">>> (0.0).as_integer_ratio()\n"
184 "(0, 1)\n"
185 ">>> (-.25).as_integer_ratio()\n"
186 "(-1, 4)");
187
188 #define FLOAT_AS_INTEGER_RATIO_METHODDEF \
189 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, float_as_integer_ratio__doc__},
190
191 static PyObject *
192 float_as_integer_ratio_impl(PyObject *self);
193
194 static PyObject *
195 float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
196 {
197 return float_as_integer_ratio_impl(self);
198 }
199
200 PyDoc_STRVAR(float_new__doc__,
201 "float(x=0, /)\n"
202 "--\n"
203 "\n"
204 "Convert a string or number to a floating point number, if possible.");
205
206 static PyObject *
207 float_new_impl(PyTypeObject *type, PyObject *x);
208
209 static PyObject *
210 float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
211 {
212 PyObject *return_value = NULL;
213 PyTypeObject *base_tp = &PyFloat_Type;
214 PyObject *x = NULL;
215
216 if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
217 !_PyArg_NoKeywords("float", kwargs)) {
218 goto exit;
219 }
220 if (!_PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1)) {
221 goto exit;
222 }
223 if (PyTuple_GET_SIZE(args) < 1) {
224 goto skip_optional;
225 }
226 x = PyTuple_GET_ITEM(args, 0);
227 skip_optional:
228 return_value = float_new_impl(type, x);
229
230 exit:
231 return return_value;
232 }
233
234 PyDoc_STRVAR(float___getnewargs____doc__,
235 "__getnewargs__($self, /)\n"
236 "--\n"
237 "\n");
238
239 #define FLOAT___GETNEWARGS___METHODDEF \
240 {"__getnewargs__", (PyCFunction)float___getnewargs__, METH_NOARGS, float___getnewargs____doc__},
241
242 static PyObject *
243 float___getnewargs___impl(PyObject *self);
244
245 static PyObject *
246 float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
247 {
248 return float___getnewargs___impl(self);
249 }
250
251 PyDoc_STRVAR(float___getformat____doc__,
252 "__getformat__($type, typestr, /)\n"
253 "--\n"
254 "\n"
255 "You probably don\'t want to use this function.\n"
256 "\n"
257 " typestr\n"
258 " Must be \'double\' or \'float\'.\n"
259 "\n"
260 "It exists mainly to be used in Python\'s test suite.\n"
261 "\n"
262 "This function returns whichever of \'unknown\', \'IEEE, big-endian\' or \'IEEE,\n"
263 "little-endian\' best describes the format of floating point numbers used by the\n"
264 "C type named by typestr.");
265
266 #define FLOAT___GETFORMAT___METHODDEF \
267 {"__getformat__", (PyCFunction)float___getformat__, METH_O|METH_CLASS, float___getformat____doc__},
268
269 static PyObject *
270 float___getformat___impl(PyTypeObject *type, const char *typestr);
271
272 static PyObject *
273 float___getformat__(PyTypeObject *type, PyObject *arg)
274 {
275 PyObject *return_value = NULL;
276 const char *typestr;
277
278 if (!PyUnicode_Check(arg)) {
279 _PyArg_BadArgument("__getformat__", "argument", "str", arg);
280 goto exit;
281 }
282 Py_ssize_t typestr_length;
283 typestr = PyUnicode_AsUTF8AndSize(arg, &typestr_length);
284 if (typestr == NULL) {
285 goto exit;
286 }
287 if (strlen(typestr) != (size_t)typestr_length) {
288 PyErr_SetString(PyExc_ValueError, "embedded null character");
289 goto exit;
290 }
291 return_value = float___getformat___impl(type, typestr);
292
293 exit:
294 return return_value;
295 }
296
297 PyDoc_STRVAR(float___format____doc__,
298 "__format__($self, format_spec, /)\n"
299 "--\n"
300 "\n"
301 "Formats the float according to format_spec.");
302
303 #define FLOAT___FORMAT___METHODDEF \
304 {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
305
306 static PyObject *
307 float___format___impl(PyObject *self, PyObject *format_spec);
308
309 static PyObject *
310 float___format__(PyObject *self, PyObject *arg)
311 {
312 PyObject *return_value = NULL;
313 PyObject *format_spec;
314
315 if (!PyUnicode_Check(arg)) {
316 _PyArg_BadArgument("__format__", "argument", "str", arg);
317 goto exit;
318 }
319 if (PyUnicode_READY(arg) == -1) {
320 goto exit;
321 }
322 format_spec = arg;
323 return_value = float___format___impl(self, format_spec);
324
325 exit:
326 return return_value;
327 }
328 /*[clinic end generated code: output=ea329577074911b9 input=a9049054013a1b77]*/