<QtNumeric> - Qt Numeric Functions
<QtNumeric> 头文件提供常用数字函数。更多
Header: | #include <QtNumeric> |
函数
T | qAbs(const T &t) |
(since 6.1) typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> | qAddOverflow(T v1, T v2, T *result) |
quint64 | qFloatDistance(double a, double b) |
quint32 | qFloatDistance(float a, float b) |
int | qFpClassify(double val) |
int | qFpClassify(float val) |
bool | qFuzzyCompare(double p1, double p2) |
bool | qFuzzyCompare(float p1, float p2) |
bool | qFuzzyIsNull(double d) |
bool | qFuzzyIsNull(float f) |
double | qInf() |
bool | qIsFinite(double d) |
bool | qIsFinite(float f) |
bool | qIsInf(double d) |
bool | qIsInf(float f) |
bool | qIsNaN(double d) |
bool | qIsNaN(float f) |
(since 6.1) typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> | qMulOverflow(T v1, T v2, T *result) |
double | qQNaN() |
qint64 | qRound64(double d) |
qint64 | qRound64(float d) |
int | qRound(double d) |
int | qRound(float d) |
double | qSNaN() |
(since 6.1) typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> | qSubOverflow(T v1, T v2, T *result) |
函数文档
对浮点数值进行分类。
返回值在<cmath>
中定义:返回以下值之一,由val 的浮点分类决定:
- FP_NAN 非数字
- FP_INFINITE 无穷大(正数或负数)
- FP_ZERO 零(正数或负数)
- 带有全尾数的 FP_NORMAL 有限数
- 具有缩小尾数的 FP_SUBNORMAL 有限数
[constexpr]
template <typename T> T qAbs(const T &t)
将t 与 T 类型的 0 进行比较,并返回绝对值。因此,如果 T 是double,则t 与(double)0 比较。
示例
int absoluteValue; int myValue = -4; absoluteValue = qAbs(myValue); // absoluteValue == 4
[since 6.1]
template <typename T> typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> qAddOverflow(T v1, T v2, T *result)
将数值类型T
的两个值v1 和v2 相加,并将值记录在result 中。如果相加值溢出了类型T
的有效范围,则返回true
,否则返回false
。
保证 8 位、16 位和 32 位整数类型以及指针大小的整数类型都能实现。其他类型的溢出数学(如果可用)被视为私有 API。
此函数在 Qt 6.1 中引入。
quint64 qFloatDistance(double a, double b)
返回a 和b 之间可表示的浮点数的个数。
该函数的功能与qFloatDistance(float, float)
相同,但返回的是两个double
数字之间的距离。由于范围大于两个float
数 ([-DBL_MAX,DBL_MAX]
),因此返回类型为 quint64。
另请参见 qFuzzyCompare()。
quint32 qFloatDistance(float a, float b)
返回a 和b 之间可表示的浮点数的个数。
该函数提供了另一种对浮点数进行近似比较的方法,与qFuzzyCompare() 类似。不过,它返回的是两个数之间的距离,这使得调用者有可能选择可接受的错误。误差是相对的,例如,1.0E-5 和 1.00001E-5 之间的距离为 110,而 1.0E36 和 1.00001E36 之间的距离为 127。
如果浮点比较需要一定的精度,这个函数就非常有用。因此,如果a 和b 相等,它将返回 0。对于 32 位浮点数,它返回的最大值是 4,278,190,078。这是-FLT_MAX
和+FLT_MAX
之间的距离。
如果任何参数为Infinite
或NaN
,函数将不会给出有意义的结果。您可以调用qIsFinite() 来检查这一点。
返回值可视为 "误差",因此,如果您想比较两个 32 位浮点数,但只需要近似的 24 位精度,则可以这样使用该函数:
if (qFloatDistance(a, b) < (1 << 7)) { // The last 7 bits are not // significant // precise enough }
另请参见 qFuzzyCompare().
[constexpr noexcept]
bool qFuzzyCompare(double p1, double p2)
比较浮点数值p1 和p2 ,如果认为相等,则返回true
,否则返回false
。
请注意,比较p1 或p2 的值是否为 0.0 将不起作用,比较其中一个值为 NaN 或无穷大的值也不起作用。如果其中一个值总是 0.0,则使用qFuzzyIsNull 代替。如果其中一个值很可能是 0.0,一种解决办法是在两个值上都加上 1.0。
// Instead of comparing with 0.0 qFuzzyCompare(0.0, 1.0e-200); // This will return false // Compare adding 1 to both values will fix the problem qFuzzyCompare(1 + 0.0, 1 + 1.0e-200); // This will return true
两个数字是相对比较的,数字越小,精确度越高。
注意:此函数是线程安全的。
[constexpr noexcept]
bool qFuzzyCompare(float p1, float p2)
比较浮点数p1 和p2 ,如果认为相等,则返回true
,否则返回false
。
两个数字是相对比较的,数字越小,精确度越高。
注意:此函数是线程安全的。
[constexpr noexcept]
bool qFuzzyIsNull(double d)
如果d 的绝对值在 0.0 的 0.000000000001 范围内,则返回 true。
注意:此函数是线程安全的。
[constexpr noexcept]
bool qFuzzyIsNull(float f)
如果f 的绝对值在 0.0 的 0.00001f 范围内,则返回 true。
注意:此函数是线程安全的。
double qInf()
以二进制形式返回无穷级数的位型。
另请参阅 qIsInf()。
bool qIsFinite(double d)
如果双d 是有限数值,则返回true
。
bool qIsFinite(float f)
如果浮点数f 是有限数值,则返回true
。
bool qIsInf(double d)
如果双d 等于无穷大,则返回true
。
另请参见 qInf().
bool qIsInf(float f)
如果浮点数f 等于无穷大,则返回true
。
另请参阅 qInf().
bool qIsNaN(double d)
如果 doubled 不是数字 (NaN),则返回true
。
bool qIsNaN(float f)
如果浮点数f 不是数字(NaN),则返回true
。
[since 6.1]
template <typename T> typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> qMulOverflow(T v1, T v2, T *result)
将v1 和v2 相乘,并将乘积值记录在result 中。如果乘积值溢出T
类型的有效范围,则返回true
,否则返回false
。
保证 8 位、16 位和 32 位整数类型以及指针大小的整数类型都能实现。其他类型的溢出数学(如果可用)被视为私有 API。
此函数在 Qt 6.1 中引入。
double qQNaN()
以 double 形式返回静态 NaN 的位型。
另请参阅 qIsNaN().
[constexpr]
qint64 qRound64(double d)
将d 舍入为最接近的 64 位整数。
从 0 开始取整(例如 0.5 -> 1,-0.5 -> -1)。
注意: 此函数不保证高精度的正确性。
例如
double valueA = 42949672960.3; double valueB = 42949672960.7; qint64 roundedValueA = qRound64(valueA); // roundedValueA = 42949672960 qint64 roundedValueB = qRound64(valueB); // roundedValueB = 42949672961
注意: 如果d 的值超出qint64
的范围,则行为未定义。
[constexpr]
qint64 qRound64(float d)
将d 舍入为最接近的 64 位整数。
从 0 开始舍入一半(例如 0.5f -> 1,-0.5f ->-1)。
注意: 此函数不保证高精度的正确性。
例如
float valueA = 42949672960.3; float valueB = 42949672960.7; qint64 roundedValueA = qRound64(valueA); // roundedValueA = 42949672960 qint64 roundedValueB = qRound64(valueB); // roundedValueB = 42949672961
注意: 如果d 的值超出qint64
的范围,则行为未定义。
[constexpr]
int qRound(double d)
将d 舍入到最接近的整数。
从零开始取整(例如 0.5 -> 1,-0.5 -> -1)。
注意: 此函数不保证高精度的正确性。
例如
double valueA = 2.3; double valueB = 2.7; int roundedValueA = qRound(valueA); // roundedValueA = 2 int roundedValueB = qRound(valueB); // roundedValueB = 3
注意: 如果d 的值超出int
的范围,则行为未定义。
[constexpr]
int qRound(float d)
将d 舍入到最接近的整数。
从零开始取整(例如 0.5f -> 1,-0.5f ->-1)。
注意: 此函数不保证高精度的正确性。
例如
float valueA = 2.3; float valueB = 2.7; int roundedValueA = qRound(valueA); // roundedValueA = 2 int roundedValueB = qRound(valueB); // roundedValueB = 3
注意: 如果d 的值超出int
的范围,则行为未定义。
double qSNaN()
以双倍形式返回信号 NaN 的位型。
[since 6.1]
template <typename T> typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> qSubOverflow(T v1, T v2, T *result)
从v1 减去v2 ,并将所得值记录在result 中。如果减法超出T
类型的有效范围,则返回true
,否则返回false
。
保证 8 位、16 位和 32 位整数类型以及指针大小的整数类型都能实现。其他类型的溢出数学(如果可用)被视为私有 API。
此函数在 Qt 6.1 中引入。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.