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