File indexing completed on 2024-04-28 16:21:25

0001 /* This file is part of the KDE project
0002    Copyright 2007 Tomas Mecir <mecirt@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; only
0007    version 2 of the License.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef CALLIGRA_SHEETS_NUMBER_H
0021 #define CALLIGRA_SHEETS_NUMBER_H
0022 
0023 #include "sheets_odf_export.h"
0024 
0025 // #define CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
0026 
0027 #ifndef CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
0028 
0029 #include <math.h>
0030 
0031 typedef long double Number;
0032 
0033 inline long double numToDouble(Number n)
0034 {
0035     return n;
0036 }
0037 
0038 namespace Calligra
0039 {
0040 namespace Sheets
0041 {
0042 
0043 inline Number log(const Number &n, Number base)
0044 {
0045     return ::log10(n) / ::log10(base);
0046 }
0047 inline Number ln(const Number &n)
0048 {
0049     return ::log(n);
0050 }
0051 inline Number tg(const Number &n)
0052 {
0053     return ::tan(n);
0054 }
0055 inline Number atg(const Number &n)
0056 {
0057     return ::atan(n);
0058 }
0059 inline Number tgh(const Number &n)
0060 {
0061     return ::tanh(n);
0062 }
0063 inline Number atgh(const Number &n)
0064 {
0065     return ::atanh(n);
0066 }
0067 
0068 } // namespace Sheets
0069 } // namespace Calligra
0070 
0071 #else // CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
0072 
0073 #include <QSharedDataPointer>
0074 
0075 #include <complex>
0076 
0077 using namespace std;
0078 
0079 namespace Calligra
0080 {
0081 namespace Sheets
0082 {
0083 
0084 /**
0085 The Number class holds a single floating-point number. At the moment, it's just a wrapper for long double, but it's going to support GnuMP or something eventually.
0086 
0087 The class is made so that if high precision is not desired, a "typedef long double Number" will revert us back to doubles.
0088 
0089 The class will be able to format itself into a string, using provided locale settings. (TODO: how to handle this so that parsing/formatting works even if we typedef this class out?)
0090 
0091 Out-of-class methods for computations are provided
0092 */
0093 
0094 class CALLIGRA_SHEETS_ODF_EXPORT Number
0095 {
0096 public:
0097     enum Type {
0098         Float  // GnuMP will be here as well, eventually
0099     };
0100 
0101     // constructors
0102     Number();
0103     explicit Number(int num); //krazy:exclude=explicit
0104     explicit Number(long double num); //krazy:exclude=explicit
0105 
0106     Number(const Number& n);
0107 
0108     ~Number();
0109 
0110     long double asFloat() const;
0111 
0112     // set/get
0113     Number& operator= (const Number &n);
0114 
0115     // basic operations
0116     Number operator+ (const Number &n) const;
0117     Number operator- (const Number &n) const;
0118     Number operator*(const Number &n) const;
0119     Number operator/ (const Number &n) const;
0120 
0121     void operator+= (const Number &n);
0122     void operator-= (const Number &n);
0123     void operator*= (const Number &n);
0124     void operator/= (const Number &n);
0125 
0126     void operator++ () {
0127         return operator+= (1);
0128     }
0129     void operator-- () {
0130         return operator-= (1);
0131     }
0132 
0133     // unary -
0134     Number operator- () const;
0135 
0136     Number mod(const Number &n) const;
0137 
0138     // comparison
0139     bool operator<= (const Number &n) const;
0140     bool operator< (const Number &n) const;
0141     bool operator== (const Number &n) const;
0142     bool operator!= (const Number &n) const {
0143         return (!operator== (n));
0144     }
0145     bool operator>= (const Number &n) const {
0146         return (!operator< (n));
0147     }
0148     bool operator> (const Number &n) const {
0149         return (!operator<= (n));
0150     }
0151 
0152     // absolute value
0153     Number abs() const;
0154     // negative value
0155     Number neg() const;
0156     // power
0157     Number pow(const Number &exp) const;
0158     // logarithms
0159     Number log(Number base) const;
0160     Number ln() const;
0161     Number exp() const;
0162 
0163     // goniometric functions
0164     Number sin() const;
0165     Number cos() const;
0166     Number tg() const;
0167     Number cotg() const;
0168     Number asin() const;
0169     Number acos() const;
0170     Number atg() const;
0171     static Number atan2(const Number &y, const Number &x);
0172 
0173     // hyperbolic functions
0174     Number sinh() const;
0175     Number cosh() const;
0176     Number tgh() const;
0177     Number asinh() const;
0178     Number acosh() const;
0179     Number atgh() const;
0180 
0181     // TODO: add more functions, as needed
0182 
0183     // TODO: functions to output the number to a string
0184 
0185 private:
0186     class Private;
0187     QSharedDataPointer<Private> d;
0188 
0189 };  // class Number
0190 
0191 // conversion to double ... when we add the option to #define the Number class as double, this routine should be kept in place, and it should simply return its parameter
0192 // usage of this function should eventually be removed, because places that use it are not ready for high precision support
0193 CALLIGRA_SHEETS_ODF_EXPORT long double numToDouble(Number n);
0194 
0195 // external operators, so that we can do things like 4+a without having to create temporary objects
0196 // not provided for complex numbers, as we won't be using them often like that
0197 Number operator+ (long double n1, const Number &n2);
0198 Number operator- (long double n1, const Number &n2);
0199 Number operator*(long double n1, const Number &n2);
0200 Number operator/ (long double n1, const Number &n2);
0201 bool operator<= (long double n1, const Number &n2);
0202 bool operator< (long double n1, const Number &n2);
0203 bool operator== (long double n1, const Number &n2);
0204 bool operator!= (long double n1, const Number &n2);
0205 bool operator>= (long double n1, const Number &n2);
0206 bool operator> (long double n1, const Number &n2);
0207 
0208 // external versions of the functions
0209 Number fmod(const Number &n1, const Number &n2);
0210 Number fabs(const Number &n);
0211 Number abs(const Number &n);
0212 Number neg(const Number &n);
0213 Number pow(const Number &n, const Number &exp);
0214 Number sqrt(const Number &n);
0215 Number log(const Number &n, Number base);
0216 Number ln(const Number &n);
0217 Number log(const Number &n);
0218 Number log10(const Number &n);
0219 Number exp(const Number &n);
0220 Number sin(const Number &n);
0221 Number cos(const Number &n);
0222 Number tg(const Number &n);
0223 Number cotg(const Number &n);
0224 Number asin(const Number &n);
0225 Number acos(const Number &n);
0226 Number atg(const Number &n);
0227 Number atan2(const Number &y, const Number &x);
0228 Number sinh(const Number &n);
0229 Number cosh(const Number &n);
0230 Number tgh(const Number &n);
0231 Number asinh(const Number &n);
0232 Number acosh(const Number &n);
0233 Number atgh(const Number &n);
0234 
0235 } // namespace Sheets
0236 } // namespace Calligra
0237 
0238 #endif // CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
0239 
0240 #endif // CALLIGRA_SHEETS_NUMBER_H