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