Add degree<->radians conversions for long double and integral types

For long double: they're just missing, so add them for completeness.

For integral types: follow the advice of C11's trigonometric
functions; first convert the angle to double, then do the actual
conversion. This is offered only for the degree->radians conversions,
as someone may legitimately want to call e.g. qDegreesToRadians(90).

On the other hand, it seems extremely unlikely that someone may
want to do a radians->degree conversion starting from integral
datatypes, so I'm not adding it for the moment being (instead,
I'm leaving a note).

[ChangeLog][QtCore][QtMath] qDegreesToRadians now also accepts
long double and integral types. A value of integral type will be
casted to double before the conversion to radians.

[ChangeLog][QtCore][QtMath] qRadiansToDegrees now also accepts
long double.

Change-Id: Ib1576be5193ae09bb6cb4a70d7a31702955df2c5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2020-08-10 16:21:16 +02:00
parent 20db9a0d7f
commit d7205a7cae
2 changed files with 54 additions and 0 deletions

View File

@ -233,6 +233,17 @@ Q_DECL_CONSTEXPR inline double qDegreesToRadians(double degrees)
return degrees * (M_PI / 180);
}
Q_DECL_CONSTEXPR inline long double qDegreesToRadians(long double degrees)
{
return degrees * (M_PI / 180);
}
template <typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
Q_DECL_CONSTEXPR inline double qDegreesToRadians(T degrees)
{
return qDegreesToRadians(static_cast<double>(degrees));
}
Q_DECL_CONSTEXPR inline float qRadiansToDegrees(float radians)
{
return radians * float(180/M_PI);
@ -243,6 +254,15 @@ Q_DECL_CONSTEXPR inline double qRadiansToDegrees(double radians)
return radians * (180 / M_PI);
}
Q_DECL_CONSTEXPR inline long double qRadiansToDegrees(long double radians)
{
return radians * (180 / M_PI);
}
// A qRadiansToDegrees(Integral) overload isn't here; it's extremely
// questionable that someone is manipulating quantities in radians
// using integral datatypes...
namespace QtPrivate {
constexpr inline quint32 qConstexprNextPowerOfTwo(quint32 v) {
v |= v >> 1;

View File

@ -204,6 +204,30 @@
\sa qRadiansToDegrees()
*/
/*!
\fn long double qDegreesToRadians(long double degrees)
\relates <QtMath>
\since 6.0
This function converts the \a degrees in double to radians.
\sa qRadiansToDegrees()
*/
/*!
\fn template <typename Integral> double qDegreesToRadians(Integral degrees)
\relates <QtMath>
\since 6.0
This function converts the \a degrees in double to radians;
the angle is casted to a double before the conversion.
This function participates in overload resolution if and only if
\c Integral is an integral type.
\sa qRadiansToDegrees()
*/
/*!
\fn float qRadiansToDegrees(float radians)
\relates <QtMath>
@ -232,6 +256,16 @@
\sa qDegreesToRadians()
*/
/*!
\fn long double qRadiansToDegrees(long double radians)
\relates <QtMath>
\since 6.0
This function converts the \a radians in double to degrees.
\sa qDegreesToRadians()
*/
/*!
\fn quint32 qNextPowerOfTwo(quint32 value)
\relates <QtMath>