0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
/**
|
|
43 |
* @file
|
|
44 |
* @short This file is included by qabstractfloat_p.h.
|
|
45 |
* If you need includes in this file, put them in qabstractfloat_p.h, outside of the namespace.
|
|
46 |
*/
|
|
47 |
|
|
48 |
template <const bool isDouble>
|
|
49 |
AbstractFloat<isDouble>::AbstractFloat(const xsDouble num) : m_value(num)
|
|
50 |
{
|
|
51 |
}
|
|
52 |
|
|
53 |
template <const bool isDouble>
|
|
54 |
Numeric::Ptr AbstractFloat<isDouble>::fromValue(const xsDouble num)
|
|
55 |
{
|
|
56 |
return Numeric::Ptr(new AbstractFloat<isDouble>(num));
|
|
57 |
}
|
|
58 |
|
|
59 |
template <const bool isDouble>
|
|
60 |
AtomicValue::Ptr AbstractFloat<isDouble>::fromLexical(const QString &strNumeric)
|
|
61 |
{
|
|
62 |
/* QString::toDouble() handles the whitespace facet. */
|
|
63 |
|
|
64 |
if(strNumeric == QLatin1String("NaN"))
|
|
65 |
return isDouble ? CommonValues::DoubleNaN : CommonValues::FloatNaN;
|
|
66 |
else if(strNumeric == QLatin1String("-INF"))
|
|
67 |
return isDouble ? CommonValues::NegativeInfDouble : CommonValues::NegativeInfFloat;
|
|
68 |
else if(strNumeric == QLatin1String("INF"))
|
|
69 |
return isDouble ? CommonValues::InfDouble : CommonValues::InfFloat;
|
|
70 |
|
|
71 |
/* QString::toDouble() supports any case as well as +INF, but we don't. */
|
|
72 |
const QString toUpper(strNumeric.toUpper());
|
|
73 |
if(toUpper == QLatin1String("-INF") ||
|
|
74 |
toUpper == QLatin1String("INF") ||
|
|
75 |
toUpper == QLatin1String("+INF") ||
|
|
76 |
toUpper == QLatin1String("NAN"))
|
|
77 |
{
|
|
78 |
return ValidationError::createError();
|
|
79 |
}
|
|
80 |
|
|
81 |
bool conversionOk = false;
|
|
82 |
const xsDouble num = strNumeric.toDouble(&conversionOk);
|
|
83 |
|
|
84 |
if(conversionOk)
|
|
85 |
return AtomicValue::Ptr(new AbstractFloat<isDouble>(num));
|
|
86 |
else
|
|
87 |
return ValidationError::createError();
|
|
88 |
}
|
|
89 |
|
|
90 |
template <const bool isDouble>
|
|
91 |
int AbstractFloat<isDouble>::internalSignbit(const xsDouble num)
|
|
92 |
{
|
|
93 |
Q_ASSERT_X(sizeof(xsDouble) == 8 || sizeof(xsDouble) == 4, Q_FUNC_INFO,
|
|
94 |
"This implementation of signbit assumes xsDouble, that is qreal, is 64 bits large.");
|
|
95 |
|
|
96 |
union
|
|
97 |
{
|
|
98 |
xsDouble asDouble;
|
|
99 |
qint64 asInt;
|
|
100 |
} value;
|
|
101 |
|
|
102 |
value.asDouble = num;
|
|
103 |
|
|
104 |
/* 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
|
|
105 |
* only one left. */
|
|
106 |
if(sizeof(xsDouble) == 8)
|
|
107 |
return value.asInt >> 63;
|
|
108 |
else
|
|
109 |
return value.asInt >> 31;
|
|
110 |
}
|
|
111 |
|
|
112 |
template <const bool isDouble>
|
|
113 |
bool AbstractFloat<isDouble>::isEqual(const xsDouble a, const xsDouble b)
|
|
114 |
{
|
|
115 |
if(qIsInf(a))
|
|
116 |
return qIsInf(b) && internalSignbit(a) == internalSignbit(b);
|
|
117 |
else if(qIsInf(b))
|
|
118 |
return qIsInf(a) && internalSignbit(a) == internalSignbit(b);
|
|
119 |
else
|
|
120 |
{
|
|
121 |
/* Preferrably, we would use std::numeric_limits<xsDouble>::espilon(), but
|
|
122 |
* we cannot since we cannot depend on the STL. The small xs:double value below,
|
|
123 |
* was extracted by printing the std::numeric_limits<xsDouble>::epsilon() using
|
|
124 |
* gdb. */
|
|
125 |
return qAbs(a - b) <= 2.2204460492503131e-16 * qAbs(a);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
template <const bool isDouble>
|
|
130 |
bool AbstractFloat<isDouble>::isZero() const
|
|
131 |
{
|
|
132 |
return AbstractFloat<isDouble>::isEqual(m_value, 0.0);
|
|
133 |
}
|
|
134 |
|
|
135 |
template <const bool isDouble>
|
|
136 |
bool AbstractFloat<isDouble>::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const
|
|
137 |
{
|
|
138 |
if(isZero() || qIsNaN(m_value))
|
|
139 |
return false;
|
|
140 |
else
|
|
141 |
return true;
|
|
142 |
}
|
|
143 |
|
|
144 |
template <const bool isDouble>
|
|
145 |
QString AbstractFloat<isDouble>::stringValue() const
|
|
146 |
{
|
|
147 |
if(qIsNaN(m_value))
|
|
148 |
return QLatin1String("NaN");
|
|
149 |
else if(qIsInf(m_value))
|
|
150 |
return internalSignbit(m_value) == 0 ? QLatin1String("INF") : QLatin1String("-INF");
|
|
151 |
/*
|
|
152 |
* If SV has an absolute value that is greater than or equal to 0.000001
|
|
153 |
* (one millionth) and less than 1000000 (one million),
|
|
154 |
* then the value is converted to an xs:decimal and the resulting xs:decimal
|
|
155 |
* is converted to an xs:string according to the rules above.
|
|
156 |
*/
|
|
157 |
else if(0.000001 <= qAbs(m_value) && qAbs(m_value) < 1000000.0)
|
|
158 |
return Decimal::toString(toDecimal());
|
|
159 |
/*
|
|
160 |
* If SV has the value positive or negative zero, TV is "0" or "-0" respectively.
|
|
161 |
*/
|
|
162 |
else if(isZero())
|
|
163 |
return internalSignbit(m_value) == 0 ? QLatin1String("0") : QLatin1String("-0");
|
|
164 |
else
|
|
165 |
{
|
|
166 |
/*
|
|
167 |
* Besides these special values, the general form of the canonical form for
|
|
168 |
* xs:float and xs:double is a mantissa, which is a xs:decimal, followed by
|
|
169 |
* the letter "E", followed by an exponent which is an xs:integer.
|
|
170 |
*/
|
|
171 |
int sign;
|
|
172 |
int decimalPoint;
|
|
173 |
char *result = 0;
|
|
174 |
static_cast<void>(qdtoa(m_value, -1, 0, &decimalPoint, &sign, 0, &result));
|
|
175 |
|
|
176 |
/* If the copy constructor is used instead of QString::operator=(),
|
|
177 |
* it doesn't compile. I have no idea why. */
|
|
178 |
const QString qret(QString::fromLatin1(result));
|
|
179 |
|
|
180 |
/* We use free() instead of delete here, because qlocale.cpp use malloc(). Spotted
|
|
181 |
* by valgrind. */
|
|
182 |
free(result);
|
|
183 |
|
|
184 |
QString valueAsString;
|
|
185 |
|
|
186 |
if(sign)
|
|
187 |
valueAsString += QLatin1Char('-');
|
|
188 |
|
|
189 |
valueAsString += qret.at(0);
|
|
190 |
valueAsString += QLatin1Char('.');
|
|
191 |
|
|
192 |
if(1 == qret.size())
|
|
193 |
valueAsString += QLatin1Char('0');
|
|
194 |
else
|
|
195 |
valueAsString += qret.mid(1);
|
|
196 |
|
|
197 |
valueAsString += QLatin1Char('E');
|
|
198 |
decimalPoint--;
|
|
199 |
valueAsString += QString::number(decimalPoint);
|
|
200 |
return valueAsString;
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
template <const bool isDouble>
|
|
205 |
xsDouble AbstractFloat<isDouble>::toDouble() const
|
|
206 |
{
|
|
207 |
return m_value;
|
|
208 |
}
|
|
209 |
|
|
210 |
template <const bool isDouble>
|
|
211 |
xsInteger AbstractFloat<isDouble>::toInteger() const
|
|
212 |
{
|
|
213 |
return static_cast<xsInteger>(m_value);
|
|
214 |
}
|
|
215 |
|
|
216 |
template <const bool isDouble>
|
|
217 |
xsFloat AbstractFloat<isDouble>::toFloat() const
|
|
218 |
{
|
|
219 |
/* No cast, since xsFloat and xsDouble are typedef'ed with the same type. */
|
|
220 |
return m_value;
|
|
221 |
}
|
|
222 |
|
|
223 |
template <const bool isDouble>
|
|
224 |
xsDecimal AbstractFloat<isDouble>::toDecimal() const
|
|
225 |
{
|
|
226 |
return static_cast<xsDecimal>(m_value);
|
|
227 |
}
|
|
228 |
|
|
229 |
template <const bool isDouble>
|
|
230 |
Numeric::Ptr AbstractFloat<isDouble>::round() const
|
|
231 |
{
|
|
232 |
return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(roundFloat(m_value)));
|
|
233 |
}
|
|
234 |
|
|
235 |
template <const bool isDouble>
|
|
236 |
Numeric::Ptr AbstractFloat<isDouble>::roundHalfToEven(const xsInteger precision) const
|
|
237 |
{
|
|
238 |
if(isNaN() || isInf() || isZero())
|
|
239 |
return Numeric::Ptr(const_cast<AbstractFloat<isDouble> *>(this));
|
|
240 |
else
|
|
241 |
{
|
|
242 |
/* The cast to double helps finding the correct pow() version on irix-cc. */
|
|
243 |
const xsDouble powered = pow(double(10), double(precision));
|
|
244 |
xsDouble val = powered * m_value;
|
|
245 |
bool isHalf = false;
|
|
246 |
|
|
247 |
if(val - 0.5 == ::floor(val))
|
|
248 |
isHalf = true;
|
|
249 |
|
|
250 |
val = m_value * powered + 0.5;
|
|
251 |
val = ::floor(val);
|
|
252 |
|
|
253 |
if(isHalf /*&& isOdd(val) or? TODO */)
|
|
254 |
val -= 1;
|
|
255 |
|
|
256 |
val /= powered;
|
|
257 |
|
|
258 |
return fromValue(val);
|
|
259 |
}
|
|
260 |
}
|
|
261 |
|
|
262 |
template <const bool isDouble>
|
|
263 |
Numeric::Ptr AbstractFloat<isDouble>::floor() const
|
|
264 |
{
|
|
265 |
return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(::floor(m_value)));
|
|
266 |
}
|
|
267 |
|
|
268 |
template <const bool isDouble>
|
|
269 |
Numeric::Ptr AbstractFloat<isDouble>::ceiling() const
|
|
270 |
{
|
|
271 |
return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(ceil(m_value)));
|
|
272 |
}
|
|
273 |
|
|
274 |
template <const bool isDouble>
|
|
275 |
Numeric::Ptr AbstractFloat<isDouble>::abs() const
|
|
276 |
{
|
|
277 |
/* We must use fabs() instead of qAbs() because qAbs()
|
|
278 |
* doesn't return 0 for -0.0. */
|
|
279 |
return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(fabs(m_value)));
|
|
280 |
}
|
|
281 |
|
|
282 |
template <const bool isDouble>
|
|
283 |
bool AbstractFloat<isDouble>::isNaN() const
|
|
284 |
{
|
|
285 |
return qIsNaN(m_value);
|
|
286 |
}
|
|
287 |
|
|
288 |
template <const bool isDouble>
|
|
289 |
bool AbstractFloat<isDouble>::isInf() const
|
|
290 |
{
|
|
291 |
return qIsInf(m_value);
|
|
292 |
}
|
|
293 |
|
|
294 |
template <const bool isDouble>
|
|
295 |
ItemType::Ptr AbstractFloat<isDouble>::type() const
|
|
296 |
{
|
|
297 |
return isDouble ? BuiltinTypes::xsDouble : BuiltinTypes::xsFloat;
|
|
298 |
}
|
|
299 |
|
|
300 |
template <const bool isDouble>
|
|
301 |
Item AbstractFloat<isDouble>::toNegated() const
|
|
302 |
{
|
|
303 |
return fromValue(-m_value).data();
|
|
304 |
}
|
|
305 |
|
|
306 |
template <const bool isDouble>
|
|
307 |
bool AbstractFloat<isDouble>::isSigned() const
|
|
308 |
{
|
|
309 |
Q_ASSERT_X(false, Q_FUNC_INFO,
|
|
310 |
"It makes no sense to call this function, see Numeric::isSigned().");
|
|
311 |
return false;
|
|
312 |
}
|
|
313 |
|
|
314 |
template <const bool isDouble>
|
|
315 |
qulonglong AbstractFloat<isDouble>::toUnsignedInteger() const
|
|
316 |
{
|
|
317 |
Q_ASSERT_X(false, Q_FUNC_INFO,
|
|
318 |
"It makes no sense to call this function, see Numeric::toUnsignedInteger().");
|
|
319 |
return 0;
|
|
320 |
}
|
|
321 |
|