1/* This file is part of the KDE project
2 Copyright 2007 Tomas Mecir <mecirt@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; only
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#ifndef CALLIGRA_SHEETS_NUMBER_H
21#define CALLIGRA_SHEETS_NUMBER_H
22
23#include "calligra_sheets_export.h"
24
25// #define CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
26
27#ifndef CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
28
29#include <math.h>
30
31typedef long double Number;
32
33inline long double numToDouble(Number n)
34{
35 return n;
36}
37
38namespace Calligra
39{
40namespace Sheets
41{
42
43inline Number log(const Number &n, Number base)
44{
45 return ::log10(n) / ::log10(base);
46}
47inline Number ln(const Number &n)
48{
49 return ::log(n);
50}
51inline Number tg(const Number &n)
52{
53 return ::tan(n);
54}
55inline Number atg(const Number &n)
56{
57 return ::atan(n);
58}
59inline Number tgh(const Number &n)
60{
61 return ::tanh(n);
62}
63inline Number atgh(const Number &n)
64{
65 return ::atanh(n);
66}
67
68} // namespace Sheets
69} // namespace Calligra
70
71#else // CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
72
73#include <QSharedDataPointer>
74
75#include <complex>
76
77using namespace std;
78
79namespace Calligra
80{
81namespace Sheets
82{
83
84/**
85The 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.
86
87The class is made so that if high precision is not desired, a "typedef long double Number" will revert us back to doubles.
88
89The 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?)
90
91Out-of-class methods for computations are provided
92*/
93
94class CALLIGRA_SHEETS_ODF_EXPORT Number
95{
96public:
97 enum Type {
98 Float // GnuMP will be here as well, eventually
99 };
100
101 // constructors
102 Number();
103 explicit Number(int num); //krazy:exclude=explicit
104 explicit Number(long double num); //krazy:exclude=explicit
105
106 Number(const Number& n);
107
108 ~Number();
109
110 long double asFloat() const;
111
112 // set/get
113 Number& operator= (const Number &n);
114
115 // basic operations
116 Number operator+ (const Number &n) const;
117 Number operator- (const Number &n) const;
118 Number operator*(const Number &n) const;
119 Number operator/ (const Number &n) const;
120
121 void operator+= (const Number &n);
122 void operator-= (const Number &n);
123 void operator*= (const Number &n);
124 void operator/= (const Number &n);
125
126 void operator++ () {
127 return operator+= (1);
128 }
129 void operator-- () {
130 return operator-= (1);
131 }
132
133 // unary -
134 Number operator- () const;
135
136 Number mod(const Number &n) const;
137
138 // comparison
139 bool operator<= (const Number &n) const;
140 bool operator< (const Number &n) const;
141 bool operator== (const Number &n) const;
142 bool operator!= (const Number &n) const {
143 return (!operator== (n));
144 }
145 bool operator>= (const Number &n) const {
146 return (!operator< (n));
147 }
148 bool operator> (const Number &n) const {
149 return (!operator<= (n));
150 }
151
152 // absolute value
153 Number abs() const;
154 // negative value
155 Number neg() const;
156 // power
157 Number pow(const Number &exp) const;
158 // logarithms
159 Number log(Number base) const;
160 Number ln() const;
161 Number exp() const;
162
163 // goniometric functions
164 Number sin() const;
165 Number cos() const;
166 Number tg() const;
167 Number cotg() const;
168 Number asin() const;
169 Number acos() const;
170 Number atg() const;
171 static Number atan2(const Number &y, const Number &x);
172
173 // hyperbolic functions
174 Number sinh() const;
175 Number cosh() const;
176 Number tgh() const;
177 Number asinh() const;
178 Number acosh() const;
179 Number atgh() const;
180
181 // TODO: add more functions, as needed
182
183 // TODO: functions to output the number to a string
184
185private:
186 class Private;
187 QSharedDataPointer<Private> d;
188
189}; // class Number
190
191// 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
192// usage of this function should eventually be removed, because places that use it are not ready for high precision support
193CALLIGRA_SHEETS_ODF_EXPORT long double numToDouble(Number n);
194
195// external operators, so that we can do things like 4+a without having to create temporary objects
196// not provided for complex numbers, as we won't be using them often like that
197Number operator+ (long double n1, const Number &n2);
198Number operator- (long double n1, const Number &n2);
199Number operator*(long double n1, const Number &n2);
200Number operator/ (long double n1, const Number &n2);
201bool operator<= (long double n1, const Number &n2);
202bool operator< (long double n1, const Number &n2);
203bool operator== (long double n1, const Number &n2);
204bool operator!= (long double n1, const Number &n2);
205bool operator>= (long double n1, const Number &n2);
206bool operator> (long double n1, const Number &n2);
207
208// external versions of the functions
209Number fmod(const Number &n1, const Number &n2);
210Number fabs(const Number &n);
211Number abs(const Number &n);
212Number neg(const Number &n);
213Number pow(const Number &n, const Number &exp);
214Number sqrt(const Number &n);
215Number log(const Number &n, Number base);
216Number ln(const Number &n);
217Number log(const Number &n);
218Number log10(const Number &n);
219Number exp(const Number &n);
220Number sin(const Number &n);
221Number cos(const Number &n);
222Number tg(const Number &n);
223Number cotg(const Number &n);
224Number asin(const Number &n);
225Number acos(const Number &n);
226Number atg(const Number &n);
227Number atan2(const Number &y, const Number &x);
228Number sinh(const Number &n);
229Number cosh(const Number &n);
230Number tgh(const Number &n);
231Number asinh(const Number &n);
232Number acosh(const Number &n);
233Number atgh(const Number &n);
234
235} // namespace Sheets
236} // namespace Calligra
237
238#endif // CALLIGRA_SHEETS_HIGH_PRECISION_SUPPORT
239
240#endif // CALLIGRA_SHEETS_NUMBER_H
241