summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-01-14 11:17:47 +0100
committerAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-02-16 16:21:15 +0000
commit515e802ae20c045e5c47b400ee6ef6e92349c978 (patch)
treef9f4be4c9e360611b5823ef28c2cb293733aee42 /src/corelib
parent3d835eb62e70435fe32318441dc7c10aba3a6fba (diff)
Use C++ <cmath> instead of <math.h>
Including math.h can pollute the default namespace, and break some compilers if cmath versions of the method are declared as using. Switching to C++ math functions also greatly simplifies handling of float qreal as C++ automatically chooses the right method. [ChangeLog][QtCore][QtMath] qmath.h no longer includes math.h, so any sources depending on that indirect inclusion may fail to build. Change-Id: I4d0e331dafba354ec05dc5052e61ef4ff8d387fe Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/doc/snippets/code/doc_src_qiterator.cpp6
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp2
-rw-r--r--src/corelib/kernel/qmath.h113
-rw-r--r--src/corelib/tools/qeasingcurve.cpp14
4 files changed, 40 insertions, 95 deletions
diff --git a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp
index e37212b475..c311db957c 100644
--- a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp
+++ b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp
@@ -257,7 +257,7 @@ while (i.hasNext()) {
QMutableListIterator<double> i(list);
while (i.hasNext()) {
double val = i.next();
- i.setValue(sqrt(val));
+ i.setValue(std::sqrt(val));
}
//! [23]
@@ -266,7 +266,7 @@ while (i.hasNext()) {
QMutableLinkedListIterator<double> i(list);
while (i.hasNext()) {
double val = i.next();
- i.setValue(sqrt(val));
+ i.setValue(std::sqrt(val));
}
//! [24]
@@ -275,7 +275,7 @@ while (i.hasNext()) {
QMutableVectorIterator<double> i(list);
while (i.hasNext()) {
double val = i.next();
- i.setValue(sqrt(val));
+ i.setValue(std::sqrt(val));
}
//! [25]
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp
index 17a252cc1b..b545c2dad8 100644
--- a/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qpoint.cpp
@@ -105,7 +105,7 @@ MyWidget::mouseMoveEvent(QMouseEvent *event)
//! [8]
-double trueLength = sqrt(pow(x(), 2) + pow(y(), 2));
+double trueLength = std::sqrt(std::pow(x(), 2) + std::pow(y(), 2));
//! [8]
diff --git a/src/corelib/kernel/qmath.h b/src/corelib/kernel/qmath.h
index 2395e9ee1c..5cc3ec586e 100644
--- a/src/corelib/kernel/qmath.h
+++ b/src/corelib/kernel/qmath.h
@@ -38,13 +38,11 @@
#pragma qt_class(QtMath)
#endif
-#include <math.h>
-
#include <QtCore/qglobal.h>
-#include <QtCore/qcompilerdetection.h>
-QT_BEGIN_NAMESPACE
+#include <cmath>
+QT_BEGIN_NAMESPACE
#define QT_SINE_TABLE_SIZE 256
@@ -52,139 +50,86 @@ extern Q_CORE_EXPORT const qreal qt_sine_table[QT_SINE_TABLE_SIZE];
inline int qCeil(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return int(ceilf(float(v)));
- else
-#endif
- return int(ceil(v));
+ using std::ceil;
+ return int(ceil(v));
}
inline int qFloor(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return int(floorf(float(v)));
- else
-#endif
- return int(floor(v));
+ using std::floor;
+ return int(floor(v));
}
inline qreal qFabs(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if(sizeof(qreal) == sizeof(float))
- return fabsf(float(v));
- else
-#endif
- return fabs(v);
+ using std::fabs;
+ return fabs(v);
}
inline qreal qSin(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return sinf(float(v));
- else
-#endif
- return sin(v);
+ using std::sin;
+ return sin(v);
}
inline qreal qCos(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return cosf(float(v));
- else
-#endif
- return cos(v);
+ using std::cos;
+ return cos(v);
}
inline qreal qTan(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return tanf(float(v));
- else
-#endif
- return tan(v);
+ using std::tan;
+ return tan(v);
}
inline qreal qAcos(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return acosf(float(v));
- else
-#endif
- return acos(v);
+ using std::acos;
+ return acos(v);
}
inline qreal qAsin(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return asinf(float(v));
- else
-#endif
- return asin(v);
+ using std::asin;
+ return asin(v);
}
inline qreal qAtan(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return atanf(float(v));
- else
-#endif
- return atan(v);
+ using std::atan;
+ return atan(v);
}
inline qreal qAtan2(qreal y, qreal x)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return atan2f(float(y), float(x));
- else
-#endif
- return atan2(y, x);
+ using std::atan2;
+ return atan2(y, x);
}
inline qreal qSqrt(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return sqrtf(float(v));
- else
-#endif
- return sqrt(v);
+ using std::sqrt;
+ return sqrt(v);
}
inline qreal qLn(qreal v)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return logf(float(v));
- else
-#endif
- return log(v);
+ using std::log;
+ return log(v);
}
inline qreal qExp(qreal v)
{
- // only one signature
- // exists, exp(double)
+ using std::exp;
return exp(v);
}
inline qreal qPow(qreal x, qreal y)
{
-#ifdef QT_USE_MATH_H_FLOATS
- if (sizeof(qreal) == sizeof(float))
- return powf(float(x), float(y));
- else
-#endif
- return pow(x, y);
+ using std::pow;
+ return pow(x, y);
}
#ifndef M_E
diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp
index c1d74b3b43..1bd9c5ebb9 100644
--- a/src/corelib/tools/qeasingcurve.cpp
+++ b/src/corelib/tools/qeasingcurve.cpp
@@ -657,7 +657,7 @@ struct BezierEase : public QEasingCurveFunction
qreal static inline _acos(qreal x)
{
- return sqrt(1-x)*(1.5707963267948966192313216916398f + x*(-0.213300989f + x*(0.077980478f + x*-0.02164095f)));
+ return std::sqrt(1-x)*(1.5707963267948966192313216916398f + x*(-0.213300989f + x*(0.077980478f + x*-0.02164095f)));
}
qreal static inline _cos(qreal x) //super fast _cos
@@ -704,8 +704,8 @@ struct BezierEase : public QEasingCurveFunction
//We use approximations instead
const qreal x_squared = x * x;
- const qreal x_plus_one_sqrt = sqrt(1.0 + x);
- const qreal one_minus_x_sqrt = sqrt(1.0 - x);
+ const qreal x_plus_one_sqrt = qSqrt(1.0 + x);
+ const qreal one_minus_x_sqrt = qSqrt(1.0 - x);
//cos(acos(x) / 3)
//s1 = _cos(_acos(x) / 3);
@@ -743,7 +743,7 @@ struct BezierEase : public QEasingCurveFunction
const qreal D = 0.25 * q_squared + p_cubic / 27.0;
if (D >= 0) {
- const qreal D_sqrt = sqrt(D);
+ const qreal D_sqrt = qSqrt(D);
qreal u = _cbrt( -q * 0.5 + D_sqrt);
qreal v = _cbrt( -q * 0.5 - D_sqrt);
qreal z1 = u + v;
@@ -758,13 +758,13 @@ struct BezierEase : public QEasingCurveFunction
}
//casus irreducibilis
- const qreal p_minus_sqrt = sqrt(-p);
+ const qreal p_minus_sqrt = qSqrt(-p);
//const qreal f = sqrt(4.0 / 3.0 * -p);
- const qreal f = sqrt(4.0 / 3.0) * p_minus_sqrt;
+ const qreal f = qSqrt(4.0 / 3.0) * p_minus_sqrt;
//const qreal sqrtP = sqrt(27.0 / -p_cubic);
- const qreal sqrtP = -3.0*sqrt(3.0) / (p_minus_sqrt * p);
+ const qreal sqrtP = -3.0*qSqrt(3.0) / (p_minus_sqrt * p);
const qreal g = -q * 0.5 * sqrtP;