Skip to content

Instantly share code, notes, and snippets.

@b-epelbaum
Created February 24, 2023 11:17
Show Gist options
  • Select an option

  • Save b-epelbaum/068f6109f97a1c6148d4c61f131715d0 to your computer and use it in GitHub Desktop.

Select an option

Save b-epelbaum/068f6109f97a1c6148d4c61f131715d0 to your computer and use it in GitHub Desktop.
fuzzy logic in Qt
<QtGlobal> - Global Qt Declarations
The <QtGlobal> header file includes the fundamental global declarations. It is included by most other Qt header files. More...
Header: #include <QtGlobal>
Obsolete members
Types
typedef QFunctionPointer
typedef QtMessageHandler
enum QtMsgType { QtDebugMsg, QtInfoMsg, QtWarningMsg, QtCriticalMsg, QtFatalMsg, QtSystemMsg }
typedef qint8
typedef qint16
typedef qint32
typedef qint64
typedef qintptr
typedef qlonglong
typedef qptrdiff
typedef qreal
(alias) qsizetype
typedef quint8
typedef quint16
typedef quint32
typedef quint64
typedef quintptr
typedef qulonglong
typedef uchar
typedef uint
typedef ulong
typedef ushort
Functions
T qAbs(const T &t)
typename std::add_const<T>::type & qAsConst(T &t)
void qAsConst(const T &&t)
const T & qBound(const T &min, const T &val, const T &max)
auto qConstOverload(T memberFunctionPointer)
QString qEnvironmentVariable(const char *varName)
QString qEnvironmentVariable(const char *varName, const QString &defaultValue)
int qEnvironmentVariableIntValue(const char *varName, bool *ok = nullptr)
bool qEnvironmentVariableIsEmpty(const char *varName)
bool qEnvironmentVariableIsSet(const char *varName)
T qExchange(T &obj, U &&newValue)
quint32 qFloatDistance(float a, float b)
quint64 qFloatDistance(double a, double b)
QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
int qFpClassify(double val)
bool qFuzzyCompare(double p1, double p2)
bool qFuzzyCompare(float p1, float p2)
bool qFuzzyIsNull(double d)
bool qFuzzyIsNull(float f)
double qInf()
QtMessageHandler qInstallMessageHandler(QtMessageHandler handler)
bool qIsFinite(double d)
bool qIsFinite(float f)
bool qIsInf(double d)
bool qIsInf(float f)
bool qIsNaN(double d)
bool qIsNaN(float f)
const T & qMax(const T &a, const T &b)
const T & qMin(const T &a, const T &b)
auto qNonConstOverload(T memberFunctionPointer)
auto qOverload(T functionPointer)
double qQNaN()
qint64 qRound64(double d)
qint64 qRound64(float d)
int qRound(double d)
int qRound(float d)
double qSNaN()
void qSetMessagePattern(const QString &pattern)
const char * qVersion()
T * q_check_ptr(T *p)
QByteArray qgetenv(const char *varName)
bool qputenv(const char *varName, const QByteArray &value)
QString qtTrId(const char *id, int n = -1)
bool qunsetenv(const char *varName)
Macros
QT_DEPRECATED_WARNINGS
QT_DISABLE_DEPRECATED_BEFORE
QT_NO_DEPRECATED_WARNINGS
QT_POINTER_SIZE
QT_REQUIRE_VERSION(int argc, char **argv, const char *version)
QT_TRANSLATE_NOOP3(context, sourceText, disambiguation)
QT_TRANSLATE_NOOP(context, sourceText)
QT_TRANSLATE_N_NOOP3(context, sourceText, comment)
QT_TRANSLATE_N_NOOP(context, sourceText)
QT_TRID_NOOP(id)
QT_TR_NOOP(sourceText)
QT_TR_N_NOOP(sourceText)
QT_VERSION
QT_VERSION_CHECK
QT_VERSION_STR
void Q_ASSERT(bool test)
void Q_ASSERT_X(bool test, const char *where, const char *what)
void Q_ASSUME(bool expr)
Q_BIG_ENDIAN
Q_BYTE_ORDER
Q_CC_BOR
Q_CC_CDS
Q_CC_CLANG
Q_CC_COMEAU
Q_CC_DEC
Q_CC_EDG
Q_CC_GHS
Q_CC_GNU
Q_CC_HIGHC
Q_CC_HPACC
Q_CC_INTEL
Q_CC_KAI
Q_CC_MIPS
Q_CC_MSVC
Q_CC_OC
Q_CC_PGI
Q_CC_SUN
Q_CC_SYM
Q_CC_USLC
Q_CC_WAT
void Q_CHECK_PTR(void *pointer)
Q_DECLARE_TYPEINFO(Type, Flags)
Q_DECL_CONSTEXPR
Q_DECL_EXPORT
Q_DECL_IMPORT
Q_DECL_NOEXCEPT
Q_DECL_NOEXCEPT_EXPR(x)
Q_DECL_NOTHROW
Q_DECL_RELAXED_CONSTEXPR
void Q_FALLTHROUGH
Q_FOREACH(variable, container)
Q_FOREVER
Q_FORWARD_DECLARE_CF_TYPE(type)
Q_FORWARD_DECLARE_MUTABLE_CF_TYPE(type)
Q_FORWARD_DECLARE_OBJC_CLASS(classname)
const char* Q_FUNC_INFO
qint64 Q_INT64_C(literal)
Q_LIKELY(expr)
Q_LITTLE_ENDIAN
Q_OS_AIX
Q_OS_ANDROID
Q_OS_BSD4
Q_OS_CYGWIN
Q_OS_DARWIN
Q_OS_FREEBSD
Q_OS_HPUX
Q_OS_HURD
Q_OS_IOS
Q_OS_LINUX
Q_OS_LYNX
Q_OS_MAC
Q_OS_MACOS
Q_OS_NETBSD
Q_OS_OPENBSD
Q_OS_OSX
Q_OS_QNX
Q_OS_SOLARIS
Q_OS_TVOS
Q_OS_UNIX
Q_OS_WASM
Q_OS_WATCHOS
Q_OS_WIN32
Q_OS_WIN64
Q_OS_WIN
Q_OS_WINDOWS
Q_OS_WINRT
Q_PROCESSOR_X86
Q_PROCESSOR_S390
Q_PROCESSOR_ALPHA
Q_PROCESSOR_ARM
Q_PROCESSOR_ARM_V5
Q_PROCESSOR_ARM_V6
Q_PROCESSOR_ARM_V7
Q_PROCESSOR_AVR32
Q_PROCESSOR_BLACKFIN
Q_PROCESSOR_IA64
Q_PROCESSOR_MIPS
Q_PROCESSOR_MIPS_32
Q_PROCESSOR_MIPS_64
Q_PROCESSOR_MIPS_I
Q_PROCESSOR_MIPS_II
Q_PROCESSOR_MIPS_III
Q_PROCESSOR_MIPS_IV
Q_PROCESSOR_MIPS_V
Q_PROCESSOR_POWER
Q_PROCESSOR_POWER_32
Q_PROCESSOR_POWER_64
Q_PROCESSOR_RISCV
Q_PROCESSOR_RISCV_32
Q_PROCESSOR_RISCV_64
Q_PROCESSOR_S390_X
Q_PROCESSOR_SH
Q_PROCESSOR_SH_4A
Q_PROCESSOR_SPARC
Q_PROCESSOR_SPARC_V9
Q_PROCESSOR_X86_32
Q_PROCESSOR_X86_64
quint64 Q_UINT64_C(literal)
Q_UNLIKELY(expr)
void Q_UNREACHABLE
Q_UNUSED(name)
foreach(variable, container)
forever
qCritical(const char *message, ...)
qDebug(const char *message, ...)
qFatal(const char *message, ...)
qInfo(const char *message, ...)
const char * qPrintable(const QString &str)
const wchar_t * qUtf16Printable(const QString &str)
const char * qUtf8Printable(const QString &str)
qWarning(const char *message, ...)
Detailed Description
The global declarations include types, functions and macros.
The type definitions are partly convenience definitions for basic types (some of which guarantee certain bit-sizes on all platforms supported by Qt), partly types related to Qt message handling. The functions are related to generating messages, Qt version handling and comparing and adjusting object values. And finally, some of the declared macros enable programmers to add compiler or platform specific code to their applications, while others are convenience macros for larger operations.
Types
The header file declares several type definitions that guarantee a specified bit-size on all platforms supported by Qt for various basic types, for example qint8 which is a signed char guaranteed to be 8-bit on all platforms supported by Qt. The header file also declares the qlonglong type definition for long long int (__int64 on Windows).
Several convenience type definitions are declared: qreal for double or float, uchar for unsigned char, uint for unsigned int, ulong for unsigned long and ushort for unsigned short.
Finally, the QtMsgType definition identifies the various messages that can be generated and sent to a Qt message handler; QtMessageHandler is a type definition for a pointer to a function with the signature void myMessageHandler(QtMsgType, const QMessageLogContext &, const char *). QMessageLogContext class contains the line, file, and function the message was logged at. This information is created by the QMessageLogger class.
Functions
The <QtGlobal> header file contains several functions comparing and adjusting an object's value. These functions take a template type as argument: You can retrieve the absolute value of an object using the qAbs() function, and you can bound a given object's value by given minimum and maximum values using the qBound() function. You can retrieve the minimum and maximum of two given objects using qMin() and qMax() respectively. All these functions return a corresponding template type; the template types can be replaced by any other type.
Example:
int myValue = 10;
int minValue = 2;
int maxValue = 6;
int boundedValue = qBound(minValue, myValue, maxValue);
// boundedValue == 6
<QtGlobal> also contains functions that generate messages from the given string argument: qDebug(), qInfo(), qWarning(), qCritical(), and qFatal(). These functions call the message handler with the given message.
Example:
if (!driver()->isOpen() || driver()->isOpenError()) {
qWarning("QSqlQuery::exec: database not open");
return false;
}
The remaining functions are qRound() and qRound64(), which both accept a double or float value as their argument returning the value rounded up to the nearest integer and 64-bit integer respectively, the qInstallMessageHandler() function which installs the given QtMessageHandler, and the qVersion() function which returns the version number of Qt at run-time as a string.
Macros
The <QtGlobal> header file provides a range of macros (Q_CC_*) that are defined if the application is compiled using the specified platforms. For example, the Q_CC_SUN macro is defined if the application is compiled using Forte Developer, or Sun Studio C++. The header file also declares a range of macros (Q_OS_*) that are defined for the specified platforms. For example, Q_OS_UNIX which is defined for the Unix-based systems.
The purpose of these macros is to enable programmers to add compiler or platform specific code to their application.
The remaining macros are convenience macros for larger operations: The QT_TR_NOOP(), QT_TRANSLATE_NOOP(), and QT_TRANSLATE_NOOP3() macros provide the possibility of marking strings for delayed translation. QT_TR_N_NOOP(), QT_TRANSLATE_N_NOOP(), and QT_TRANSLATE_N_NOOP3() are numerator dependent variants of these. The Q_ASSERT() and Q_ASSERT_X() enables warning messages of various level of refinement. The Q_FOREACH() and foreach() macros implement Qt's foreach loop.
The Q_INT64_C() and Q_UINT64_C() macros wrap signed and unsigned 64-bit integer literals in a platform-independent way. The Q_CHECK_PTR() macro prints a warning containing the source code's file name and line number, saying that the program ran out of memory, if the pointer is nullptr. The qPrintable() and qUtf8Printable() macros represent an easy way of printing text.
The QT_POINTER_SIZE macro expands to the size of a pointer in bytes.
The macros QT_VERSION and QT_VERSION_STR expand to a numeric value or a string, respectively, that specifies the version of Qt that the application is compiled against.
See also <QtAlgorithms> and QSysInfo.
Type Documentation
typedef QFunctionPointer
This is a typedef for void (*)(), a pointer to a function that takes no arguments and returns void.
typedef QtMessageHandler
This is a typedef for a pointer to a function with the following signature:
void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
This typedef was introduced in Qt 5.0.
See also QtMsgType and qInstallMessageHandler().
enum QtMsgType
This enum describes the messages that can be sent to a message handler (QtMessageHandler). You can use the enum to identify and associate the various message types with the appropriate actions.
Constant Value Description
QtDebugMsg 0 A message generated by the qDebug() function.
QtInfoMsg 4 A message generated by the qInfo() function.
QtWarningMsg 1 A message generated by the qWarning() function.
QtCriticalMsg 2 A message generated by the qCritical() function.
QtFatalMsg 3 A message generated by the qFatal() function.
QtSystemMsg QtCriticalMsg
QtInfoMsg was added in Qt 5.5.
See also QtMessageHandler and qInstallMessageHandler().
typedef qint8
Typedef for signed char. This type is guaranteed to be 8-bit on all platforms supported by Qt.
typedef qint16
Typedef for signed short. This type is guaranteed to be 16-bit on all platforms supported by Qt.
typedef qint32
Typedef for signed int. This type is guaranteed to be 32-bit on all platforms supported by Qt.
typedef qint64
Typedef for long long int (__int64 on Windows). This type is guaranteed to be 64-bit on all platforms supported by Qt.
Literals of this type can be created using the Q_INT64_C() macro:
qint64 value = Q_INT64_C(932838457459459);
See also Q_INT64_C(), quint64, and qlonglong.
typedef qintptr
Integral type for representing pointers in a signed integer (useful for hashing, etc.).
Typedef for either qint32 or qint64. This type is guaranteed to be the same size as a pointer on all platforms supported by Qt. On a system with 32-bit pointers, qintptr is a typedef for qint32; on a system with 64-bit pointers, qintptr is a typedef for qint64.
Note that qintptr is signed. Use quintptr for unsigned values.
See also qptrdiff, qint32, and qint64.
typedef qlonglong
Typedef for long long int (__int64 on Windows). This is the same as qint64.
See also qulonglong and qint64.
typedef qptrdiff
Integral type for representing pointer differences.
Typedef for either qint32 or qint64. This type is guaranteed to be the same size as a pointer on all platforms supported by Qt. On a system with 32-bit pointers, quintptr is a typedef for quint32; on a system with 64-bit pointers, quintptr is a typedef for quint64.
Note that qptrdiff is signed. Use quintptr for unsigned values.
See also quintptr, qint32, and qint64.
typedef qreal
Typedef for double unless Qt is configured with the -qreal float option.
[alias]qsizetype
This is a type alias for QIntegerForSizeof<std::size_t>::Signed.
Integral type providing Posix' ssize_t for all platforms.
This type is guaranteed to be the same size as a size_t on all platforms supported by Qt.
Note that qsizetype is signed. Use size_t for unsigned values.
This alias was introduced in Qt 5.10.
See also qptrdiff.
typedef quint8
Typedef for unsigned char. This type is guaranteed to be 8-bit on all platforms supported by Qt.
typedef quint16
Typedef for unsigned short. This type is guaranteed to be 16-bit on all platforms supported by Qt.
typedef quint32
Typedef for unsigned int. This type is guaranteed to be 32-bit on all platforms supported by Qt.
typedef quint64
Typedef for unsigned long long int (unsigned __int64 on Windows). This type is guaranteed to be 64-bit on all platforms supported by Qt.
Literals of this type can be created using the Q_UINT64_C() macro:
quint64 value = Q_UINT64_C(932838457459459);
See also Q_UINT64_C(), qint64, and qulonglong.
typedef quintptr
Integral type for representing pointers in an unsigned integer (useful for hashing, etc.).
Typedef for either quint32 or quint64. This type is guaranteed to be the same size as a pointer on all platforms supported by Qt. On a system with 32-bit pointers, quintptr is a typedef for quint32; on a system with 64-bit pointers, quintptr is a typedef for quint64.
Note that quintptr is unsigned. Use qptrdiff for signed values.
See also qptrdiff, quint32, and quint64.
typedef qulonglong
Typedef for unsigned long long int (unsigned __int64 on Windows). This is the same as quint64.
See also quint64 and qlonglong.
typedef uchar
Convenience typedef for unsigned char.
typedef uint
Convenience typedef for unsigned int.
typedef ulong
Convenience typedef for unsigned long.
typedef ushort
Convenience typedef for unsigned short.
Function Documentation
QString qEnvironmentVariable(const char *varName)
QString qEnvironmentVariable(const char *varName, const QString &defaultValue)
These functions return the value of the environment variable, varName, as a QString. If no variable varName is found in the environment and defaultValue is provided, defaultValue is returned. Otherwise QString() is returned.
The Qt environment manipulation functions are thread-safe, but this requires that the C library equivalent functions like getenv and putenv are not directly called.
The following table describes how to choose between qgetenv() and qEnvironmentVariable():
Condition Recommendation
Variable contains file paths or user text qEnvironmentVariable()
Windows-specific code qEnvironmentVariable()
Unix-specific code, destination variable is not QString and/or is used to interface with non-Qt APIs qgetenv()
Destination variable is a QString qEnvironmentVariable()
Destination variable is a QByteArray or std::string qgetenv()
Note: on Unix systems, this function may produce data loss if the original string contains arbitrary binary data that cannot be decoded by the locale codec. Use qgetenv() instead for that case. On Windows, this function is lossless.
Note: the variable name varName must contain only US-ASCII characters.
This function was introduced in Qt 5.10.
See also qputenv(), qgetenv(), qEnvironmentVariableIsSet(), and qEnvironmentVariableIsEmpty().
template <typename T> T qAbs(const T &t)
Compares t to the 0 of type T and returns the absolute value. Thus if T is double, then t is compared to (double) 0.
Example:
int absoluteValue;
int myValue = -4;
absoluteValue = qAbs(myValue);
// absoluteValue == 4
template <typename T> typename std::add_const<T>::type &qAsConst(T &t)
Returns t cast to const T.
This function is a Qt implementation of C++17's std::as_const(), a cast function like std::move(). But while std::move() turns lvalues into rvalues, this function turns non-const lvalues into const lvalues. Like std::as_const(), it doesn't work on rvalues, because it cannot be efficiently implemented for rvalues without leaving dangling references.
Its main use in Qt is to prevent implicitly-shared Qt containers from detaching:
QString s = ...;
for (QChar ch : s) // detaches 's' (performs a deep-copy if 's' was shared)
process(ch);
for (QChar ch : qAsConst(s)) // ok, no detach attempt
process(ch);
Of course, in this case, you could (and probably should) have declared s as const in the first place:
const QString s = ...;
for (QChar ch : s) // ok, no detach attempt on const objects
process(ch);
but often that is not easily possible.
It is important to note that qAsConst() does not copy its argument, it just performs a const_cast<const T&>(t). This is also the reason why it is designed to fail for rvalues: The returned reference would go stale too soon. So while this works (but detaches the returned object):
for (QChar ch : funcReturningQString())
process(ch); // OK, the returned object is kept alive for the loop's duration
this would not:
for (QChar ch : qAsConst(funcReturningQString()))
process(ch); // ERROR: ch is copied from deleted memory
To prevent this construct from compiling (and failing at runtime), qAsConst() has a second, deleted, overload which binds to rvalues.
This function was introduced in Qt 5.7.
template <typename T> void qAsConst(const T &&t)
This is an overloaded function.
This overload is deleted to prevent a dangling reference in code like
for (QChar ch : qAsConst(funcReturningQString()))
process(ch); // ERROR: ch is copied from deleted memory
This function was introduced in Qt 5.7.
template <typename T> const T &qBound(const T &min, const T &val, const T &max)
Returns val bounded by min and max. This is equivalent to qMax(min, qMin(val, max)).
Example:
int myValue = 10;
int minValue = 2;
int maxValue = 6;
int boundedValue = qBound(minValue, myValue, maxValue);
// boundedValue == 6
See also qMin() and qMax().
template <typename T> auto qConstOverload(T memberFunctionPointer)
Returns the memberFunctionPointer pointer to a constant member function:
struct Foo {
void overloadedFunction(int, const QString &);
void overloadedFunction(int, const QString &) const;
};
... qConstOverload<int, const QString &>(&Foo::overloadedFunction)
... qNonConstOverload<int, const QString &>(&Foo::overloadedFunction)
This function was introduced in Qt 5.7.
See also qOverload, qNonConstOverload, and Differences between String-Based and Functor-Based Connections.
QString qEnvironmentVariable(const char *varName)
QString qEnvironmentVariable(const char *varName, const QString &defaultValue)
int qEnvironmentVariableIntValue(const char *varName, bool *ok = nullptr)
Returns the numerical value of the environment variable varName. If ok is not null, sets *ok to true or false depending on the success of the conversion.
Equivalent to
qgetenv(varName).toInt(ok, 0)
except that it's much faster, and can't throw exceptions.
Note: there's a limit on the length of the value, which is sufficient for all valid values of int, not counting leading zeroes or spaces. Values that are too long will either be truncated or this function will set ok to false.
This function was introduced in Qt 5.5.
See also qgetenv(), qEnvironmentVariable(), and qEnvironmentVariableIsSet().
bool qEnvironmentVariableIsEmpty(const char *varName)
Returns whether the environment variable varName is empty.
Equivalent to
qgetenv(varName).isEmpty()
except that it's potentially much faster, and can't throw exceptions.
This function was introduced in Qt 5.1.
See also qgetenv(), qEnvironmentVariable(), and qEnvironmentVariableIsSet().
bool qEnvironmentVariableIsSet(const char *varName)
Returns whether the environment variable varName is set.
Equivalent to
!qgetenv(varName).isNull()
except that it's potentially much faster, and can't throw exceptions.
This function was introduced in Qt 5.1.
See also qgetenv(), qEnvironmentVariable(), and qEnvironmentVariableIsEmpty().
template <typename T, typename U> T qExchange(T &obj, U &&newValue)
Replaces the value of obj with newValue and returns the old value of obj.
This is Qt's implementation of std::exchange(). It differs from std::exchange() only in that it is constexpr already in C++14, and available on all supported compilers.
Here is how to use qExchange() to implement move constructors:
MyClass(MyClass &&other)
: m_pointer{qExchange(other.m_pointer, nullptr)},
m_int{qExchange(other.m_int, 0)},
m_vector{std::move(other.m_vector)},
...
For members of class type, we can use std::move(), as their move-constructor will do the right thing. But for scalar types such as raw pointers or integer type, move is the same as copy, which, particularly for pointers, is not what we expect. So, we cannot use std::move() for such types, but we can use std::exchange()/qExchange() to make sure the source object's member is already reset by the time we get to the initialization of our next data member, which might come in handy if the constructor exits with an exception.
Here is how to use qExchange() to write a loop that consumes the collection it iterates over:
for (auto &e : qExchange(collection, {})
doSomethingWith(e);
Which is equivalent to the following, much more verbose code:
{
auto tmp = std::move(collection);
collection = {}; // or collection.clear()
for (auto &e : tmp)
doSomethingWith(e);
} // destroys 'tmp'
This is perfectly safe, as the for-loop keeps the result of qExchange() alive for as long as the loop runs, saving the declaration of a temporary variable. Be aware, though, that qExchange() returns a non-const object, so Qt containers may detach.
This function was introduced in Qt 5.14.
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
}
This function was introduced in Qt 5.2.
See also 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.
This function was introduced in Qt 5.2.
See also qFuzzyCompare().
QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, const QString &str)
Generates a formatted string out of the type, context, str arguments.
qFormatLogMessage returns a QString that is formatted according to the current message pattern. It can be used by custom message handlers to format output similar to Qt's default message handler.
The function is thread-safe.
This function was introduced in Qt 5.4.
See also qInstallMessageHandler() and qSetMessagePattern().
int qFpClassify(double val)
Classifies a floating-point value.
The return values are defined in <cmath>: returns one of the following, determined by the floating-point class of val:
FP_NAN not a number
FP_INFINITE infinities (positive or negative)
FP_ZERO zero (positive or negative)
FP_NORMAL finite with a full mantissa
FP_SUBNORMAL finite with a reduced mantissa
bool qFuzzyCompare(double p1, double p2)
Compares the floating point value p1 and p2 and returns true if they are considered equal, otherwise 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.
Note: This function is thread-safe.
This function was introduced in Qt 4.4.
bool qFuzzyCompare(float p1, float p2)
Compares the floating point value p1 and p2 and returns true if they are considered equal, otherwise false.
The two numbers are compared in a relative way, where the exactness is stronger the smaller the numbers are.
Note: This function is thread-safe.
This function was introduced in Qt 4.4.
bool qFuzzyIsNull(double d)
Returns true if the absolute value of d is within 0.000000000001 of 0.0.
Note: This function is thread-safe.
This function was introduced in Qt 4.4.
bool qFuzzyIsNull(float f)
Returns true if the absolute value of f is within 0.00001f of 0.0.
Note: This function is thread-safe.
This function was introduced in Qt 4.4.
double qInf()
Returns the bit pattern for an infinite number as a double.
See also qIsInf().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment