Qt 4.8
Public Functions | Static Public Functions | Protected Functions | Private Functions | Static Private Functions | Properties | List of all members
QPatternist::AbstractFloat< isDouble > Class Template Reference

Base template class for Float and Double classes. More...

#include <qabstractfloat_p.h>

Inheritance diagram for QPatternist::AbstractFloat< isDouble >:
QPatternist::Numeric QPatternist::AtomicValue QSharedData QPatternist::CppCastingHelper< AtomicValue >

Public Functions

virtual Numeric::Ptr abs () const
 
virtual Numeric::Ptr ceiling () const
 
bool evaluateEBV (const QExplicitlySharedDataPointer< DynamicContext > &) const
 
virtual Numeric::Ptr floor () const
 
virtual bool isInf () const
 
virtual bool isNaN () const
 
virtual bool isSigned () const
 Returns true if this value is signed. If false is returned, the value is unsigned. More...
 
virtual Numeric::Ptr round () const
 
virtual Numeric::Ptr roundHalfToEven (const xsInteger scale) const
 
virtual QString stringValue () const
 
virtual xsDecimal toDecimal () const
 
virtual xsDouble toDouble () const
 
virtual xsFloat toFloat () const
 
virtual xsInteger toInteger () const
 
virtual Item toNegated () const
 
virtual qulonglong toUnsignedInteger () const
 
virtual ItemType::Ptr type () const
 
- Public Functions inherited from QPatternist::AtomicValue
virtual bool hasError () const
 
virtual ~AtomicValue ()
 
- Public Functions inherited from QSharedData
 QSharedData ()
 Constructs a QSharedData object with a reference count of 0. More...
 
 QSharedData (const QSharedData &)
 Constructs a QSharedData object with reference count 0. More...
 
- Public Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
const TCastTarget * as () const
 
TCastTarget * as ()
 

Static Public Functions

static AtomicValue::Ptr fromLexical (const QString &strNumeric)
 
static Numeric::Ptr fromValue (const xsDouble num)
 
static bool isEqual (const xsDouble a, const xsDouble b)
 
- Static Public Functions inherited from QPatternist::Numeric
static AtomicValue::Ptr fromLexical (const QString &number)
 
- Static Public Functions inherited from QPatternist::AtomicValue
static ItemType::Ptr qtToXDMType (const QXmlItem &item)
 
static QVariant toQt (const AtomicValue *const value)
 
static QVariant toQt (const AtomicValue::Ptr &value)
 
static Item toXDM (const QVariant &value)
 

Protected Functions

 AbstractFloat (const xsDouble num)
 
- Protected Functions inherited from QPatternist::AtomicValue
 AtomicValue ()
 
- Protected Functions inherited from QPatternist::CppCastingHelper< AtomicValue >
 CppCastingHelper ()
 

Private Functions

bool isZero () const
 

Static Private Functions

static int internalSignbit (const xsDouble v)
 

Properties

const xsDouble m_value
 

Additional Inherited Members

- Public Types inherited from QPatternist::Numeric
typedef QExplicitlySharedDataPointer< NumericPtr
 
- Public Types inherited from QPatternist::AtomicValue
typedef QList< AtomicValue::PtrList
 
typedef QExplicitlySharedDataPointer< AtomicValuePtr
 
- Public Variables inherited from QSharedData
QAtomicInt ref
 
- Static Protected Functions inherited from QPatternist::Numeric
static xsDouble roundFloat (const xsDouble val)
 Implements fn:round() for types implemented with floating point. More...
 

Detailed Description

template<const bool isDouble>
class QPatternist::AbstractFloat< isDouble >

Base template class for Float and Double classes.

Author
Vincent Ricard magic.nosp@m.@mag.nosp@m.icnin.nosp@m.ja.o.nosp@m.rg

Definition at line 78 of file qabstractfloat_p.h.

Constructors and Destructors

◆ AbstractFloat()

template<const bool isDouble>
QPatternist::AbstractFloat< isDouble >::AbstractFloat ( const xsDouble  num)
protected

Definition at line 49 of file qabstractfloat.cpp.

49  : m_value(num)
50 {
51 }

Functions

◆ abs()

template<const bool isDouble>
Numeric::Ptr QPatternist::AbstractFloat< isDouble >::abs ( ) const
virtual

Performs the algorithm specified for the function fn:abs on this Numeric, and whose result is returned.

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 6.4.1 fn:abs

Implements QPatternist::Numeric.

Definition at line 275 of file qabstractfloat.cpp.

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 }
static Numeric::Ptr fromValue(const xsDouble num)

◆ ceiling()

template<const bool isDouble>
Numeric::Ptr QPatternist::AbstractFloat< isDouble >::ceiling ( ) const
virtual

Performs the algorithm specified for the function fn:ceiling on this Numeric, and whose result is returned.

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 6.4.2 fn:ceiling

Implements QPatternist::Numeric.

Definition at line 269 of file qabstractfloat.cpp.

270 {
271  return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(ceil(m_value)));
272 }
#define ceil(x)
static Numeric::Ptr fromValue(const xsDouble num)

◆ evaluateEBV()

template<const bool isDouble>
bool QPatternist::AbstractFloat< isDouble >::evaluateEBV ( const QExplicitlySharedDataPointer< DynamicContext > &  ) const
virtual

Determines the Effective Boolean Value of this number.

Returns
false if the number is 0 or NaN, otherwise true.

Reimplemented from QPatternist::AtomicValue.

Definition at line 136 of file qabstractfloat.cpp.

137 {
138  if(isZero() || qIsNaN(m_value))
139  return false;
140  else
141  return true;
142 }
Q_CORE_EXPORT bool qIsNaN(double d)
Returns true if the double {d} is not a number (NaN).
Definition: qnumeric.cpp:55

◆ floor()

template<const bool isDouble>
Numeric::Ptr QPatternist::AbstractFloat< isDouble >::floor ( ) const
virtual

Performs the algorithm specified for the function fn:floor on this Numeric, and whose result is returned.

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 6.4.3 fn:floor

Implements QPatternist::Numeric.

Definition at line 263 of file qabstractfloat.cpp.

Referenced by QPatternist::AbstractFloat< isDouble >::roundHalfToEven().

264 {
265  return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(::floor(m_value)));
266 }
virtual Numeric::Ptr floor() const
static Numeric::Ptr fromValue(const xsDouble num)

◆ fromLexical()

template<const bool isDouble>
AtomicValue::Ptr QPatternist::AbstractFloat< isDouble >::fromLexical ( const QString strNumeric)
static

Definition at line 60 of file qabstractfloat.cpp.

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)
85  return AtomicValue::Ptr(new AbstractFloat<isDouble>(num));
86  else
88 }
QExplicitlySharedDataPointer< AtomicValue > Ptr
Definition: qitem_p.h:127
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)
static const AtomicValue::Ptr DoubleNaN
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static const AtomicValue::Ptr FloatNaN
The QString class provides a Unicode character string.
Definition: qstring.h:83
static const AtomicValue::Ptr NegativeInfFloat
static const AtomicValue::Ptr InfFloat
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
static const AtomicValue::Ptr InfDouble
static const AtomicValue::Ptr NegativeInfDouble

◆ fromValue()

template<const bool isDouble>
Numeric::Ptr QPatternist::AbstractFloat< isDouble >::fromValue ( const xsDouble  num)
static

◆ internalSignbit()

template<const bool isDouble>
int QPatternist::AbstractFloat< isDouble >::internalSignbit ( const xsDouble  v)
inlinestaticprivate

From the Open Group's man page: "The signbit() macro shall return a non-zero value if and only if the sign of its argument value is negative."

MS Windows doesn't have std::signbit() so here's a reinvention of that function.

Definition at line 91 of file qabstractfloat.cpp.

Referenced by QPatternist::AbstractFloat< isDouble >::isEqual(), and QPatternist::AbstractFloat< isDouble >::stringValue().

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 }
__int64 qint64
Definition: qglobal.h:942
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ isEqual()

template<const bool isDouble>
bool QPatternist::AbstractFloat< isDouble >::isEqual ( const xsDouble  a,
const xsDouble  b 
)
static

Performs floating point comparison.

Returns
true if a and are equal, otherwise false.

Definition at line 113 of file qabstractfloat.cpp.

Referenced by QPatternist::DurationNumericMathematician::calculate(), QPatternist::NumericToBooleanCaster::castFrom(), QPatternist::AbstractFloatComparator::compare(), QPatternist::AbstractFloatSortComparator< t_op >::compare(), QPatternist::DecimalComparator::compare(), QPatternist::AbstractFloatComparator::equals(), QPatternist::DecimalComparator::equals(), QPatternist::Decimal::evaluateEBV(), QPatternist::AbstractFloat< isDouble >::isZero(), QPatternist::GenericPredicate::mapToItem(), and QPatternist::Decimal::toString().

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 }
long ASN1_INTEGER_get ASN1_INTEGER * a
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
static int internalSignbit(const xsDouble v)
Q_CORE_EXPORT bool qIsInf(double d)
Returns true if the double {d} is equivalent to infinity.
Definition: qnumeric.cpp:50

◆ isInf()

template<const bool isDouble>
bool QPatternist::AbstractFloat< isDouble >::isInf ( ) const
virtual

Determines whether this Numeric is an infinite number. Signedness is irrelevant, -INF as well as INF is considered infinity.

For numeric types that cannot represent infinity, such as xs:integer , this function should return false.

Returns
true if this Numeric is an infinite number

Implements QPatternist::Numeric.

Definition at line 289 of file qabstractfloat.cpp.

Referenced by QPatternist::AbstractFloat< isDouble >::roundHalfToEven().

290 {
291  return qIsInf(m_value);
292 }
Q_CORE_EXPORT bool qIsInf(double d)
Returns true if the double {d} is equivalent to infinity.
Definition: qnumeric.cpp:50

◆ isNaN()

template<const bool isDouble>
bool QPatternist::AbstractFloat< isDouble >::isNaN ( ) const
virtual

Determines whether this Numeric is not-a-number, NaN. For numeric types that cannot represent NaN, this function should return false.

Returns
true if this Numeric is NaN

Implements QPatternist::Numeric.

Definition at line 283 of file qabstractfloat.cpp.

Referenced by QPatternist::AbstractFloat< isDouble >::roundHalfToEven().

284 {
285  return qIsNaN(m_value);
286 }
Q_CORE_EXPORT bool qIsNaN(double d)
Returns true if the double {d} is not a number (NaN).
Definition: qnumeric.cpp:55

◆ isSigned()

template<const bool isDouble>
bool QPatternist::AbstractFloat< isDouble >::isSigned ( ) const
virtual

Returns true if this value is signed. If false is returned, the value is unsigned.

For float and decimal values, xs:double, xs:float and xs:decimal, the code asserts and behavior is undefined.

Implements QPatternist::Numeric.

Definition at line 307 of file qabstractfloat.cpp.

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 }
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ isZero()

template<const bool isDouble>
bool QPatternist::AbstractFloat< isDouble >::isZero ( ) const
inlineprivate

◆ round()

template<const bool isDouble>
Numeric::Ptr QPatternist::AbstractFloat< isDouble >::round ( ) const
virtual

Performs the algorithm specified for the function fn:round on this Numeric, and whose result is returned.

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 6.4.4 fn:round

Implements QPatternist::Numeric.

Definition at line 230 of file qabstractfloat.cpp.

231 {
232  return AbstractFloat<isDouble>::fromValue(static_cast<xsDouble>(roundFloat(m_value)));
233 }
static Numeric::Ptr fromValue(const xsDouble num)
static xsDouble roundFloat(const xsDouble val)
Implements fn:round() for types implemented with floating point.

◆ roundHalfToEven()

template<const bool isDouble>
Numeric::Ptr QPatternist::AbstractFloat< isDouble >::roundHalfToEven ( const xsInteger  scale) const
virtual

Performs rounding as defined for the fn:round-half-to-even on this Numeric, and whose result is returned.

See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 6.4.5 fn:round-half-to-even

Implements QPatternist::Numeric.

Definition at line 236 of file qabstractfloat.cpp.

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 }
virtual Numeric::Ptr floor() const
virtual bool isInf() const
virtual bool isNaN() const
static Numeric::Ptr fromValue(const xsDouble num)
QExplicitlySharedDataPointer< Numeric > Ptr

◆ stringValue()

template<const bool isDouble>
QString QPatternist::AbstractFloat< isDouble >::stringValue ( ) const
virtual

Returns this AbstractFloat represented as an xs:string.

Note
In the XPath/XQuery languages, converting xs:double and xs:float to xs:string is not specified in XML Schema 1.0 Part 2: Datatypes Second Edition, but in XQuery 1.0 and XPath 2.0 Functions and Operators. This will change with W3C XML Schema 1.1
See also
XQuery 1.0 and XPath 2.0 Functions and Operators, 17.1.2 Casting to xs:string and xdt:untypedAtomic

Implements QPatternist::AtomicValue.

Definition at line 145 of file qabstractfloat.cpp.

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 }
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
virtual xsDecimal toDecimal() const
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
static int internalSignbit(const xsDouble v)
static int sign(int x)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
Q_CORE_EXPORT bool qIsNaN(double d)
Returns true if the double {d} is not a number (NaN).
Definition: qnumeric.cpp:55
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
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
Q_CORE_EXPORT char * qdtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
Q_CORE_EXPORT bool qIsInf(double d)
Returns true if the double {d} is equivalent to infinity.
Definition: qnumeric.cpp:50
static QString toString(const xsDecimal value)
Definition: qdecimal.cpp:99

◆ toDecimal()

template<const bool isDouble>
xsDecimal QPatternist::AbstractFloat< isDouble >::toDecimal ( ) const
virtual
Returns
the particular number's value as a native representation of the type xs:decimal. This can be considered that the value is cast to xs:decimal.

Implements QPatternist::Numeric.

Definition at line 224 of file qabstractfloat.cpp.

Referenced by QPatternist::AbstractFloat< isDouble >::stringValue().

225 {
226  return static_cast<xsDecimal>(m_value);
227 }
xsDouble xsDecimal

◆ toDouble()

template<const bool isDouble>
xsDouble QPatternist::AbstractFloat< isDouble >::toDouble ( ) const
virtual
Returns
the particular number's value as a native representation of the type xs:double. This can be considered that the value is cast to xs:double.

Implements QPatternist::Numeric.

Definition at line 205 of file qabstractfloat.cpp.

206 {
207  return m_value;
208 }

◆ toFloat()

template<const bool isDouble>
xsFloat QPatternist::AbstractFloat< isDouble >::toFloat ( ) const
virtual
Returns
the particular number's value as a native representation of the type xs:float. This can be considered that the value is cast to xs:float.

Implements QPatternist::Numeric.

Definition at line 217 of file qabstractfloat.cpp.

218 {
219  /* No cast, since xsFloat and xsDouble are typedef'ed with the same type. */
220  return m_value;
221 }

◆ toInteger()

template<const bool isDouble>
xsInteger QPatternist::AbstractFloat< isDouble >::toInteger ( ) const
virtual
Returns
the particular number's value as a native representation of the type xs:integer. This can be considered that the value is cast to xs:integer.

Implements QPatternist::Numeric.

Definition at line 211 of file qabstractfloat.cpp.

212 {
213  return static_cast<xsInteger>(m_value);
214 }
qint64 xsInteger

◆ toNegated()

template<const bool isDouble>
Item QPatternist::AbstractFloat< isDouble >::toNegated ( ) const
virtual

Unary minus.

Implements QPatternist::Numeric.

Definition at line 301 of file qabstractfloat.cpp.

302 {
303  return fromValue(-m_value).data();
304 }
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
static Numeric::Ptr fromValue(const xsDouble num)

◆ toUnsignedInteger()

template<const bool isDouble>
qulonglong QPatternist::AbstractFloat< isDouble >::toUnsignedInteger ( ) const
virtual
Returns
the number as an unsigned integer. If the value is not unsigned, the code asserts and behavior is undefined.

Implements QPatternist::Numeric.

Definition at line 315 of file qabstractfloat.cpp.

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 }
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
#define Q_FUNC_INFO
Definition: qglobal.h:1871

◆ type()

template<const bool isDouble>
ItemType::Ptr QPatternist::AbstractFloat< isDouble >::type ( ) const
virtual

Implements QPatternist::AtomicValue.

Definition at line 295 of file qabstractfloat.cpp.

296 {
297  return isDouble ? BuiltinTypes::xsDouble : BuiltinTypes::xsFloat;
298 }
static const AtomicType::Ptr xsDouble
static const AtomicType::Ptr xsFloat

Properties

◆ m_value

template<const bool isDouble>
const xsDouble QPatternist::AbstractFloat< isDouble >::m_value
private

The documentation for this class was generated from the following files: