浮點數(Floating-Point)物件¶
-
PyTypeObject PyFloat_Type¶
- 為 穩定 ABI 的一部分.
這個
PyTypeObject的實例代表 Python 浮點數型別。這與 Python 層中的float物件相同。
-
int PyFloat_Check(PyObject *p)¶
如果其引數是
PyFloatObject或PyFloatObject的子型別,則回傳 true。這個函式不會失敗。
-
int PyFloat_CheckExact(PyObject *p)¶
如果其引數是
PyFloatObject,但不是PyFloatObject的子型別,則回傳 true。這個函式不會失敗。
-
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.h的簡單封裝。
-
double PyFloat_GetMax()¶
- 為 穩定 ABI 的一部分.
將可表示的最大有限浮點數 DBL_MAX 作為 C double 回傳。
-
double PyFloat_GetMin()¶
- 為 穩定 ABI 的一部分.
將最小的正規化浮點數 DBL_MIN 作為 C double 回傳。
-
Py_INFINITY¶
這個巨集會展開為一型別為 double 的常數運算式,表示正無窮大。
It is equivalent to the
INFINITYmacro from the C11 standard<math.h>header.在 3.15 版之後被棄用: 這個巨集已被軟性棄用。
-
Py_NAN¶
這個巨集會展開為一型別為 double 的常數運算式,表示安靜型 NaN (qNaN) 值。
在大多數平台上,這相當於 C11 標準
<math.h>標頭檔中的NAN巨集。
-
Py_RETURN_INF(sign)¶
根據 sign 的正負號,從函式回傳
math.inf或-math.inf。在大多數平台上,這相當於以下內容:
return PyFloat_FromDouble(copysign(INFINITY, sign));
-
Py_IS_FINITE(X)¶
如果給定的浮點數 X 是有限的(即為正規數 (normal)、次正規數 (subnormal) 或零,但不是無窮大或 NaN),則回傳
1。否則回傳0。在 3.14 版之後被棄用: 此巨集已被軟性棄用。請改用
isfinite。
打包和解包函式¶
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.
在 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() 為 true(並且會設置一個例外,最有可能是 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。