From a97cda8b8b5806d3fd170c6ffd56094bbd60bd65 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 21 Apr 2020 02:09:27 +0200 Subject: QMath: make the math-related functions templates This way we can take and return more datatypes than qreal, just piggy-backing on the std:: functions (which take any integral and any fp datatype). This makes these functions pure ADL enablers (like qSwap). A type (hi, QAngle!) that wants to have math related functions simply needs those defined in its own namespace using the "standard" names (sin, cos, etc.); and we'll find them using the q-prefixed function. qCeil and qFloor signatures however still return int to avoid too much breakage. The FP-related functions (qIsInf, etc.) have been left alone. Those are "special"; a lot of care is in qnumeric because some implementations define them as macros, which blocks any possibility of user-defined overloads found via ADL. [ChangeLog][QtCore][QtMath] The math-related functions (such as qSin, qCos, qPow and so on) can now take an arbitrary parameter rather than just qreal. They will do a ADL-enabled call to the respective free function, using the functions in namespace std as a fallback. Moreover, they will now return whatever datatype is returned by the free function (e.g. long double if the call is placed on a long double). Change-Id: I111084eda52556663802e65a85e082187c2a6861 Reviewed-by: Lars Knoll Reviewed-by: Edward Welbourne --- src/corelib/kernel/qmath.h | 28 ++++++++++++++-------------- src/corelib/kernel/qmath.qdoc | 26 +++++++++++++------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h index 391d6d4282..57314901e8 100644 --- a/src/corelib/kernel/qmath.h +++ b/src/corelib/kernel/qmath.h @@ -65,85 +65,85 @@ QT_BEGIN_NAMESPACE extern Q_CORE_EXPORT const qreal qt_sine_table[QT_SINE_TABLE_SIZE]; -inline int qCeil(qreal v) +template int qCeil(T v) { using std::ceil; return int(ceil(v)); } -inline int qFloor(qreal v) +template int qFloor(T v) { using std::floor; return int(floor(v)); } -inline qreal qFabs(qreal v) +template auto qFabs(T v) { using std::fabs; return fabs(v); } -inline qreal qSin(qreal v) +template auto qSin(T v) { using std::sin; return sin(v); } -inline qreal qCos(qreal v) +template auto qCos(T v) { using std::cos; return cos(v); } -inline qreal qTan(qreal v) +template auto qTan(T v) { using std::tan; return tan(v); } -inline qreal qAcos(qreal v) +template auto qAcos(T v) { using std::acos; return acos(v); } -inline qreal qAsin(qreal v) +template auto qAsin(T v) { using std::asin; return asin(v); } -inline qreal qAtan(qreal v) +template auto qAtan(T v) { using std::atan; return atan(v); } -inline qreal qAtan2(qreal y, qreal x) +template auto qAtan2(T1 y, T2 x) { using std::atan2; return atan2(y, x); } -inline qreal qSqrt(qreal v) +template auto qSqrt(T v) { using std::sqrt; return sqrt(v); } -inline qreal qLn(qreal v) +template auto qLn(T v) { using std::log; return log(v); } -inline qreal qExp(qreal v) +template auto qExp(T v) { using std::exp; return exp(v); } -inline qreal qPow(qreal x, qreal y) +template auto qPow(T1 x, T2 y) { using std::pow; return pow(x, y); diff --git a/src/corelib/kernel/qmath.qdoc b/src/corelib/kernel/qmath.qdoc index 346171f044..82a4ae85a5 100644 --- a/src/corelib/kernel/qmath.qdoc +++ b/src/corelib/kernel/qmath.qdoc @@ -76,14 +76,14 @@ */ /*! - \fn qreal qFabs(qreal v) - Returns the absolute value of \a v as a qreal. + \fn template auto qFabs(T v) + Returns the absolute value of \a v. \relates */ /*! - \fn qreal qSin(qreal v) + \fn template auto qSin(T v) Returns the sine of the angle \a v in radians. \relates @@ -91,7 +91,7 @@ */ /*! - \fn qreal qCos(qreal v) + \fn template auto qCos(T v) Returns the cosine of an angle \a v in radians. \relates @@ -99,7 +99,7 @@ */ /*! - \fn qreal qTan(qreal v) + \fn template auto qTan(T v) Returns the tangent of an angle \a v in radians. \relates @@ -107,7 +107,7 @@ */ /*! - \fn qreal qAcos(qreal v) + \fn template auto qAcos(T v) Returns the arccosine of \a v as an angle in radians. Arccosine is the inverse operation of cosine. @@ -116,7 +116,7 @@ */ /*! - \fn qreal qAsin(qreal v) + \fn template auto qAsin(T v) Returns the arcsine of \a v as an angle in radians. Arcsine is the inverse operation of sine. @@ -125,7 +125,7 @@ */ /*! - \fn qreal qAtan(qreal v) + \fn template auto qAtan(T v) Returns the arctangent of \a v as an angle in radians. Arctangent is the inverse operation of tangent. @@ -134,7 +134,7 @@ */ /*! - \fn qreal qAtan2(qreal y, qreal x) + \fn template auto qAtan2(T1 y, T2 x) Returns the arctangent of a point specified by the coordinates \a y and \a x. This function will return the angle (argument) of that point. @@ -143,7 +143,7 @@ */ /*! - \fn qreal qSqrt(qreal v) + \fn template auto qSqrt(T v) Returns the square root of \a v. This function returns a NaN if \a v is a negative number. @@ -152,7 +152,7 @@ */ /*! - \fn qreal qLn(qreal v) + \fn template auto qLn(T v) Returns the natural logarithm of \a v. Natural logarithm uses base e. \relates @@ -160,7 +160,7 @@ */ /*! - \fn qreal qExp(qreal v) + \fn template auto qExp(T v) Returns the exponential function of \c e to the power of \a v. \relates @@ -168,7 +168,7 @@ */ /*! - \fn qreal qPow(qreal x, qreal y) + \fn template auto qPow(T1 x, T2 y) Returns the value of \a x raised to the power of \a y. That is, \a x is the base and \a y is the exponent. -- cgit v1.2.3