<QtNumeric> - Qt Numeric Functions

The <QtNumeric> header file provides common numeric functions. 更多...

头: #include <QtNumeric>

函数

T qAbs (const T & t )
typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> qAddOverflow (T v1 , T v2 , T * result )
quint32 qFloatDistance (float a , float b )
quint64 qFloatDistance (double a , double 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 )
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 ()
typename std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> qSubOverflow (T v1 , T v2 , T * result )

详细描述

The <QtNumeric> header file contains various numeric functions for comparing and adjusting a numeric value.

函数文档编制

int qFpClassify ( double val )

int qFpClassify ( float val )

分类浮点值。

返回值的定义在 <cmath> : returns one of the following, determined by the floating-point class of val :

  • FP_NAN 非数字
  • FP_INFINITE 无穷大 (正值或负值)
  • FP_ZERO zero (positive or negative)
  • FP_NORMAL finite with a full mantissa
  • FP_SUBNORMAL finite with a reduced mantissa

[constexpr] template <typename T> T qAbs (const T & t )

比较 t to the 0 of type T and returns the absolute value. Thus if T is double ,那么 t is compared to (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 )

Adds two values v1 and v2 , of a numeric type T and records the value in result . If the addition overflows the valid range for type T ,返回 true ,否则返回 false .

An implementation is guaranteed to be available for 8-, 16-, and 32-bit integer types, as well as integer types of the size of a pointer. Overflow math for other types, if available, is considered private API.

该函数在 Qt 6.1 引入。

quint32 qFloatDistance ( float a , float b )

Returns the number of representable floating-point numbers between a and b .

This function provides an alternative way of doing approximated comparisons of floating-point numbers similar to qFuzzyCompare (). However, it returns the distance between two numbers, which gives the caller a possibility to choose the accepted error. Errors are relative, so for instance the distance between 1.0E-5 and 1.00001E-5 will give 110, while the distance between 1.0E36 and 1.00001E36 will give 127.

This function is useful if a floating point comparison requires a certain precision. Therefore, if a and b are equal it will return 0. The maximum value it will return for 32-bit floating point numbers is 4,278,190,078. This is the distance between -FLT_MAX and +FLT_MAX .

The function does not give meaningful results if any of the arguments are Infinite or NaN . You can check for this by calling qIsFinite ().

The return value can be considered as the "error", so if you for instance want to compare two 32-bit floating point numbers and all you need is an approximated 24-bit precision, you can use this function like this:

    if (qFloatDistance(a, b) < (1 << 7)) {   // The last 7 bits are not
                                            // significant
        // precise enough
    }
					

另请参阅 qFuzzyCompare ().

quint64 qFloatDistance ( double a , double b )

Returns the number of representable floating-point numbers between a and b .

This function serves the same purpose as qFloatDistance(float, float) , but returns the distance between two double numbers. Since the range is larger than for two float numbers ( [-DBL_MAX,DBL_MAX] ), the return type is quint64.

另请参阅 qFuzzyCompare ().

int qFpClassify ( double val )

int qFpClassify ( float val )

[constexpr] bool qFuzzyCompare ( double p1 , double p2 )

比较浮点值 p1 and p2 并返回 true 若它们被认为相等,否则 false .

Note that comparing values where either p1 or p2 is 0.0 will not work, nor does comparing values where one of the values is NaN or infinity. If one of the values is always 0.0, use qFuzzyIsNull instead. If one of the values is likely to be 0.0, one solution is to add 1.0 to both values.

// 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
					

The two numbers are compared in a relative way, where the exactness is stronger the smaller the numbers are.

注意: 此函数是 thread-safe .

[constexpr] bool qFuzzyCompare ( float p1 , float p2 )

比较浮点值 p1 and p2 并返回 true 若它们被认为相等,否则 false .

The two numbers are compared in a relative way, where the exactness is stronger the smaller the numbers are.

注意: 此函数是 thread-safe .

[constexpr] bool qFuzzyIsNull ( double d )

Returns true if the absolute value of d is within 0.000000000001 of 0.0.

注意: 此函数是 thread-safe .

[constexpr] bool qFuzzyIsNull ( float f )

Returns true if the absolute value of f is within 0.00001f of 0.0.

注意: 此函数是 thread-safe .

double qInf ()

Returns the bit pattern for an infinite number as a double.

另请参阅 qIsInf ().

bool qIsFinite ( double d )

返回 true if the double d 是有限数。

bool qIsFinite ( float f )

返回 true if the float f 是有限数。

bool qIsInf ( double d )

返回 true if the double d 相当于无穷大。

另请参阅 qInf ().

bool qIsInf ( float f )

返回 true if the float f 相当于无穷大。

另请参阅 qInf ().

bool qIsNaN ( double d )

返回 true if the double d 是 NaN (非数字)。

bool qIsNaN ( float f )

返回 true if the float f 是 NaN (非数字)。

[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 )

Multiplies v1 and v2 , and records the resulting value in result . If the multiplication overflows the valid range for type T ,返回 true ,否则返回 false .

An implementation is guaranteed to be available for 8-, 16-, and 32-bit integer types, as well as integer types of the size of a pointer. Overflow math for other types, if available, is considered private API.

该函数在 Qt 6.1 引入。

double qQNaN ()

Returns the bit pattern of a quiet NaN as a double.

另请参阅 qIsNaN ().

[constexpr] qint64 qRound64 ( double d )

圆整 d 到最近 64 位整数。

Rounds half away from zero (e.g. 0.5 -> 1, -0.5 -> -1).

注意: This function does not guarantee correctness for high precisions.

范例:

double valueA = 42949672960.3;
double valueB = 42949672960.7;
qint64 roundedValueA = qRound64(valueA);
// roundedValueA = 42949672960
qint64 roundedValueB = qRound64(valueB);
// roundedValueB = 42949672961
					

注意: If the value d is outside the range of qint64 , the behavior is undefined.

[constexpr] qint64 qRound64 ( float d )

圆整 d 到最近 64 位整数。

Rounds half away from zero (e.g. 0.5f -> 1, -0.5f -> -1).

注意: This function does not guarantee correctness for high precisions.

范例:

float valueA = 42949672960.3;
float valueB = 42949672960.7;
qint64 roundedValueA = qRound64(valueA);
// roundedValueA = 42949672960
qint64 roundedValueB = qRound64(valueB);
// roundedValueB = 42949672961
					

注意: If the value d is outside the range of qint64 , the behavior is undefined.

[constexpr] int qRound ( double d )

圆整 d 到最近整数。

Rounds half away from zero (e.g. 0.5 -> 1, -0.5 -> -1).

注意: This function does not guarantee correctness for high precisions.

范例:

double valueA = 2.3;
double valueB = 2.7;
int roundedValueA = qRound(valueA);
// roundedValueA = 2
int roundedValueB = qRound(valueB);
// roundedValueB = 3
					

注意: If the value d is outside the range of int , the behavior is undefined.

[constexpr] int qRound ( float d )

圆整 d 到最近整数。

Rounds half away from zero (e.g. 0.5f -> 1, -0.5f -> -1).

注意: This function does not guarantee correctness for high precisions.

范例:

float valueA = 2.3;
float valueB = 2.7;
int roundedValueA = qRound(valueA);
// roundedValueA = 2
int roundedValueB = qRound(valueB);
// roundedValueB = 3
					

注意: If the value d is outside the range of int , the behavior is undefined.

double qSNaN ()

Returns the bit pattern of a signalling NaN as a double.

[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 )

减去 v2 from v1 and records the resulting value in result . If the subtraction overflows the valid range for type T ,返回 true ,否则返回 false .

An implementation is guaranteed to be available for 8-, 16-, and 32-bit integer types, as well as integer types of the size of a pointer. Overflow math for other types, if available, is considered private API.

该函数在 Qt 6.1 引入。