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 <lars.knoll@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2020-04-21 02:09:27 +02:00
parent d2a04cf165
commit a97cda8b8b
2 changed files with 27 additions and 27 deletions

View File

@ -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 <typename T> int qCeil(T v)
{
using std::ceil;
return int(ceil(v));
}
inline int qFloor(qreal v)
template <typename T> int qFloor(T v)
{
using std::floor;
return int(floor(v));
}
inline qreal qFabs(qreal v)
template <typename T> auto qFabs(T v)
{
using std::fabs;
return fabs(v);
}
inline qreal qSin(qreal v)
template <typename T> auto qSin(T v)
{
using std::sin;
return sin(v);
}
inline qreal qCos(qreal v)
template <typename T> auto qCos(T v)
{
using std::cos;
return cos(v);
}
inline qreal qTan(qreal v)
template <typename T> auto qTan(T v)
{
using std::tan;
return tan(v);
}
inline qreal qAcos(qreal v)
template <typename T> auto qAcos(T v)
{
using std::acos;
return acos(v);
}
inline qreal qAsin(qreal v)
template <typename T> auto qAsin(T v)
{
using std::asin;
return asin(v);
}
inline qreal qAtan(qreal v)
template <typename T> auto qAtan(T v)
{
using std::atan;
return atan(v);
}
inline qreal qAtan2(qreal y, qreal x)
template <typename T1, typename T2> auto qAtan2(T1 y, T2 x)
{
using std::atan2;
return atan2(y, x);
}
inline qreal qSqrt(qreal v)
template <typename T> auto qSqrt(T v)
{
using std::sqrt;
return sqrt(v);
}
inline qreal qLn(qreal v)
template <typename T> auto qLn(T v)
{
using std::log;
return log(v);
}
inline qreal qExp(qreal v)
template <typename T> auto qExp(T v)
{
using std::exp;
return exp(v);
}
inline qreal qPow(qreal x, qreal y)
template <typename T1, typename T2> auto qPow(T1 x, T2 y)
{
using std::pow;
return pow(x, y);

View File

@ -76,14 +76,14 @@
*/
/*!
\fn qreal qFabs(qreal v)
Returns the absolute value of \a v as a qreal.
\fn template <typename T> auto qFabs(T v)
Returns the absolute value of \a v.
\relates <QtMath>
*/
/*!
\fn qreal qSin(qreal v)
\fn template <typename T> auto qSin(T v)
Returns the sine of the angle \a v in radians.
\relates <QtMath>
@ -91,7 +91,7 @@
*/
/*!
\fn qreal qCos(qreal v)
\fn template <typename T> auto qCos(T v)
Returns the cosine of an angle \a v in radians.
\relates <QtMath>
@ -99,7 +99,7 @@
*/
/*!
\fn qreal qTan(qreal v)
\fn template <typename T> auto qTan(T v)
Returns the tangent of an angle \a v in radians.
\relates <QtMath>
@ -107,7 +107,7 @@
*/
/*!
\fn qreal qAcos(qreal v)
\fn template <typename T> 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 <typename T> 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 <typename T> 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 <typename T1, typename T2> 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 <typename T> 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 <typename T> auto qLn(T v)
Returns the natural logarithm of \a v. Natural logarithm uses base e.
\relates <QtMath>
@ -160,7 +160,7 @@
*/
/*!
\fn qreal qExp(qreal v)
\fn template <typename T> auto qExp(T v)
Returns the exponential function of \c e to the power of \a v.
\relates <QtMath>
@ -168,7 +168,7 @@
*/
/*!
\fn qreal qPow(qreal x, qreal y)
\fn template <typename T1, typename T2> 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.