Branch data Line data Source code
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 : 0 : float_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored))
25 : : {
26 : 0 : 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 : 0 : float___trunc__(PyObject *self, PyObject *Py_UNUSED(ignored))
43 : : {
44 : 0 : 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 : 0 : float___floor__(PyObject *self, PyObject *Py_UNUSED(ignored))
61 : : {
62 : 0 : 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 : 0 : float___ceil__(PyObject *self, PyObject *Py_UNUSED(ignored))
79 : : {
80 : 0 : 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 : 18 : float___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
99 : : {
100 : 18 : PyObject *return_value = NULL;
101 : 18 : PyObject *o_ndigits = Py_None;
102 : :
103 [ + - - + : 18 : if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) {
- - ]
104 : 0 : goto exit;
105 : : }
106 [ + + ]: 18 : if (nargs < 1) {
107 : 12 : goto skip_optional;
108 : : }
109 : 6 : o_ndigits = args[0];
110 : 18 : skip_optional:
111 : 18 : return_value = float___round___impl(self, o_ndigits);
112 : :
113 : 18 : exit:
114 : 18 : 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 : 0 : float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
131 : : {
132 : 0 : 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 : 96 : float_hex(PyObject *self, PyObject *Py_UNUSED(ignored))
154 : : {
155 : 96 : 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 : 29615 : float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
196 : : {
197 : 29615 : 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 : 3 : float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
211 : : {
212 : 3 : PyObject *return_value = NULL;
213 : 3 : PyTypeObject *base_tp = &PyFloat_Type;
214 : 3 : PyObject *x = NULL;
215 : :
216 [ + - + - : 3 : if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
- + ]
217 [ # # ]: 0 : !_PyArg_NoKeywords("float", kwargs)) {
218 : 0 : goto exit;
219 : : }
220 [ + - - + : 3 : if (!_PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1)) {
- - ]
221 : 0 : goto exit;
222 : : }
223 [ + - ]: 3 : if (PyTuple_GET_SIZE(args) < 1) {
224 : 3 : goto skip_optional;
225 : : }
226 : 0 : x = PyTuple_GET_ITEM(args, 0);
227 : 3 : skip_optional:
228 : 3 : return_value = float_new_impl(type, x);
229 : :
230 : 3 : exit:
231 : 3 : 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 : 0 : float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
247 : : {
248 : 0 : 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 : 1 : float___getformat__(PyTypeObject *type, PyObject *arg)
274 : : {
275 : 1 : PyObject *return_value = NULL;
276 : : const char *typestr;
277 : :
278 [ - + ]: 1 : if (!PyUnicode_Check(arg)) {
279 : 0 : _PyArg_BadArgument("__getformat__", "argument", "str", arg);
280 : 0 : goto exit;
281 : : }
282 : : Py_ssize_t typestr_length;
283 : 1 : typestr = PyUnicode_AsUTF8AndSize(arg, &typestr_length);
284 [ - + ]: 1 : if (typestr == NULL) {
285 : 0 : goto exit;
286 : : }
287 [ - + ]: 1 : if (strlen(typestr) != (size_t)typestr_length) {
288 : 0 : PyErr_SetString(PyExc_ValueError, "embedded null character");
289 : 0 : goto exit;
290 : : }
291 : 1 : return_value = float___getformat___impl(type, typestr);
292 : :
293 : 1 : exit:
294 : 1 : 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 : 2 : float___format__(PyObject *self, PyObject *arg)
311 : : {
312 : 2 : PyObject *return_value = NULL;
313 : : PyObject *format_spec;
314 : :
315 [ - + ]: 2 : if (!PyUnicode_Check(arg)) {
316 : 0 : _PyArg_BadArgument("__format__", "argument", "str", arg);
317 : 0 : goto exit;
318 : : }
319 [ - + ]: 2 : if (PyUnicode_READY(arg) == -1) {
320 : 0 : goto exit;
321 : : }
322 : 2 : format_spec = arg;
323 : 2 : return_value = float___format___impl(self, format_spec);
324 : :
325 : 2 : exit:
326 : 2 : return return_value;
327 : : }
328 : : /*[clinic end generated code: output=ea329577074911b9 input=a9049054013a1b77]*/
|