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 QtXmlPatterns 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 | // |
41 | // W A R N I N G |
42 | // ------------- |
43 | // |
44 | // This file is not part of the Qt API. It exists purely as an |
45 | // implementation detail. This header file may change from version to |
46 | // version without notice, or even be removed. |
47 | // |
48 | // We mean it. |
49 | // |
50 | |
51 | /** |
52 | * @file |
53 | * @short This file is included by qabstractfloat_p.h. |
54 | * If you need includes in this file, put them in qabstractfloat_p.h, outside of the namespace. |
55 | */ |
56 | |
57 | template <const bool isDouble> |
58 | AbstractFloat<isDouble>::AbstractFloat(const xsDouble num) : m_value(num) |
59 | { |
60 | } |
61 | |
62 | template <const bool isDouble> |
63 | Numeric::Ptr AbstractFloat<isDouble>::fromValue(const xsDouble num) |
64 | { |
65 | return Numeric::Ptr(new AbstractFloat<isDouble>(num)); |
66 | } |
67 | |
68 | template <const bool isDouble> |
69 | AtomicValue::Ptr AbstractFloat<isDouble>::fromLexical(const QString &strNumeric) |
70 | { |
71 | /* QString::toDouble() handles the whitespace facet. */ |
72 | |
73 | if(strNumeric == QLatin1String("NaN" )) |
74 | return isDouble ? CommonValues::DoubleNaN : CommonValues::FloatNaN; |
75 | else if(strNumeric == QLatin1String("-INF" )) |
76 | return isDouble ? CommonValues::NegativeInfDouble : CommonValues::NegativeInfFloat; |
77 | else if(strNumeric == QLatin1String("INF" )) |
78 | return isDouble ? CommonValues::InfDouble : CommonValues::InfFloat; |
79 | |
80 | /* QString::toDouble() supports any case as well as +INF, but we don't. */ |
81 | const QString toUpper(strNumeric.toUpper()); |
82 | if(toUpper == QLatin1String("-INF" ) || |
83 | toUpper == QLatin1String("INF" ) || |
84 | toUpper == QLatin1String("+INF" ) || |
85 | toUpper == QLatin1String("NAN" )) |
86 | { |
87 | return ValidationError::createError(); |
88 | } |
89 | |
90 | bool conversionOk = false; |
91 | const xsDouble num = strNumeric.toDouble(ok: &conversionOk); |
92 | |
93 | if(conversionOk) |
94 | return AtomicValue::Ptr(new AbstractFloat<isDouble>(num)); |
95 | else |
96 | return ValidationError::createError(); |
97 | } |
98 | |
99 | template <const bool isDouble> |
100 | int AbstractFloat<isDouble>::internalSignbit(const xsDouble num) |
101 | { |
102 | Q_ASSERT_X(sizeof(xsDouble) == 8 || sizeof(xsDouble) == 4, Q_FUNC_INFO, |
103 | "This implementation of signbit assumes xsDouble, that is qreal, is 64 bits large." ); |
104 | |
105 | union |
106 | { |
107 | xsDouble asDouble; |
108 | qint64 asInt; |
109 | } value; |
110 | |
111 | value.asDouble = num; |
112 | |
113 | /* The highest bit, the 64'th for those who have 64bit floats, is the sign bit. So we pull it down until that bit is the |
114 | * only one left. */ |
115 | if(sizeof(xsDouble) == 8) |
116 | return value.asInt >> 63; |
117 | else |
118 | return value.asInt >> 31; |
119 | } |
120 | |
121 | template <const bool isDouble> |
122 | bool AbstractFloat<isDouble>::isEqual(const xsDouble a, const xsDouble b) |
123 | { |
124 | if(qIsInf(d: a)) |
125 | return qIsInf(d: b) && internalSignbit(num: a) == internalSignbit(num: b); |
126 | else if(qIsInf(d: b)) |
127 | return qIsInf(d: a) && internalSignbit(num: a) == internalSignbit(num: b); |
128 | else |
129 | { |
130 | /* Preferably, we would use std::numeric_limits<xsDouble>::espilon(), but |
131 | * we cannot since we cannot depend on the STL. The small xs:double value below, |
132 | * was extracted by printing the std::numeric_limits<xsDouble>::epsilon() using |
133 | * gdb. */ |
134 | return qAbs(t: a - b) <= 2.2204460492503131e-16 * qAbs(t: a); |
135 | } |
136 | } |
137 | |
138 | template <const bool isDouble> |
139 | bool AbstractFloat<isDouble>::isZero() const |
140 | { |
141 | return AbstractFloat<isDouble>::isEqual(a: m_value, b: 0.0); |
142 | } |
143 | |
144 | template <const bool isDouble> |
145 | bool AbstractFloat<isDouble>::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const |
146 | { |
147 | if(isZero() || qIsNaN(d: m_value)) |
148 | return false; |
149 | else |
150 | return true; |
151 | } |
152 | |
153 | template <const bool isDouble> |
154 | QString AbstractFloat<isDouble>::stringValue() const |
155 | { |
156 | if(qIsNaN(d: m_value)) |
157 | return QLatin1String("NaN" ); |
158 | else if(qIsInf(d: m_value)) |
159 | return internalSignbit(num: m_value) == 0 ? QLatin1String("INF" ) : QLatin1String("-INF" ); |
160 | /* |
161 | * If SV has an absolute value that is greater than or equal to 0.000001 |
162 | * (one millionth) and less than 1000000 (one million), |
163 | * then the value is converted to an xs:decimal and the resulting xs:decimal |
164 | * is converted to an xs:string according to the rules above. |
165 | */ |
166 | else if(0.000001 <= qAbs(t: m_value) && qAbs(t: m_value) < 1000000.0) |
167 | return Decimal::toString(value: toDecimal()); |
168 | /* |
169 | * If SV has the value positive or negative zero, TV is "0" or "-0" respectively. |
170 | */ |
171 | else if(isZero()) |
172 | return internalSignbit(num: m_value) == 0 ? QLatin1String("0" ) : QLatin1String("-0" ); |
173 | else |
174 | { |
175 | /* |
176 | * Besides these special values, the general form of the canonical form for |
177 | * xs:float and xs:double is a mantissa, which is a xs:decimal, followed by |
178 | * the letter "E", followed by an exponent which is an xs:integer. |
179 | */ |
180 | int sign; |
181 | int decimalPoint; |
182 | const QString qret = qdtoa(d: m_value, decpt: &decimalPoint, sign: &sign); |
183 | QString valueAsString; |
184 | |
185 | if(sign) |
186 | valueAsString += QLatin1Char('-'); |
187 | |
188 | valueAsString += qret.at(i: 0); |
189 | valueAsString += QLatin1Char('.'); |
190 | |
191 | if(1 == qret.size()) |
192 | valueAsString += QLatin1Char('0'); |
193 | else |
194 | valueAsString += qret.mid(position: 1); |
195 | |
196 | valueAsString += QLatin1Char('E'); |
197 | decimalPoint--; |
198 | valueAsString += QString::number(decimalPoint); |
199 | return valueAsString; |
200 | } |
201 | } |
202 | |
203 | template <const bool isDouble> |
204 | xsDouble AbstractFloat<isDouble>::toDouble() const |
205 | { |
206 | return m_value; |
207 | } |
208 | |
209 | template <const bool isDouble> |
210 | xsInteger AbstractFloat<isDouble>::toInteger() const |
211 | { |
212 | return static_cast<xsInteger>(m_value); |
213 | } |
214 | |
215 | template <const bool isDouble> |
216 | xsFloat AbstractFloat<isDouble>::toFloat() const |
217 | { |
218 | /* No cast, since xsFloat and xsDouble are typedef'ed with the same type. */ |
219 | return m_value; |
220 | } |
221 | |
222 | template <const bool isDouble> |
223 | xsDecimal AbstractFloat<isDouble>::toDecimal() const |
224 | { |
225 | return static_cast<xsDecimal>(m_value); |
226 | } |
227 | |
228 | template <const bool isDouble> |
229 | Numeric::Ptr AbstractFloat<isDouble>::round() const |
230 | { |
231 | return AbstractFloat<isDouble>::fromValue(num: static_cast<xsDouble>(roundFloat(val: m_value))); |
232 | } |
233 | |
234 | template <const bool isDouble> |
235 | Numeric::Ptr AbstractFloat<isDouble>::roundHalfToEven(const xsInteger precision) const |
236 | { |
237 | if(isNaN() || isInf() || isZero()) |
238 | return Numeric::Ptr(const_cast<AbstractFloat<isDouble> *>(this)); |
239 | else |
240 | { |
241 | /* The cast to double helps finding the correct pow() version on irix-cc. */ |
242 | const xsDouble powered = pow(x: double(10), y: double(precision)); |
243 | xsDouble val = powered * m_value; |
244 | bool isHalf = false; |
245 | |
246 | if(val - 0.5 == ::floor(x: val)) |
247 | isHalf = true; |
248 | |
249 | val = m_value * powered + 0.5; |
250 | val = ::floor(x: val); |
251 | |
252 | if(isHalf /*&& isOdd(val) or? TODO */) |
253 | val -= 1; |
254 | |
255 | val /= powered; |
256 | |
257 | return fromValue(num: val); |
258 | } |
259 | } |
260 | |
261 | template <const bool isDouble> |
262 | Numeric::Ptr AbstractFloat<isDouble>::floor() const |
263 | { |
264 | return AbstractFloat<isDouble>::fromValue(num: static_cast<xsDouble>(::floor(x: m_value))); |
265 | } |
266 | |
267 | template <const bool isDouble> |
268 | Numeric::Ptr AbstractFloat<isDouble>::ceiling() const |
269 | { |
270 | return AbstractFloat<isDouble>::fromValue(num: static_cast<xsDouble>(ceil(x: m_value))); |
271 | } |
272 | |
273 | template <const bool isDouble> |
274 | Numeric::Ptr AbstractFloat<isDouble>::abs() const |
275 | { |
276 | /* We must use fabs() instead of qAbs() because qAbs() |
277 | * doesn't return 0 for -0.0. */ |
278 | return AbstractFloat<isDouble>::fromValue(num: static_cast<xsDouble>(fabs(x: m_value))); |
279 | } |
280 | |
281 | template <const bool isDouble> |
282 | bool AbstractFloat<isDouble>::isNaN() const |
283 | { |
284 | return qIsNaN(d: m_value); |
285 | } |
286 | |
287 | template <const bool isDouble> |
288 | bool AbstractFloat<isDouble>::isInf() const |
289 | { |
290 | return qIsInf(d: m_value); |
291 | } |
292 | |
293 | template <const bool isDouble> |
294 | ItemType::Ptr AbstractFloat<isDouble>::type() const |
295 | { |
296 | return isDouble ? BuiltinTypes::xsDouble : BuiltinTypes::xsFloat; |
297 | } |
298 | |
299 | template <const bool isDouble> |
300 | Item AbstractFloat<isDouble>::toNegated() const |
301 | { |
302 | return fromValue(num: -m_value).data(); |
303 | } |
304 | |
305 | template <const bool isDouble> |
306 | bool AbstractFloat<isDouble>::isSigned() const |
307 | { |
308 | Q_ASSERT_X(false, Q_FUNC_INFO, |
309 | "It makes no sense to call this function, see Numeric::isSigned()." ); |
310 | return false; |
311 | } |
312 | |
313 | template <const bool isDouble> |
314 | qulonglong AbstractFloat<isDouble>::toUnsignedInteger() const |
315 | { |
316 | Q_ASSERT_X(false, Q_FUNC_INFO, |
317 | "It makes no sense to call this function, see Numeric::toUnsignedInteger()." ); |
318 | return 0; |
319 | } |
320 | |
321 | |