| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/global/qnumeric.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||
| 2 | ** | - | ||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||
| 5 | ** | - | ||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
| 7 | ** | - | ||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||
| 9 | ** Commercial License Usage | - | ||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
| 14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||
| 16 | ** | - | ||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||
| 25 | ** | - | ||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||
| 29 | ** | - | ||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||
| 31 | ** | - | ||||||
| 32 | ****************************************************************************/ | - | ||||||
| 33 | - | |||||||
| 34 | #include "qnumeric.h" | - | ||||||
| 35 | #include "qnumeric_p.h" | - | ||||||
| 36 | #include <string.h> | - | ||||||
| 37 | - | |||||||
| 38 | QT_BEGIN_NAMESPACE | - | ||||||
| 39 | - | |||||||
| 40 | /*! | - | ||||||
| 41 | Returns \c true if the double \a {d} is equivalent to infinity. | - | ||||||
| 42 | \relates <QtGlobal> | - | ||||||
| 43 | */ | - | ||||||
| 44 | Q_CORE_EXPORT bool qIsInf(double d) { return qt_is_inf(d); } executed 785 times by 66 tests: return qt_is_inf(d);Executed by:
| 785 | ||||||
| 45 | - | |||||||
| 46 | /*! | - | ||||||
| 47 | Returns \c true if the double \a {d} is not a number (NaN). | - | ||||||
| 48 | \relates <QtGlobal> | - | ||||||
| 49 | */ | - | ||||||
| 50 | Q_CORE_EXPORT bool qIsNaN(double d) { return qt_is_nan(d); } executed 946549 times by 129 tests: return qt_is_nan(d);Executed by:
| 946549 | ||||||
| 51 | - | |||||||
| 52 | /*! | - | ||||||
| 53 | Returns \c true if the double \a {d} is a finite number. | - | ||||||
| 54 | \relates <QtGlobal> | - | ||||||
| 55 | */ | - | ||||||
| 56 | Q_CORE_EXPORT bool qIsFinite(double d) { return qt_is_finite(d); } executed 44 times by 1 test: return qt_is_finite(d);Executed by:
| 44 | ||||||
| 57 | - | |||||||
| 58 | /*! | - | ||||||
| 59 | Returns \c true if the float \a {f} is equivalent to infinity. | - | ||||||
| 60 | \relates <QtGlobal> | - | ||||||
| 61 | */ | - | ||||||
| 62 | Q_CORE_EXPORT bool qIsInf(float f) { return qt_is_inf(f); } executed 11 times by 2 tests: return qt_is_inf(f);Executed by:
| 11 | ||||||
| 63 | - | |||||||
| 64 | /*! | - | ||||||
| 65 | Returns \c true if the float \a {f} is not a number (NaN). | - | ||||||
| 66 | \relates <QtGlobal> | - | ||||||
| 67 | */ | - | ||||||
| 68 | Q_CORE_EXPORT bool qIsNaN(float f) { return qt_is_nan(f); } executed 18 times by 2 tests: return qt_is_nan(f);Executed by:
| 18 | ||||||
| 69 | - | |||||||
| 70 | /*! | - | ||||||
| 71 | Returns \c true if the float \a {f} is a finite number. | - | ||||||
| 72 | \relates <QtGlobal> | - | ||||||
| 73 | */ | - | ||||||
| 74 | Q_CORE_EXPORT bool qIsFinite(float f) { return qt_is_finite(f); } executed 44 times by 1 test: return qt_is_finite(f);Executed by:
| 44 | ||||||
| 75 | - | |||||||
| 76 | /*! | - | ||||||
| 77 | Returns the bit pattern of a signalling NaN as a double. | - | ||||||
| 78 | \relates <QtGlobal> | - | ||||||
| 79 | */ | - | ||||||
| 80 | Q_CORE_EXPORT double qSNaN() { return qt_snan(); } executed 21 times by 2 tests: return qt_snan();Executed by:
| 21 | ||||||
| 81 | - | |||||||
| 82 | /*! | - | ||||||
| 83 | Returns the bit pattern of a quiet NaN as a double. | - | ||||||
| 84 | \relates <QtGlobal> | - | ||||||
| 85 | */ | - | ||||||
| 86 | Q_CORE_EXPORT double qQNaN() { return qt_qnan(); } executed 64 times by 6 tests: return qt_qnan();Executed by:
| 64 | ||||||
| 87 | - | |||||||
| 88 | /*! | - | ||||||
| 89 | Returns the bit pattern for an infinite number as a double. | - | ||||||
| 90 | \relates <QtGlobal> | - | ||||||
| 91 | */ | - | ||||||
| 92 | Q_CORE_EXPORT double qInf() { return qt_inf(); } executed 158 times by 9 tests: return qt_inf();Executed by:
| 158 | ||||||
| 93 | - | |||||||
| 94 | - | |||||||
| 95 | - | |||||||
| 96 | /*! | - | ||||||
| 97 | \internal | - | ||||||
| 98 | */ | - | ||||||
| 99 | static inline quint32 f2i(float f) | - | ||||||
| 100 | { | - | ||||||
| 101 | quint32 i; | - | ||||||
| 102 | memcpy(&i, &f, sizeof(f)); | - | ||||||
| 103 | return i; executed 23 times by 1 test: return i;Executed by:
| 23 | ||||||
| 104 | } | - | ||||||
| 105 | - | |||||||
| 106 | /*! | - | ||||||
| 107 | Returns the number of representable floating-point numbers between \a a and \a b. | - | ||||||
| 108 | - | |||||||
| 109 | This function provides an alternative way of doing approximated comparisons of floating-point | - | ||||||
| 110 | numbers similar to qFuzzyCompare(). However, it returns the distance between two numbers, which | - | ||||||
| 111 | gives the caller a possibility to choose the accepted error. Errors are relative, so for | - | ||||||
| 112 | instance the distance between 1.0E-5 and 1.00001E-5 will give 110, while the distance between | - | ||||||
| 113 | 1.0E36 and 1.00001E36 will give 127. | - | ||||||
| 114 | - | |||||||
| 115 | This function is useful if a floating point comparison requires a certain precision. | - | ||||||
| 116 | Therefore, if \a a and \a b are equal it will return 0. The maximum value it will return for 32-bit | - | ||||||
| 117 | floating point numbers is 4,278,190,078. This is the distance between \c{-FLT_MAX} and | - | ||||||
| 118 | \c{+FLT_MAX}. | - | ||||||
| 119 | - | |||||||
| 120 | The function does not give meaningful results if any of the arguments are \c Infinite or \c NaN. | - | ||||||
| 121 | You can check for this by calling qIsFinite(). | - | ||||||
| 122 | - | |||||||
| 123 | The return value can be considered as the "error", so if you for instance want to compare | - | ||||||
| 124 | two 32-bit floating point numbers and all you need is an approximated 24-bit precision, you can | - | ||||||
| 125 | use this function like this: | - | ||||||
| 126 | - | |||||||
| 127 | \code | - | ||||||
| 128 | if (qFloatDistance(a, b) < (1 << 7)) { // The last 7 bits are not | - | ||||||
| 129 | // significant | - | ||||||
| 130 | // precise enough | - | ||||||
| 131 | } | - | ||||||
| 132 | \endcode | - | ||||||
| 133 | - | |||||||
| 134 | \sa qFuzzyCompare() | - | ||||||
| 135 | \since 5.2 | - | ||||||
| 136 | \relates <QtGlobal> | - | ||||||
| 137 | */ | - | ||||||
| 138 | Q_CORE_EXPORT quint32 qFloatDistance(float a, float b) | - | ||||||
| 139 | { | - | ||||||
| 140 | static const quint32 smallestPositiveFloatAsBits = 0x00000001; // denormalized, (SMALLEST), (1.4E-45) | - | ||||||
| 141 | /* Assumes: | - | ||||||
| 142 | * IEE754 format. | - | ||||||
| 143 | * Integers and floats have the same endian | - | ||||||
| 144 | */ | - | ||||||
| 145 | Q_STATIC_ASSERT(sizeof(quint32) == sizeof(float)); | - | ||||||
| 146 | Q_ASSERT(qIsFinite(a) && qIsFinite(b)); | - | ||||||
| 147 | if (a == b)
| 1-21 | ||||||
| 148 | return 0; executed 1 time by 1 test: return 0;Executed by:
| 1 | ||||||
| 149 | if ((a < 0) != (b < 0)) {
| 4-17 | ||||||
| 150 | // if they have different signs | - | ||||||
| 151 | if (a < 0)
| 0-4 | ||||||
| 152 | a = -a; executed 4 times by 1 test: a = -a;Executed by:
| 4 | ||||||
| 153 | else /*if (b < 0)*/ | - | ||||||
| 154 | b = -b; never executed: b = -b; | 0 | ||||||
| 155 | return qFloatDistance(0.0F, a) + qFloatDistance(0.0F, b); executed 4 times by 1 test: return qFloatDistance(0.0F, a) + qFloatDistance(0.0F, b);Executed by:
| 4 | ||||||
| 156 | } | - | ||||||
| 157 | if (a < 0) {
| 2-15 | ||||||
| 158 | a = -a; | - | ||||||
| 159 | b = -b; | - | ||||||
| 160 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||
| 161 | // at this point a and b should not be negative | - | ||||||
| 162 | - | |||||||
| 163 | // 0 is special | - | ||||||
| 164 | if (!a)
| 6-11 | ||||||
| 165 | return f2i(b) - smallestPositiveFloatAsBits + 1; executed 11 times by 1 test: return f2i(b) - smallestPositiveFloatAsBits + 1;Executed by:
| 11 | ||||||
| 166 | if (!b)
| 0-6 | ||||||
| 167 | return f2i(a) - smallestPositiveFloatAsBits + 1; never executed: return f2i(a) - smallestPositiveFloatAsBits + 1; | 0 | ||||||
| 168 | - | |||||||
| 169 | // finally do the common integer subtraction | - | ||||||
| 170 | return a > b ? f2i(a) - f2i(b) : f2i(b) - f2i(a); executed 6 times by 1 test: return a > b ? f2i(a) - f2i(b) : f2i(b) - f2i(a);Executed by:
| 1-6 | ||||||
| 171 | } | - | ||||||
| 172 | - | |||||||
| 173 | - | |||||||
| 174 | /*! | - | ||||||
| 175 | \internal | - | ||||||
| 176 | */ | - | ||||||
| 177 | static inline quint64 d2i(double d) | - | ||||||
| 178 | { | - | ||||||
| 179 | quint64 i; | - | ||||||
| 180 | memcpy(&i, &d, sizeof(d)); | - | ||||||
| 181 | return i; executed 23 times by 1 test: return i;Executed by:
| 23 | ||||||
| 182 | } | - | ||||||
| 183 | - | |||||||
| 184 | /*! | - | ||||||
| 185 | Returns the number of representable floating-point numbers between \a a and \a b. | - | ||||||
| 186 | - | |||||||
| 187 | This function serves the same purpose as \c{qFloatDistance(float, float)}, but | - | ||||||
| 188 | returns the distance between two \c double numbers. Since the range is larger | - | ||||||
| 189 | than for two \c float numbers (\c{[-DBL_MAX,DBL_MAX]}), the return type is quint64. | - | ||||||
| 190 | - | |||||||
| 191 | - | |||||||
| 192 | \sa qFuzzyCompare() | - | ||||||
| 193 | \since 5.2 | - | ||||||
| 194 | \relates <QtGlobal> | - | ||||||
| 195 | */ | - | ||||||
| 196 | Q_CORE_EXPORT quint64 qFloatDistance(double a, double b) | - | ||||||
| 197 | { | - | ||||||
| 198 | static const quint64 smallestPositiveFloatAsBits = 0x1; // denormalized, (SMALLEST) | - | ||||||
| 199 | /* Assumes: | - | ||||||
| 200 | * IEE754 format double precision | - | ||||||
| 201 | * Integers and floats have the same endian | - | ||||||
| 202 | */ | - | ||||||
| 203 | Q_STATIC_ASSERT(sizeof(quint64) == sizeof(double)); | - | ||||||
| 204 | Q_ASSERT(qIsFinite(a) && qIsFinite(b)); | - | ||||||
| 205 | if (a == b)
| 1-21 | ||||||
| 206 | return 0; executed 1 time by 1 test: return 0;Executed by:
| 1 | ||||||
| 207 | if ((a < 0) != (b < 0)) {
| 4-17 | ||||||
| 208 | // if they have different signs | - | ||||||
| 209 | if (a < 0)
| 0-4 | ||||||
| 210 | a = -a; executed 4 times by 1 test: a = -a;Executed by:
| 4 | ||||||
| 211 | else /*if (b < 0)*/ | - | ||||||
| 212 | b = -b; never executed: b = -b; | 0 | ||||||
| 213 | return qFloatDistance(0.0, a) + qFloatDistance(0.0, b); executed 4 times by 1 test: return qFloatDistance(0.0, a) + qFloatDistance(0.0, b);Executed by:
| 4 | ||||||
| 214 | } | - | ||||||
| 215 | if (a < 0) {
| 2-15 | ||||||
| 216 | a = -a; | - | ||||||
| 217 | b = -b; | - | ||||||
| 218 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||
| 219 | // at this point a and b should not be negative | - | ||||||
| 220 | - | |||||||
| 221 | // 0 is special | - | ||||||
| 222 | if (!a)
| 6-11 | ||||||
| 223 | return d2i(b) - smallestPositiveFloatAsBits + 1; executed 11 times by 1 test: return d2i(b) - smallestPositiveFloatAsBits + 1;Executed by:
| 11 | ||||||
| 224 | if (!b)
| 0-6 | ||||||
| 225 | return d2i(a) - smallestPositiveFloatAsBits + 1; never executed: return d2i(a) - smallestPositiveFloatAsBits + 1; | 0 | ||||||
| 226 | - | |||||||
| 227 | // finally do the common integer subtraction | - | ||||||
| 228 | return a > b ? d2i(a) - d2i(b) : d2i(b) - d2i(a); executed 6 times by 1 test: return a > b ? d2i(a) - d2i(b) : d2i(b) - d2i(a);Executed by:
| 1-6 | ||||||
| 229 | } | - | ||||||
| 230 | - | |||||||
| 231 | - | |||||||
| 232 | QT_END_NAMESPACE | - | ||||||
| Source code | Switch to Preprocessed file |