diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2019-02-22 17:13:36 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2019-02-25 12:49:13 +0000 |
commit | b959074afb43d25c1a8818e0528004f8cf073ae2 (patch) | |
tree | 9c00bb615057b51ded5f26795c8b7e02cf706dc0 /src/qml/jsruntime/qv4runtime.cpp | |
parent | cdb8eb988e572411030486ad2834ae54e4567bf3 (diff) |
Unify and fix number to string conversion with radix
Previously, the loop that generated the string could fail to terminate
with certain numbers as input. Also, the algorithm was duplicated in two
places.
Change-Id: Ie2075148d931e7cfcedb5bcd23af61e2e8afc232
Fixes: QTBUG-73999
Reviewed-by: Robert Loehning <robert.loehning@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4runtime.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4runtime.cpp | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp index 13244fdd95..424103cb08 100644 --- a/src/qml/jsruntime/qv4runtime.cpp +++ b/src/qml/jsruntime/qv4runtime.cpp @@ -298,13 +298,22 @@ void RuntimeHelpers::numberToString(QString *result, double num, int radix) if (frac != 0) { result->append(QLatin1Char('.')); + double magnitude = 1; + double next = frac; do { - frac = frac * radix; - char c = (char)::floor(frac); + next *= radix; + const int floored = ::floor(next); + char c = char(floored); c = (c < 10) ? (c + '0') : (c - 10 + 'a'); result->append(QLatin1Char(c)); - frac = frac - ::floor(frac); - } while (frac != 0); + magnitude /= radix; + frac -= double(floored) * magnitude; + next -= double(floored); + + // The next digit still makes a difference + // if a value of "radix" for it would change frac. + // Otherwise we've reached the limit of numerical precision. + } while (frac > 0 && frac - magnitude != frac); } if (negative) |