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 | |
31 | typedef long double Number; |
32 | |
33 | inline long double numToDouble(Number n) |
34 | { |
35 | return n; |
36 | } |
37 | |
38 | namespace Calligra |
39 | { |
40 | namespace Sheets |
41 | { |
42 | |
43 | inline Number log(const Number &n, Number base) |
44 | { |
45 | return ::log10(n) / ::log10(base); |
46 | } |
47 | inline Number ln(const Number &n) |
48 | { |
49 | return ::log(n); |
50 | } |
51 | inline Number tg(const Number &n) |
52 | { |
53 | return ::tan(n); |
54 | } |
55 | inline Number atg(const Number &n) |
56 | { |
57 | return ::atan(n); |
58 | } |
59 | inline Number tgh(const Number &n) |
60 | { |
61 | return ::tanh(n); |
62 | } |
63 | inline 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 | |
77 | using namespace std; |
78 | |
79 | namespace Calligra |
80 | { |
81 | namespace Sheets |
82 | { |
83 | |
84 | /** |
85 | 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. |
86 | |
87 | The class is made so that if high precision is not desired, a "typedef long double Number" will revert us back to doubles. |
88 | |
89 | 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?) |
90 | |
91 | Out-of-class methods for computations are provided |
92 | */ |
93 | |
94 | class CALLIGRA_SHEETS_ODF_EXPORT Number |
95 | { |
96 | public: |
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 | |
185 | private: |
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 |
193 | CALLIGRA_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 |
197 | Number operator+ (long double n1, const Number &n2); |
198 | Number operator- (long double n1, const Number &n2); |
199 | Number operator*(long double n1, const Number &n2); |
200 | Number operator/ (long double n1, const Number &n2); |
201 | bool operator<= (long double n1, const Number &n2); |
202 | bool operator< (long double n1, const Number &n2); |
203 | bool operator== (long double n1, const Number &n2); |
204 | bool operator!= (long double n1, const Number &n2); |
205 | bool operator>= (long double n1, const Number &n2); |
206 | bool operator> (long double n1, const Number &n2); |
207 | |
208 | // external versions of the functions |
209 | Number fmod(const Number &n1, const Number &n2); |
210 | Number fabs(const Number &n); |
211 | Number abs(const Number &n); |
212 | Number neg(const Number &n); |
213 | Number pow(const Number &n, const Number &exp); |
214 | Number sqrt(const Number &n); |
215 | Number log(const Number &n, Number base); |
216 | Number ln(const Number &n); |
217 | Number log(const Number &n); |
218 | Number log10(const Number &n); |
219 | Number exp(const Number &n); |
220 | Number sin(const Number &n); |
221 | Number cos(const Number &n); |
222 | Number tg(const Number &n); |
223 | Number cotg(const Number &n); |
224 | Number asin(const Number &n); |
225 | Number acos(const Number &n); |
226 | Number atg(const Number &n); |
227 | Number atan2(const Number &y, const Number &x); |
228 | Number sinh(const Number &n); |
229 | Number cosh(const Number &n); |
230 | Number tgh(const Number &n); |
231 | Number asinh(const Number &n); |
232 | Number acosh(const Number &n); |
233 | Number 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 | |