1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
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.
48 template <const bool isDouble>
49 AbstractFloat<isDouble>::AbstractFloat(const xsDouble num) : m_value(num)
53 template <const bool isDouble>
54 Numeric::Ptr AbstractFloat<isDouble>::fromValue(const xsDouble num)
56 return Numeric::Ptr(new AbstractFloat<isDouble>(num));
59 template <const bool isDouble>
60 AtomicValue::Ptr AbstractFloat<isDouble>::fromLexical(const QString &strNumeric)
62 /* QString::toDouble() handles the whitespace facet. */
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;
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"))
78 return ValidationError::createError();
81 bool conversionOk = false;
82 const xsDouble num = strNumeric.toDouble(&conversionOk);
85 return AtomicValue::Ptr(new AbstractFloat<isDouble>(num));
87 return ValidationError::createError();
90 template <const bool isDouble>
91 int AbstractFloat<isDouble>::internalSignbit(const xsDouble num)
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.");
102 value.asDouble = num;
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
106 if(sizeof(xsDouble) == 8)
107 return value.asInt >> 63;
109 return value.asInt >> 31;
112 template <const bool isDouble>
113 bool AbstractFloat<isDouble>::isEqual(const xsDouble a, const xsDouble b)
116 return qIsInf(b) && internalSignbit(a) == internalSignbit(b);
118 return qIsInf(a) && internalSignbit(a) == internalSignbit(b);
121 /* Preferably, 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
125 return qAbs(a - b) <= 2.2204460492503131e-16 * qAbs(a);
129 template <const bool isDouble>
130 bool AbstractFloat<isDouble>::isZero() const
132 return AbstractFloat<isDouble>::isEqual(m_value, 0.0);
135 template <const bool isDouble>
136 bool AbstractFloat<isDouble>::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const
138 if(isZero() || qIsNaN(m_value))
144 template <const bool isDouble>
145 QString AbstractFloat<isDouble>::stringValue() const
148 return QLatin1String("NaN");
149 else if(qIsInf(m_value))
150 return internalSignbit(m_value) == 0 ? QLatin1String("INF") : QLatin1String("-INF");
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.
157 else if(0.000001 <= qAbs(m_value) && qAbs(m_value) < 1000000.0)
158 return Decimal::toString(toDecimal());
160 * If SV has the value positive or negative zero, TV is "0" or "-0" respectively.
163 return internalSignbit(m_value) == 0 ? QLatin1String("0") : QLatin1String("-0");
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.
174 static_cast<void>(qdtoa(m_value, -1, 0, &decimalPoint, &sign, 0, &result));
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));
180 /* We use free() instead of delete here, because qlocale.cpp use malloc(). Spotted
184 QString valueAsString;
187 valueAsString += QLatin1Char('-');
189 valueAsString += qret.at(0);
190 valueAsString += QLatin1Char('.');
193 valueAsString += QLatin1Char('0');
195 valueAsString += qret.mid(1);
197 valueAsString += QLatin1Char('E');
199 valueAsString += QString::number(decimalPoint);
200 return valueAsString;
204 template <const bool isDouble>
205 xsDouble AbstractFloat<isDouble>::toDouble() const
210 template <const bool isDouble>
211 xsInteger AbstractFloat<isDouble>::toInteger() const
213 return static_cast<xsInteger>(m_value);
216 template <const bool isDouble>
217 xsFloat AbstractFloat<isDouble>::toFloat() const
219 /* No cast, since xsFloat and xsDouble are typedef'ed with the same type. */
223 template <const bool isDouble>
224 xsDecimal AbstractFloat<isDouble>::toDecimal() const
226 return static_cast<xsDecimal>(m_value);
229 template <const bool isDouble>
230 Numeric::Ptr AbstractFloat<isDouble>::round() const
232 return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(roundFloat(m_value)));
235 template <const bool isDouble>
236 Numeric::Ptr AbstractFloat<isDouble>::roundHalfToEven(const xsInteger precision) const
238 if(isNaN() || isInf() || isZero())
239 return Numeric::Ptr(const_cast<AbstractFloat<isDouble> *>(this));
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;
247 if(val - 0.5 == ::floor(val))
250 val = m_value * powered + 0.5;
253 if(isHalf /*&& isOdd(val) or? TODO */)
258 return fromValue(val);
262 template <const bool isDouble>
263 Numeric::Ptr AbstractFloat<isDouble>::floor() const
265 return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(::floor(m_value)));
268 template <const bool isDouble>
269 Numeric::Ptr AbstractFloat<isDouble>::ceiling() const
271 return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(ceil(m_value)));
274 template <const bool isDouble>
275 Numeric::Ptr AbstractFloat<isDouble>::abs() const
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)));
282 template <const bool isDouble>
283 bool AbstractFloat<isDouble>::isNaN() const
285 return qIsNaN(m_value);
288 template <const bool isDouble>
289 bool AbstractFloat<isDouble>::isInf() const
291 return qIsInf(m_value);
294 template <const bool isDouble>
295 ItemType::Ptr AbstractFloat<isDouble>::type() const
297 return isDouble ? BuiltinTypes::xsDouble : BuiltinTypes::xsFloat;
300 template <const bool isDouble>
301 Item AbstractFloat<isDouble>::toNegated() const
303 return fromValue(-m_value).data();
306 template <const bool isDouble>
307 bool AbstractFloat<isDouble>::isSigned() const
309 Q_ASSERT_X(false, Q_FUNC_INFO,
310 "It makes no sense to call this function, see Numeric::isSigned().");
314 template <const bool isDouble>
315 qulonglong AbstractFloat<isDouble>::toUnsignedInteger() const
317 Q_ASSERT_X(false, Q_FUNC_INFO,
318 "It makes no sense to call this function, see Numeric::toUnsignedInteger().");