floatobject.c.h 8.2 KB

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