From d7205a7cae869fca0b2feb15fd77aaf99ce9846b Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 10 Aug 2020 16:21:16 +0200 Subject: 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 --- src/corelib/kernel/qmath.h | 20 ++++++++++++++++++++ src/corelib/kernel/qmath.qdoc | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) (limited to 'src') diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h index 57314901e8..e7b6ab9c0e 100644 --- a/src/corelib/kernel/qmath.h +++ b/src/corelib/kernel/qmath.h @@ -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 , bool> = true> +Q_DECL_CONSTEXPR inline double qDegreesToRadians(T degrees) +{ + return qDegreesToRadians(static_cast(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; diff --git a/src/corelib/kernel/qmath.qdoc b/src/corelib/kernel/qmath.qdoc index 82a4ae85a5..b0a52a4a52 100644 --- a/src/corelib/kernel/qmath.qdoc +++ b/src/corelib/kernel/qmath.qdoc @@ -204,6 +204,30 @@ \sa qRadiansToDegrees() */ +/*! + \fn long double qDegreesToRadians(long double degrees) + \relates + \since 6.0 + + This function converts the \a degrees in double to radians. + + \sa qRadiansToDegrees() +*/ + +/*! + \fn template double qDegreesToRadians(Integral degrees) + \relates + \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 @@ -232,6 +256,16 @@ \sa qDegreesToRadians() */ +/*! + \fn long double qRadiansToDegrees(long double radians) + \relates + \since 6.0 + + This function converts the \a radians in double to degrees. + + \sa qDegreesToRadians() +*/ + /*! \fn quint32 qNextPowerOfTwo(quint32 value) \relates -- cgit v1.2.3