Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_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 | |
| 14 | static PyObject * |
| 15 | float_is_integer_impl(PyObject *self); |
| 16 | |
| 17 | static PyObject * |
| 18 | float_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 19 | { |
| 20 | return float_is_integer_impl(self); |
| 21 | } |
| 22 | |
| 23 | PyDoc_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 | |
| 32 | static PyObject * |
| 33 | float___trunc___impl(PyObject *self); |
| 34 | |
| 35 | static PyObject * |
| 36 | float___trunc__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 37 | { |
| 38 | return float___trunc___impl(self); |
| 39 | } |
| 40 | |
Batuhan Taşkaya | cb8b946 | 2019-12-16 01:00:28 +0300 | [diff] [blame] | 41 | PyDoc_STRVAR(float___floor____doc__, |
| 42 | "__floor__($self, /)\n" |
| 43 | "--\n" |
| 44 | "\n" |
| 45 | "Return the floor as an Integral."); |
| 46 | |
| 47 | #define FLOAT___FLOOR___METHODDEF \ |
| 48 | {"__floor__", (PyCFunction)float___floor__, METH_NOARGS, float___floor____doc__}, |
| 49 | |
| 50 | static PyObject * |
| 51 | float___floor___impl(PyObject *self); |
| 52 | |
| 53 | static PyObject * |
| 54 | float___floor__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 55 | { |
| 56 | return float___floor___impl(self); |
| 57 | } |
| 58 | |
| 59 | PyDoc_STRVAR(float___ceil____doc__, |
| 60 | "__ceil__($self, /)\n" |
| 61 | "--\n" |
| 62 | "\n" |
| 63 | "Return the ceiling as an Integral."); |
| 64 | |
| 65 | #define FLOAT___CEIL___METHODDEF \ |
| 66 | {"__ceil__", (PyCFunction)float___ceil__, METH_NOARGS, float___ceil____doc__}, |
| 67 | |
| 68 | static PyObject * |
| 69 | float___ceil___impl(PyObject *self); |
| 70 | |
| 71 | static PyObject * |
| 72 | float___ceil__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 73 | { |
| 74 | return float___ceil___impl(self); |
| 75 | } |
| 76 | |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 77 | PyDoc_STRVAR(float___round____doc__, |
| 78 | "__round__($self, ndigits=None, /)\n" |
| 79 | "--\n" |
| 80 | "\n" |
| 81 | "Return the Integral closest to x, rounding half toward even.\n" |
| 82 | "\n" |
| 83 | "When an argument is passed, work like built-in round(x, ndigits)."); |
| 84 | |
| 85 | #define FLOAT___ROUND___METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 86 | {"__round__", (PyCFunction)(void(*)(void))float___round__, METH_FASTCALL, float___round____doc__}, |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 87 | |
| 88 | static PyObject * |
| 89 | float___round___impl(PyObject *self, PyObject *o_ndigits); |
| 90 | |
| 91 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 92 | float___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 93 | { |
| 94 | PyObject *return_value = NULL; |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 95 | PyObject *o_ndigits = Py_None; |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 96 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 97 | if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) { |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 98 | goto exit; |
| 99 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 100 | if (nargs < 1) { |
| 101 | goto skip_optional; |
| 102 | } |
| 103 | o_ndigits = args[0]; |
| 104 | skip_optional: |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 105 | return_value = float___round___impl(self, o_ndigits); |
| 106 | |
| 107 | exit: |
| 108 | return return_value; |
| 109 | } |
| 110 | |
| 111 | PyDoc_STRVAR(float_conjugate__doc__, |
| 112 | "conjugate($self, /)\n" |
| 113 | "--\n" |
| 114 | "\n" |
| 115 | "Return self, the complex conjugate of any float."); |
| 116 | |
| 117 | #define FLOAT_CONJUGATE_METHODDEF \ |
| 118 | {"conjugate", (PyCFunction)float_conjugate, METH_NOARGS, float_conjugate__doc__}, |
| 119 | |
| 120 | static PyObject * |
| 121 | float_conjugate_impl(PyObject *self); |
| 122 | |
| 123 | static PyObject * |
| 124 | float_conjugate(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 125 | { |
| 126 | return float_conjugate_impl(self); |
| 127 | } |
| 128 | |
| 129 | PyDoc_STRVAR(float_hex__doc__, |
| 130 | "hex($self, /)\n" |
| 131 | "--\n" |
| 132 | "\n" |
| 133 | "Return a hexadecimal representation of a floating-point number.\n" |
| 134 | "\n" |
| 135 | ">>> (-0.1).hex()\n" |
| 136 | "\'-0x1.999999999999ap-4\'\n" |
| 137 | ">>> 3.14159.hex()\n" |
| 138 | "\'0x1.921f9f01b866ep+1\'"); |
| 139 | |
| 140 | #define FLOAT_HEX_METHODDEF \ |
| 141 | {"hex", (PyCFunction)float_hex, METH_NOARGS, float_hex__doc__}, |
| 142 | |
| 143 | static PyObject * |
| 144 | float_hex_impl(PyObject *self); |
| 145 | |
| 146 | static PyObject * |
| 147 | float_hex(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 148 | { |
| 149 | return float_hex_impl(self); |
| 150 | } |
| 151 | |
| 152 | PyDoc_STRVAR(float_fromhex__doc__, |
| 153 | "fromhex($type, string, /)\n" |
| 154 | "--\n" |
| 155 | "\n" |
| 156 | "Create a floating-point number from a hexadecimal string.\n" |
| 157 | "\n" |
| 158 | ">>> float.fromhex(\'0x1.ffffp10\')\n" |
| 159 | "2047.984375\n" |
| 160 | ">>> float.fromhex(\'-0x1p-1074\')\n" |
| 161 | "-5e-324"); |
| 162 | |
| 163 | #define FLOAT_FROMHEX_METHODDEF \ |
| 164 | {"fromhex", (PyCFunction)float_fromhex, METH_O|METH_CLASS, float_fromhex__doc__}, |
| 165 | |
| 166 | PyDoc_STRVAR(float_as_integer_ratio__doc__, |
| 167 | "as_integer_ratio($self, /)\n" |
| 168 | "--\n" |
| 169 | "\n" |
| 170 | "Return integer ratio.\n" |
| 171 | "\n" |
| 172 | "Return a pair of integers, whose ratio is exactly equal to the original float\n" |
| 173 | "and with a positive denominator.\n" |
| 174 | "\n" |
| 175 | "Raise 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 * |
| 191 | float_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 192 | { |
| 193 | return float_as_integer_ratio_impl(self); |
| 194 | } |
| 195 | |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 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 * |
| 206 | float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 207 | { |
| 208 | PyObject *return_value = NULL; |
Serhiy Storchaka | bae6881 | 2017-04-05 12:00:42 +0300 | [diff] [blame] | 209 | PyObject *x = _PyLong_Zero; |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 210 | |
| 211 | if ((type == &PyFloat_Type) && |
| 212 | !_PyArg_NoKeywords("float", kwargs)) { |
| 213 | goto exit; |
| 214 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 215 | if (!_PyArg_CheckPositional("float", PyTuple_GET_SIZE(args), 0, 1)) { |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 216 | goto exit; |
| 217 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 218 | if (PyTuple_GET_SIZE(args) < 1) { |
| 219 | goto skip_optional; |
| 220 | } |
| 221 | x = PyTuple_GET_ITEM(args, 0); |
| 222 | skip_optional: |
Serhiy Storchaka | 18b250f | 2017-03-19 08:51:07 +0200 | [diff] [blame] | 223 | return_value = float_new_impl(type, x); |
| 224 | |
| 225 | exit: |
| 226 | return return_value; |
| 227 | } |
| 228 | |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 229 | PyDoc_STRVAR(float___getnewargs____doc__, |
| 230 | "__getnewargs__($self, /)\n" |
| 231 | "--\n" |
| 232 | "\n"); |
| 233 | |
| 234 | #define FLOAT___GETNEWARGS___METHODDEF \ |
| 235 | {"__getnewargs__", (PyCFunction)float___getnewargs__, METH_NOARGS, float___getnewargs____doc__}, |
| 236 | |
| 237 | static PyObject * |
| 238 | float___getnewargs___impl(PyObject *self); |
| 239 | |
| 240 | static PyObject * |
| 241 | float___getnewargs__(PyObject *self, PyObject *Py_UNUSED(ignored)) |
| 242 | { |
| 243 | return float___getnewargs___impl(self); |
| 244 | } |
| 245 | |
| 246 | PyDoc_STRVAR(float___getformat____doc__, |
| 247 | "__getformat__($type, typestr, /)\n" |
| 248 | "--\n" |
| 249 | "\n" |
| 250 | "You probably don\'t want to use this function.\n" |
| 251 | "\n" |
| 252 | " typestr\n" |
| 253 | " Must be \'double\' or \'float\'.\n" |
| 254 | "\n" |
| 255 | "It exists mainly to be used in Python\'s test suite.\n" |
| 256 | "\n" |
| 257 | "This function returns whichever of \'unknown\', \'IEEE, big-endian\' or \'IEEE,\n" |
| 258 | "little-endian\' best describes the format of floating point numbers used by the\n" |
| 259 | "C type named by typestr."); |
| 260 | |
| 261 | #define FLOAT___GETFORMAT___METHODDEF \ |
| 262 | {"__getformat__", (PyCFunction)float___getformat__, METH_O|METH_CLASS, float___getformat____doc__}, |
| 263 | |
| 264 | static PyObject * |
| 265 | float___getformat___impl(PyTypeObject *type, const char *typestr); |
| 266 | |
| 267 | static PyObject * |
| 268 | float___getformat__(PyTypeObject *type, PyObject *arg) |
| 269 | { |
| 270 | PyObject *return_value = NULL; |
| 271 | const char *typestr; |
| 272 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 273 | if (!PyUnicode_Check(arg)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 274 | _PyArg_BadArgument("__getformat__", "argument", "str", arg); |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 275 | goto exit; |
| 276 | } |
| 277 | Py_ssize_t typestr_length; |
| 278 | typestr = PyUnicode_AsUTF8AndSize(arg, &typestr_length); |
| 279 | if (typestr == NULL) { |
| 280 | goto exit; |
| 281 | } |
| 282 | if (strlen(typestr) != (size_t)typestr_length) { |
| 283 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 284 | goto exit; |
| 285 | } |
| 286 | return_value = float___getformat___impl(type, typestr); |
| 287 | |
| 288 | exit: |
| 289 | return return_value; |
| 290 | } |
| 291 | |
| 292 | PyDoc_STRVAR(float___set_format____doc__, |
| 293 | "__set_format__($type, typestr, fmt, /)\n" |
| 294 | "--\n" |
| 295 | "\n" |
| 296 | "You probably don\'t want to use this function.\n" |
| 297 | "\n" |
| 298 | " typestr\n" |
| 299 | " Must be \'double\' or \'float\'.\n" |
| 300 | " fmt\n" |
| 301 | " Must be one of \'unknown\', \'IEEE, big-endian\' or \'IEEE, little-endian\',\n" |
| 302 | " and in addition can only be one of the latter two if it appears to\n" |
| 303 | " match the underlying C reality.\n" |
| 304 | "\n" |
| 305 | "It exists mainly to be used in Python\'s test suite.\n" |
| 306 | "\n" |
| 307 | "Override the automatic determination of C-level floating point type.\n" |
| 308 | "This affects how floats are converted to and from binary strings."); |
| 309 | |
| 310 | #define FLOAT___SET_FORMAT___METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 311 | {"__set_format__", (PyCFunction)(void(*)(void))float___set_format__, METH_FASTCALL|METH_CLASS, float___set_format____doc__}, |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 312 | |
| 313 | static PyObject * |
| 314 | float___set_format___impl(PyTypeObject *type, const char *typestr, |
| 315 | const char *fmt); |
| 316 | |
| 317 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 318 | float___set_format__(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 319 | { |
| 320 | PyObject *return_value = NULL; |
| 321 | const char *typestr; |
| 322 | const char *fmt; |
| 323 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 324 | if (!_PyArg_CheckPositional("__set_format__", nargs, 2, 2)) { |
| 325 | goto exit; |
| 326 | } |
| 327 | if (!PyUnicode_Check(args[0])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 328 | _PyArg_BadArgument("__set_format__", "argument 1", "str", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 329 | goto exit; |
| 330 | } |
| 331 | Py_ssize_t typestr_length; |
| 332 | typestr = PyUnicode_AsUTF8AndSize(args[0], &typestr_length); |
| 333 | if (typestr == NULL) { |
| 334 | goto exit; |
| 335 | } |
| 336 | if (strlen(typestr) != (size_t)typestr_length) { |
| 337 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 338 | goto exit; |
| 339 | } |
| 340 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 341 | _PyArg_BadArgument("__set_format__", "argument 2", "str", args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 342 | goto exit; |
| 343 | } |
| 344 | Py_ssize_t fmt_length; |
| 345 | fmt = PyUnicode_AsUTF8AndSize(args[1], &fmt_length); |
| 346 | if (fmt == NULL) { |
| 347 | goto exit; |
| 348 | } |
| 349 | if (strlen(fmt) != (size_t)fmt_length) { |
| 350 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 351 | goto exit; |
| 352 | } |
| 353 | return_value = float___set_format___impl(type, typestr, fmt); |
| 354 | |
| 355 | exit: |
| 356 | return return_value; |
| 357 | } |
| 358 | |
| 359 | PyDoc_STRVAR(float___format____doc__, |
| 360 | "__format__($self, format_spec, /)\n" |
| 361 | "--\n" |
| 362 | "\n" |
| 363 | "Formats the float according to format_spec."); |
| 364 | |
| 365 | #define FLOAT___FORMAT___METHODDEF \ |
| 366 | {"__format__", (PyCFunction)float___format__, METH_O, float___format____doc__}, |
| 367 | |
| 368 | static PyObject * |
| 369 | float___format___impl(PyObject *self, PyObject *format_spec); |
| 370 | |
| 371 | static PyObject * |
| 372 | float___format__(PyObject *self, PyObject *arg) |
| 373 | { |
| 374 | PyObject *return_value = NULL; |
| 375 | PyObject *format_spec; |
| 376 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 377 | if (!PyUnicode_Check(arg)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 378 | _PyArg_BadArgument("__format__", "argument", "str", arg); |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 379 | goto exit; |
| 380 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 381 | if (PyUnicode_READY(arg) == -1) { |
| 382 | goto exit; |
| 383 | } |
| 384 | format_spec = arg; |
Serhiy Storchaka | b5c51d3 | 2017-03-11 09:21:05 +0200 | [diff] [blame] | 385 | return_value = float___format___impl(self, format_spec); |
| 386 | |
| 387 | exit: |
| 388 | return return_value; |
| 389 | } |
Batuhan Taşkaya | cb8b946 | 2019-12-16 01:00:28 +0300 | [diff] [blame] | 390 | /*[clinic end generated code: output=25fbbe253f44e2df input=a9049054013a1b77]*/ |