diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2017-02-24 14:40:51 +0100 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-01-27 15:00:57 +0100 |
commit | b08368d99fa41b3f84cf30468c2dc9f5c2c43423 (patch) | |
tree | 9b5f6400423c7f23a547c0a21675a989683c4b7e /src/corelib/tools/qline.cpp | |
parent | ad16f79e5fac13f8a9dab8604de6adb6c46fe7bd (diff) |
Add qHypot() to qmath.h, exposing and extending std::hypot()
We have plenty of places where we add some squares and take a square
root; this may be done more accurately and faster by hypot().
Introduce QHypotHelper to handle hypot with more than 3 parameters,
and with 3 when the C++17 version is missing (which it never should
be). Include an overload taking arbitrarily many valus and ensure that
we can use qHypot() with qfloat16. Illustrate with some example uses,
add some tests.
[ChangeLog][QtCore][QMath] Header <QMath> now provides qHypot(), an
implementation of std::hypot() taking arbitrarily many numeric values,
including support for qfloat16, while avoiding the overflow and
underflow problems that arise when naively taking the square root of a
sum of squares.
Change-Id: Ia4e3913fe83fc27d17d8e7f1a52f03ad445c1fed
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/tools/qline.cpp')
-rw-r--r-- | src/corelib/tools/qline.cpp | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/corelib/tools/qline.cpp b/src/corelib/tools/qline.cpp index 1eb645501c..dc0a6c72fb 100644 --- a/src/corelib/tools/qline.cpp +++ b/src/corelib/tools/qline.cpp @@ -572,8 +572,7 @@ QDataStream &operator>>(QDataStream &stream, QLine &line) */ qreal QLineF::length() const { - using std::hypot; - return hypot(dx(), dy()); + return qHypot(dx(), dy()); } /*! @@ -651,12 +650,11 @@ QLineF QLineF::fromPolar(qreal length, qreal angle) */ QLineF QLineF::unitVector() const { - qreal x = dx(); - qreal y = dy(); - using std::hypot; - qreal len = hypot(x, y); + const qreal x = dx(); + const qreal y = dy(); - QLineF f(p1(), QPointF(pt1.x() + x/len, pt1.y() + y/len)); + const qreal len = qHypot(x, y); + QLineF f(p1(), QPointF(pt1.x() + x / len, pt1.y() + y / len)); #ifndef QT_NO_DEBUG if (qAbs(f.length() - 1) >= 0.001) |