blob: 893629bda6da8ef66058042fd3e3099f1173f201 [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 \
50 {"__round__", (PyCFunction)float___round__, METH_FASTCALL, float___round____doc__},
51
52static PyObject *
53float___round___impl(PyObject *self, PyObject *o_ndigits);
54
55static PyObject *
56float___round__(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
57{
58 PyObject *return_value = NULL;
59 PyObject *o_ndigits = NULL;
60
Sylvain74453812017-06-10 06:51:48 +020061 if (!_PyArg_NoStackKeywords("__round__", kwnames)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020062 goto exit;
63 }
64
Sylvain74453812017-06-10 06:51:48 +020065 if (!_PyArg_UnpackStack(args, nargs, "__round__",
66 0, 1,
67 &o_ndigits)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +020068 goto exit;
69 }
70 return_value = float___round___impl(self, o_ndigits);
71
72exit:
73 return return_value;
74}
75
76PyDoc_STRVAR(float_conjugate__doc__,
77"conjugate($self, /)\n"
78"--\n"
79"\n"
80"Return self, the complex conjugate of any float.");
81
82#define FLOAT_CONJUGATE_METHODDEF \
83 {"conjugate", (PyCFunction)float_conjugate, METH_NOARGS, float_conjugate__doc__},
84
85static PyObject *
86float_conjugate_impl(PyObject *self);
87
88static PyObject *
89float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored))
90{
91 return float_conjugate_impl(self);
92}
93
94PyDoc_STRVAR(float_hex__doc__,
95"hex($self, /)\n"
96"--\n"
97"\n"
98"Return a hexadecimal representation of a floating-point number.\n"
99"\n"
100">>> (-0.1).hex()\n"
101"\'-0x1.999999999999ap-4\'\n"
102">>> 3.14159.hex()\n"
103"\'0x1.921f9f01b866ep+1\'");
104
105#define FLOAT_HEX_METHODDEF \
106 {"hex", (PyCFunction)float_hex, METH_NOARGS, float_hex__doc__},
107
108static PyObject *
109float_hex_impl(PyObject *self);
110
111static PyObject *
112float_hex(PyObject *self, PyObject *Py_UNUSED(ignored))
113{
114 return float_hex_impl(self);
115}
116
117PyDoc_STRVAR(float_fromhex__doc__,
118"fromhex($type, string, /)\n"
119"--\n"
120"\n"
121"Create a floating-point number from a hexadecimal string.\n"
122"\n"
123">>> float.fromhex(\'0x1.ffffp10\')\n"
124"2047.984375\n"
125">>> float.fromhex(\'-0x1p-1074\')\n"
126"-5e-324");
127
128#define FLOAT_FROMHEX_METHODDEF \
129 {"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, float_fromhex__doc__},
130
131PyDoc_STRVAR(float_as_integer_ratio__doc__,
132"as_integer_ratio($self, /)\n"
133"--\n"
134"\n"
135"Return integer ratio.\n"
136"\n"
137"Return a pair of integers, whose ratio is exactly equal to the original float\n"
138"and with a positive denominator.\n"
139"\n"
140"Raise OverflowError on infinities and a ValueError on NaNs.\n"
141"\n"
142">>> (10.0).as_integer_ratio()\n"
143"(10, 1)\n"
144">>> (0.0).as_integer_ratio()\n"
145"(0, 1)\n"
146">>> (-.25).as_integer_ratio()\n"
147"(-1, 4)");
148
149#define FLOAT_AS_INTEGER_RATIO_METHODDEF \
150 {"as_integer_ratio", (PyCFunction)float_as_integer_ratio, METH_NOARGS, float_as_integer_ratio__doc__},
151
152static PyObject *
153float_as_integer_ratio_impl(PyObject *self);
154
155static PyObject *
156float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored))
157{
158 return float_as_integer_ratio_impl(self);
159}
160
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200161PyDoc_STRVAR(float_new__doc__,
162"float(x=0, /)\n"
163"--\n"
164"\n"
165"Convert a string or number to a floating point number, if possible.");
166
167static PyObject *
168float_new_impl(PyTypeObject *type, PyObject *x);
169
170static PyObject *
171float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
172{
173 PyObject *return_value = NULL;
Serhiy Storchakabae68812017-04-05 12:00:42 +0300174 PyObject *x = _PyLong_Zero;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200175
176 if ((type == &PyFloat_Type) &&
177 !_PyArg_NoKeywords("float", kwargs)) {
178 goto exit;
179 }
180 if (!PyArg_UnpackTuple(args, "float",
181 0, 1,
182 &x)) {
183 goto exit;
184 }
185 return_value = float_new_impl(type, x);
186
187exit:
188 return return_value;
189}
190
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200191PyDoc_STRVAR(float___getnewargs____doc__,
192"__getnewargs__($self, /)\n"
193"--\n"
194"\n");
195
196#define FLOAT___GETNEWARGS___METHODDEF \
197 {"__getnewargs__", (PyCFunction)float___getnewargs__, METH_NOARGS, float___getnewargs____doc__},
198
199static PyObject *
200float___getnewargs___impl(PyObject *self);
201
202static PyObject *
203float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored))
204{
205 return float___getnewargs___impl(self);
206}
207
208PyDoc_STRVAR(float___getformat____doc__,
209"__getformat__($type, typestr, /)\n"
210"--\n"
211"\n"
212"You probably don\'t want to use this function.\n"
213"\n"
214" typestr\n"
215" Must be \'double\' or \'float\'.\n"
216"\n"
217"It exists mainly to be used in Python\'s test suite.\n"
218"\n"
219"This function returns whichever of \'unknown\', \'IEEE, big-endian\' or \'IEEE,\n"
220"little-endian\' best describes the format of floating point numbers used by the\n"
221"C type named by typestr.");
222
223#define FLOAT___GETFORMAT___METHODDEF \
224 {"__getformat__", (PyCFunction)float___getformat__, METH_O|METH_CLASS, float___getformat____doc__},
225
226static PyObject *
227float___getformat___impl(PyTypeObject *type, const char *typestr);
228
229static PyObject *
230float___getformat__(PyTypeObject *type, PyObject *arg)
231{
232 PyObject *return_value = NULL;
233 const char *typestr;
234
235 if (!PyArg_Parse(arg, "s:__getformat__", &typestr)) {
236 goto exit;
237 }
238 return_value = float___getformat___impl(type, typestr);
239
240exit:
241 return return_value;
242}
243
244PyDoc_STRVAR(float___set_format____doc__,
245"__set_format__($type, typestr, fmt, /)\n"
246"--\n"
247"\n"
248"You probably don\'t want to use this function.\n"
249"\n"
250" typestr\n"
251" Must be \'double\' or \'float\'.\n"
252" fmt\n"
253" Must be one of \'unknown\', \'IEEE, big-endian\' or \'IEEE, little-endian\',\n"
254" and in addition can only be one of the latter two if it appears to\n"
255" match the underlying C reality.\n"
256"\n"
257"It exists mainly to be used in Python\'s test suite.\n"
258"\n"
259"Override the automatic determination of C-level floating point type.\n"
260"This affects how floats are converted to and from binary strings.");
261
262#define FLOAT___SET_FORMAT___METHODDEF \
263 {"__set_format__", (PyCFunction)float___set_format__, METH_FASTCALL|METH_CLASS, float___set_format____doc__},
264
265static PyObject *
266float___set_format___impl(PyTypeObject *type, const char *typestr,
267 const char *fmt);
268
269static PyObject *
270float___set_format__(PyTypeObject *type, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
271{
272 PyObject *return_value = NULL;
273 const char *typestr;
274 const char *fmt;
275
Sylvain74453812017-06-10 06:51:48 +0200276 if (!_PyArg_NoStackKeywords("__set_format__", kwnames)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200277 goto exit;
278 }
279
Sylvain74453812017-06-10 06:51:48 +0200280 if (!_PyArg_ParseStack(args, nargs, "ss:__set_format__",
281 &typestr, &fmt)) {
Serhiy Storchakab5c51d32017-03-11 09:21:05 +0200282 goto exit;
283 }
284 return_value = float___set_format___impl(type, typestr, fmt);
285
286exit:
287 return return_value;
288}
289
290PyDoc_STRVAR(float___format____doc__,
291"__format__($self, format_spec, /)\n"
292"--\n"
293"\n"
294"Formats the float according to format_spec.");
295
296#define FLOAT___FORMAT___METHODDEF \
297 {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__},
298
299static PyObject *
300float___format___impl(PyObject *self, PyObject *format_spec);
301
302static PyObject *
303float___format__(PyObject *self, PyObject *arg)
304{
305 PyObject *return_value = NULL;
306 PyObject *format_spec;
307
308 if (!PyArg_Parse(arg, "U:__format__", &format_spec)) {
309 goto exit;
310 }
311 return_value = float___format___impl(self, format_spec);
312
313exit:
314 return return_value;
315}
Sylvain74453812017-06-10 06:51:48 +0200316/*[clinic end generated code: output=b2271e7413b36162 input=a9049054013a1b77]*/