浮点数对象¶
-
PyTypeObject PyFloat_Type¶
- 属于 稳定 ABI.
这个
PyTypeObject实例代表 Python 浮点数类型。这与 Python 层面的float是同一个对象。
-
int PyFloat_Check(PyObject *p)¶
如果它的参数是一个
PyFloatObject或者PyFloatObject的子类型则返回真值。 此函数总是会成功执行。
-
int PyFloat_CheckExact(PyObject *p)¶
如果它的参数是一个
PyFloatObject但不是PyFloatObject的子类型则返回真值。 此函数总是会成功执行。
-
PyObject *PyFloat_FromString(PyObject *str)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据字符串 str 的值创建一个
PyFloatObject,失败时返回NULL。
-
PyObject *PyFloat_FromDouble(double v)¶
- 返回值:新的引用。 属于 稳定 ABI.
根据 v 创建一个
PyFloatObject对象,失败时返回NULL。
-
double PyFloat_AsDouble(PyObject *pyfloat)¶
- 属于 稳定 ABI.
返回 pyfloat 的内容的 C double 表示形式。如果 pyfloat 不是一个 Python 浮点数对象但是具有
__float__()方法,则会先调用此方法来将 pyfloat 转换为浮点数。如果__float__()未定义则将回退至__index__()。此方法在失败时将返回-1.0,因此开发者应当调用PyErr_Occurred()来检测错误。在 3.8 版本发生变更: 如果可能将使用
__index__()。
-
PyObject *PyFloat_GetInfo(void)¶
- 返回值:新的引用。 属于 稳定 ABI.
返回一个 structseq 实例,其中包含有关 float 的精度、最小值和最大值的信息。它是头文件
float.h的一个简单包装。
-
Py_INFINITY¶
这个宏会扩展为一个 double 类型的常量表达式,它代表正无穷大。
It is equivalent to the
INFINITYmacro from the C11 standard<math.h>header.自 3.15 版本弃用: 此宏已处于 soft deprecated 状态。
-
Py_NAN¶
这个宏会扩展为一个 double 类型的常量表达式,它代表一个 quiet not-a-number (qNaN) 值。
在大多数平台上,这等价于 C11 标准
<math.h>头文件中的NAN宏。
-
Py_HUGE_VAL¶
等价于
INFINITY。自 3.14 版本弃用: 此宏已处于 soft deprecated 状态。
-
Py_RETURN_INF(sign)¶
从一个函数返回
math.inf或-math.inf,具体取决于 sign 的符号值。在大多数平台上,这等价于以下代码:
return PyFloat_FromDouble(copysign(INFINITY, sign));
-
Py_IS_FINITE(X)¶
如果给定的浮点数 X 为有限值,即为规格数、非规格数或零,而不是无穷大或 NaN 则返回
1。否则返回0。自 3.14 版本弃用: 该宏已处于 soft deprecated 状态。请改用
isfinite。
-
Py_IS_INFINITY(X)¶
如果给定的浮点数 X 为正或负无穷大则返回
1。否则返回0。自 3.14 版本弃用: 该宏已处于 soft deprecated 状态。请改用
isinf。
-
Py_IS_NAN(X)¶
如果给定的浮点数 X 为非数字 (NaN) 值则返回
1。否则返回0。自 3.14 版本弃用: 该宏已处于 soft deprecated 状态。请改用
isnan。
打包与解包函数¶
The pack and unpack functions provide an efficient platform-independent way to store floating-point values as byte strings. The Pack routines produce a bytes string from a C double, and the Unpack routines produce a C double from such a bytes string. The suffix (2, 4 or 8) specifies the number of bytes in the bytes string:
The 2-byte format is the IEEE 754 binary16 half-precision format.
The 4-byte format is the IEEE 754 binary32 single-precision format.
The 8-byte format is the IEEE 754 binary64 double-precision format.
The NaN type may not be preserved on some platforms while unpacking (signaling NaNs become quiet NaNs), for example on x86 systems in 32-bit mode.
It's assumed that the double type has the IEEE 754 binary64 double precision format. What happens if it's not true is partly accidental (alas). On non-IEEE platforms with more precision, or larger dynamic range, than IEEE 754 supports, not all values can be packed; on non-IEEE platforms with less precision, or smaller dynamic range, not all values can be unpacked. The packing of special numbers like INFs and NaNs (if such things exist on the platform) may not be handled correctly, and attempting to unpack a bytes string containing an IEEE INF or NaN may raise an exception.
Added in version 3.11.
打包函数¶
The pack routines write 2, 4 or 8 bytes, starting at p. le is an
int argument, non-zero if you want the bytes string in little-endian
format (exponent last, at p+1, p+3, or p+6 and p+7), zero if you
want big-endian format (exponent first, at p). Use the PY_LITTLE_ENDIAN
constant to select the native endian: it is equal to 0 on big
endian processor, or 1 on little endian processor.
返回值:如果一切正常则为 0,如果出错则为 -1 (并会设置一个异常,最大可能为 OverflowError)。
-
int PyFloat_Pack2(double x, char *p, int le)¶
将 C double 打包为 IEEE 754 binary16 半精度格式。
-
int PyFloat_Pack4(double x, char *p, int le)¶
将 C double 打包为 IEEE 754 binary32 单精度格式。
-
int PyFloat_Pack8(double x, char *p, int le)¶
将 C double 打包为 IEEE 754 binary64 双精度格式。
CPython 实现细节: This function always succeeds in CPython.
解包函数¶
The unpack routines read 2, 4 or 8 bytes, starting at p. le is an
int argument, non-zero if the bytes string is in little-endian format
(exponent last, at p+1, p+3 or p+6 and p+7), zero if big-endian
(exponent first, at p). Use the PY_LITTLE_ENDIAN constant to
select the native endian: it is equal to 0 on big endian processor, or 1
on little endian processor.
返回值:解包后的 double。出错时,返回值为 -1.0 且 PyErr_Occurred() 为真值 (并且会设置一个异常,最大可能为 OverflowError)。
CPython 实现细节: These functions always succeed in CPython.
-
double PyFloat_Unpack2(const char *p, int le)¶
将 IEEE 754 binary16 半精度格式解包为 C double。
-
double PyFloat_Unpack4(const char *p, int le)¶
将 IEEE 754 binary32 单精度格式解包为 C double。
-
double PyFloat_Unpack8(const char *p, int le)¶
将 IEEE 754 binary64 双精度格式解包为 C double。