Coverage Report

Created: 2026-10-17 06:15

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