| 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 | #ifndef Patternist_AbstractFloat_H |
| 51 | #define Patternist_AbstractFloat_H |
| 52 | |
| 53 | #include <math.h> |
| 54 | |
| 55 | #include <qnumeric.h> |
| 56 | |
| 57 | #include <private/qcommonvalues_p.h> |
| 58 | #include <private/qdecimal_p.h> |
| 59 | #include <private/qschemanumeric_p.h> |
| 60 | #include <private/qvalidationerror_p.h> |
| 61 | #include <private/qbuiltintypes_p.h> |
| 62 | #include <private/qlocale_tools_p.h> |
| 63 | |
| 64 | QT_BEGIN_NAMESPACE |
| 65 | |
| 66 | namespace QPatternist |
| 67 | { |
| 68 | /** |
| 69 | * @short Base template class for Float and Double classes. |
| 70 | * |
| 71 | * @author Vincent Ricard <magic@magicninja.org> |
| 72 | * @ingroup Patternist_xdm |
| 73 | */ |
| 74 | template <const bool isDouble> |
| 75 | class AbstractFloat : public Numeric |
| 76 | { |
| 77 | public: |
| 78 | static Numeric::Ptr fromValue(const xsDouble num); |
| 79 | static AtomicValue::Ptr fromLexical(const QString &strNumeric); |
| 80 | |
| 81 | /** |
| 82 | * @todo more extensive docs. |
| 83 | * |
| 84 | * Performs floating point comparison. |
| 85 | * |
| 86 | * @returns @c true if @p a and @p are equal, otherwise @c false. |
| 87 | */ |
| 88 | static bool isEqual(const xsDouble a, const xsDouble b); |
| 89 | |
| 90 | /** |
| 91 | * Determines the Effective %Boolean Value of this number. |
| 92 | * |
| 93 | * @returns @c false if the number is 0 or @c NaN, otherwise @c true. |
| 94 | */ |
| 95 | bool evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const; |
| 96 | |
| 97 | /** |
| 98 | * Returns this AbstractFloat represented as an @c xs:string. |
| 99 | * |
| 100 | * @note In the XPath/XQuery languages, converting @c xs:double and @c xs:float |
| 101 | * to @c xs:string is not specified in XML Schema 1.0 Part 2: Datatypes Second Edition, |
| 102 | * but in XQuery 1.0 and XPath 2.0 Functions and Operators. This will change with W3C XML |
| 103 | * Schema 1.1 |
| 104 | * |
| 105 | * @see <a href="http://www.w3.org/TR/xpath-functions/#casting-to-string">XQuery 1.0 |
| 106 | * and XPath 2.0 Functions and Operators, 17.1.2 Casting to xs:string and xdt:untypedAtomic</a> |
| 107 | */ |
| 108 | virtual QString stringValue() const; |
| 109 | |
| 110 | virtual xsDouble toDouble() const; |
| 111 | virtual xsInteger toInteger() const; |
| 112 | virtual xsFloat toFloat() const; |
| 113 | virtual xsDecimal toDecimal() const; |
| 114 | |
| 115 | virtual Numeric::Ptr round() const; |
| 116 | virtual Numeric::Ptr roundHalfToEven(const xsInteger scale) const; |
| 117 | virtual Numeric::Ptr floor() const; |
| 118 | virtual Numeric::Ptr ceiling() const; |
| 119 | virtual Numeric::Ptr abs() const; |
| 120 | |
| 121 | virtual bool isNaN() const; |
| 122 | virtual bool isInf() const; |
| 123 | |
| 124 | virtual ItemType::Ptr type() const; |
| 125 | virtual Item toNegated() const; |
| 126 | virtual qulonglong toUnsignedInteger() const; |
| 127 | |
| 128 | virtual bool isSigned() const; |
| 129 | protected: |
| 130 | AbstractFloat(const xsDouble num); |
| 131 | |
| 132 | private: |
| 133 | /** |
| 134 | * From the Open Group's man page: "The signbit() macro shall return a |
| 135 | * non-zero value if and only if the sign of its argument value is |
| 136 | * negative." |
| 137 | * |
| 138 | * MS Windows doesn't have std::signbit() so here's |
| 139 | * a reinvention of that function. |
| 140 | */ |
| 141 | static inline int internalSignbit(const xsDouble v); |
| 142 | inline bool isZero() const; |
| 143 | |
| 144 | const xsDouble m_value; |
| 145 | }; |
| 146 | |
| 147 | template <const bool isDouble> |
| 148 | Numeric::Ptr createFloat(const xsDouble num); |
| 149 | |
| 150 | #include "qabstractfloat_tpl_p.h" |
| 151 | |
| 152 | /** |
| 153 | * @short An instantiation of AbsbstractFloat suitable for @c xs:double. |
| 154 | * |
| 155 | * @ingroup Patternist_xdm |
| 156 | */ |
| 157 | typedef AbstractFloat<true> Double; |
| 158 | |
| 159 | /** |
| 160 | * @short An instantiation of AbstractFloat suitable for @c xs:float. |
| 161 | * |
| 162 | * @ingroup Patternist_xdm |
| 163 | */ |
| 164 | typedef AbstractFloat<false> Float; |
| 165 | } |
| 166 | |
| 167 | QT_END_NAMESPACE |
| 168 | |
| 169 | #endif |
| 170 | |