Qt 4.8
qabstractfloat.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
48 template <const bool isDouble>
50 {
51 }
52 
53 template <const bool isDouble>
55 {
56  return Numeric::Ptr(new AbstractFloat<isDouble>(num));
57 }
58 
59 template <const bool isDouble>
61 {
62  /* QString::toDouble() handles the whitespace facet. */
63 
64  if(strNumeric == QLatin1String("NaN"))
66  else if(strNumeric == QLatin1String("-INF"))
68  else if(strNumeric == QLatin1String("INF"))
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  {
79  }
80 
81  bool conversionOk = false;
82  const xsDouble num = strNumeric.toDouble(&conversionOk);
83 
84  if(conversionOk)
86  else
88 }
89 
90 template <const bool isDouble>
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>
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  /* 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
124  * gdb. */
125  return qAbs(a - b) <= 2.2204460492503131e-16 * qAbs(a);
126  }
127 }
128 
129 template <const bool isDouble>
131 {
133 }
134 
135 template <const bool isDouble>
137 {
138  if(isZero() || qIsNaN(m_value))
139  return false;
140  else
141  return true;
142 }
143 
144 template <const bool isDouble>
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>
206 {
207  return m_value;
208 }
209 
210 template <const bool isDouble>
212 {
213  return static_cast<xsInteger>(m_value);
214 }
215 
216 template <const bool isDouble>
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>
225 {
226  return static_cast<xsDecimal>(m_value);
227 }
228 
229 template <const bool isDouble>
231 {
232  return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(roundFloat(m_value)));
233 }
234 
235 template <const bool isDouble>
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>
264 {
265  return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(::floor(m_value)));
266 }
267 
268 template <const bool isDouble>
270 {
271  return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(ceil(m_value)));
272 }
273 
274 template <const bool isDouble>
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>
284 {
285  return qIsNaN(m_value);
286 }
287 
288 template <const bool isDouble>
290 {
291  return qIsInf(m_value);
292 }
293 
294 template <const bool isDouble>
296 {
297  return isDouble ? BuiltinTypes::xsDouble : BuiltinTypes::xsFloat;
298 }
299 
300 template <const bool isDouble>
302 {
303  return fromValue(-m_value).data();
304 }
305 
306 template <const bool isDouble>
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>
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 
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qstring.cpp:6448
Base template class for Float and Double classes.
virtual xsDecimal toDecimal() const
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
virtual Item toNegated() const
qint64 xsInteger
QString toUpper() const Q_REQUIRED_RESULT
Returns an uppercase copy of the string.
Definition: qstring.cpp:5483
static AtomicValue::Ptr createError(const QString &description=QString(), const ReportContext::ErrorCode=ReportContext::FORG0001)
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
static const AtomicValue::Ptr DoubleNaN
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
virtual bool isSigned() const
Returns true if this value is signed. If false is returned, the value is unsigned.
long ASN1_INTEGER_get ASN1_INTEGER * a
virtual Numeric::Ptr round() const
virtual xsFloat toFloat() const
static const AtomicValue::Ptr FloatNaN
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
xsDouble xsDecimal
virtual Numeric::Ptr roundHalfToEven(const xsInteger scale) const
static const AtomicValue::Ptr NegativeInfFloat
static int internalSignbit(const xsDouble v)
static int sign(int x)
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
virtual Numeric::Ptr floor() const
int size() const
Returns the number of characters in this string.
Definition: qstring.h:102
static AtomicValue::Ptr fromLexical(const QString &strNumeric)
virtual bool isInf() const
virtual qulonglong toUnsignedInteger() const
static const AtomicType::Ptr xsDouble
bool evaluateEBV(const QExplicitlySharedDataPointer< DynamicContext > &) const
virtual bool isNaN() const
virtual QString stringValue() const
__int64 qint64
Definition: qglobal.h:942
#define ceil(x)
Q_CORE_EXPORT bool qIsNaN(double d)
Returns true if the double {d} is not a number (NaN).
Definition: qnumeric.cpp:55
virtual xsDouble toDouble() const
static const AtomicValue::Ptr InfFloat
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
static bool isEqual(const xsDouble a, const xsDouble b)
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
virtual ItemType::Ptr type() const
static const AtomicType::Ptr xsFloat
virtual xsInteger toInteger() const
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
static Numeric::Ptr fromValue(const xsDouble num)
static const AtomicValue::Ptr InfDouble
virtual Numeric::Ptr ceiling() const
xsDouble xsFloat
quint64 qulonglong
Definition: qglobal.h:952
static xsDouble roundFloat(const xsDouble val)
Implements fn:round() for types implemented with floating point.
Q_CORE_EXPORT char * qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
static const AtomicValue::Ptr NegativeInfDouble
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
virtual Numeric::Ptr abs() const
Q_CORE_EXPORT bool qIsInf(double d)
Returns true if the double {d} is equivalent to infinity.
Definition: qnumeric.cpp:50
QExplicitlySharedDataPointer< Numeric > Ptr
static QString toString(const xsDecimal value)
Definition: qdecimal.cpp:99
#define Q_FUNC_INFO
Definition: qglobal.h:1871