summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qline.cpp
diff options
context:
space:
mode:
authormread <qt-info@nokia.com>2011-10-06 14:07:29 (GMT)
committermread <qt-info@nokia.com>2011-10-06 15:38:37 (GMT)
commit51ed77637a6001f66805e8091ce8616d8f2530db (patch)
treef003f4be14bc08c96d57424511812a27cb6a45da /src/corelib/tools/qline.cpp
parentbf69c7e0af67ec877da4fee244386e538fa1c01d (diff)
downloadQt-51ed77637a6001f66805e8091ce8616d8f2530db.zip
Qt-51ed77637a6001f66805e8091ce8616d8f2530db.tar.gz
Qt-51ed77637a6001f66805e8091ce8616d8f2530db.tar.bz2
Removing accidental use of double instead of qreal from QLine
QLine contained a number of places where conversions and calculations were done as double where qreal should have been used instead. This is caused by the use of raw floating point literals. The fix is to construct the literals as qreal. The angle conversions are simplified to reduce the risk of a growing loss of precision from this change. On Symbian, where qreal is float, this gives a 5-10% performance improvement to the affected functions, as tested by a new benchmark test. The auto tests pass, these contain precision tests which indicates that there is no significant loss of precision with this change. On Windows, where qreal is double, this has no significant effect on performance. Task-number: QTBUG-4894 Reviewed-by: Shane Kearns
Diffstat (limited to 'src/corelib/tools/qline.cpp')
-rw-r--r--src/corelib/tools/qline.cpp21
1 files changed, 13 insertions, 8 deletions
diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp
index 0f67652..9c7c243 100644
--- a/src/corelib/tools/qline.cpp
+++ b/src/corelib/tools/qline.cpp
@@ -302,10 +302,15 @@ QDataStream &operator>>(QDataStream &stream, QLine &line)
#endif // QT_NO_DATASTREAM
+inline static qreal q_deg2rad(qreal x)
+{
+ return x * qreal(0.01745329251994329576923690768489); /* pi/180 */
+}
-#ifndef M_2PI
-#define M_2PI 6.28318530717958647692528676655900576
-#endif
+inline static qreal q_rad2deg(qreal x)
+{
+ return x * qreal(57.295779513082320876798154814105); /* 180/pi */
+}
/*!
\class QLineF
@@ -575,7 +580,7 @@ qreal QLineF::angle() const
const qreal dx = pt2.x() - pt1.x();
const qreal dy = pt2.y() - pt1.y();
- const qreal theta = qAtan2(-dy, dx) * 360.0 / M_2PI;
+ const qreal theta = q_rad2deg(qAtan2(-dy, dx));
const qreal theta_normalized = theta < 0 ? theta + 360 : theta;
@@ -599,7 +604,7 @@ qreal QLineF::angle() const
*/
void QLineF::setAngle(qreal angle)
{
- const qreal angleR = angle * M_2PI / 360.0;
+ const qreal angleR = q_deg2rad(angle);
const qreal l = length();
const qreal dx = qCos(angleR) * l;
@@ -621,7 +626,7 @@ void QLineF::setAngle(qreal angle)
*/
QLineF QLineF::fromPolar(qreal length, qreal angle)
{
- const qreal angleR = angle * M_2PI / 360.0;
+ const qreal angleR = q_deg2rad(angle);
return QLineF(0, 0, qCos(angleR) * length, -qSin(angleR) * length);
}
@@ -815,8 +820,8 @@ qreal QLineF::angle(const QLineF &l) const
qreal cos_line = (dx()*l.dx() + dy()*l.dy()) / (length()*l.length());
qreal rad = 0;
// only accept cos_line in the range [-1,1], if it is outside, use 0 (we return 0 rather than PI for those cases)
- if (cos_line >= -1.0 && cos_line <= 1.0) rad = qAcos( cos_line );
- return rad * 360 / M_2PI;
+ if (cos_line >= qreal(-1.0) && cos_line <= qreal(1.0)) rad = qAcos( cos_line );
+ return q_rad2deg(rad);
}