aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4mathobject.cpp
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2020-12-07 11:54:53 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2021-02-09 13:35:43 +0100
commit06926f88be6876e1cc82b1c6f11d47951d1b75b7 (patch)
tree9b3791f9e1dcf8e14aa706b59b8ad93f0f661015 /src/qml/jsruntime/qv4mathobject.cpp
parent75eb6fe06d707e91210c8795b7809dfcc723fa5c (diff)
Reimplement Math.hypot() using QHypotHelper
As part of adding qHypot() in qtbase, we now have a robust efficient implementation of arbitrarily-many-parameter hypot(), so reuse it. Change-Id: Iafd0eee84f3271c37003431a5f2b1a62d7ef6f86 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4mathobject.cpp')
-rw-r--r--src/qml/jsruntime/qv4mathobject.cpp27
1 files changed, 6 insertions, 21 deletions
diff --git a/src/qml/jsruntime/qv4mathobject.cpp b/src/qml/jsruntime/qv4mathobject.cpp
index 382185657e..8627c0a5af 100644
--- a/src/qml/jsruntime/qv4mathobject.cpp
+++ b/src/qml/jsruntime/qv4mathobject.cpp
@@ -310,29 +310,14 @@ ReturnedValue MathObject::method_hypot(const FunctionObject *, const Value *, co
{
// ES6 Math.hypot(v1, ..., vn) -> sqrt(sum(vi**2)) but "should take care to
// avoid the loss of precision from overflows and underflows" (as std::hypot does).
- double v = argc ? argv[0].toNumber() : 0;
+ double v = 0;
// Spec mandates +0 on no args; and says nothing about what to do if toNumber() signals ...
-#ifdef Q_OS_ANDROID // incomplete std :-(
- bool big = qt_is_inf(v), bad = std::isnan(v);
- v *= v;
- for (int i = 1; !big && i < argc; i++) {
- double u = argv[i].toNumber();
- if (qt_is_inf(u))
- big = true;
- if (std::isnan(u))
- bad = true;
- v += u * u;
+ if (argc > 0) {
+ QtPrivate::QHypotHelper<double> h(argv[0].toNumber());
+ for (int i = 1; i < argc; i++)
+ h = h.add(argv[i].toNumber());
+ v = h.result();
}
- if (big)
- RETURN_RESULT(Encode(qt_inf()));
- if (bad)
- RETURN_RESULT(Encode(qt_qnan()));
- // Should actually check for {und,ov}erflow, but too fiddly !
- RETURN_RESULT(Value::fromDouble(sqrt(v)));
-#else
- for (int i = 1; i < argc; i++)
- v = std::hypot(v, argv[i].toNumber());
-#endif
RETURN_RESULT(Value::fromDouble(v));
}