|
1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Symbian Foundation License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32\include\e32math.h |
|
15 // |
|
16 // |
|
17 |
|
18 #ifndef __E32MATH_H__ |
|
19 #define __E32MATH_H__ |
|
20 #include <e32std.h> |
|
21 |
|
22 |
|
23 /** |
|
24 @publishedAll |
|
25 @released |
|
26 */ |
|
27 const TInt KMaxPrecision=15; |
|
28 /** |
|
29 @publishedAll |
|
30 @released |
|
31 |
|
32 This constant specifies the maximum number of significant digits available with floating |
|
33 point computations. Rounding and string formatting methods will not use more digits than this. |
|
34 */ |
|
35 const TInt KPrecisionLimit=12; |
|
36 /** |
|
37 @publishedAll |
|
38 @released |
|
39 */ |
|
40 const TInt KMantissaBits=53; |
|
41 /** |
|
42 @publishedAll |
|
43 @released |
|
44 */ |
|
45 const TInt KMaxExponent=1023; |
|
46 /** |
|
47 @publishedAll |
|
48 @released |
|
49 */ |
|
50 const TInt KExponentBias=1022; |
|
51 /** |
|
52 @publishedAll |
|
53 @released |
|
54 */ |
|
55 const TInt KSpecialExponent=2047; |
|
56 // |
|
57 |
|
58 |
|
59 /** |
|
60 @publishedAll |
|
61 @released |
|
62 |
|
63 The maximum exponent for a 32-bit floating point number. |
|
64 */ |
|
65 const TInt KTReal32MaxExponent=128; // changed from 127 |
|
66 |
|
67 |
|
68 /** |
|
69 @publishedAll |
|
70 @released |
|
71 |
|
72 The minimum exponent for a 32-bit floating point number. |
|
73 */ |
|
74 const TInt KTReal32MinExponent=-125; |
|
75 /** |
|
76 @publishedAll |
|
77 @released |
|
78 */ |
|
79 const TInt KTReal32ExponentBias=126; |
|
80 /** |
|
81 @publishedAll |
|
82 @released |
|
83 */ |
|
84 const TInt KTReal32SpecialExponent=255; // changed from KTReal32ExponentBad |
|
85 |
|
86 |
|
87 /** |
|
88 @publishedAll |
|
89 @released |
|
90 |
|
91 A zero exponent value for a 32-bit floating point number. |
|
92 */ |
|
93 const TInt KTReal32ZeroExponent=0; |
|
94 // |
|
95 |
|
96 |
|
97 /** |
|
98 @publishedAll |
|
99 @released |
|
100 |
|
101 The maximum exponent for a 64-bit floating point number. |
|
102 */ |
|
103 const TInt KTReal64MaxExponent=1024; // changed from 1023 |
|
104 |
|
105 |
|
106 /** |
|
107 @publishedAll |
|
108 @released |
|
109 |
|
110 The minimum exponent for a 64-bit floating point number. |
|
111 */ |
|
112 const TInt KTReal64MinExponent=-1021; |
|
113 |
|
114 |
|
115 /** |
|
116 @publishedAll |
|
117 @released |
|
118 */ |
|
119 const TInt KTReal64ExponentBias=1022; |
|
120 |
|
121 |
|
122 /** |
|
123 @publishedAll |
|
124 @released |
|
125 */ |
|
126 const TInt KTReal64SpecialExponent=2047; // changed from KTReal64BadExponent |
|
127 |
|
128 |
|
129 /** |
|
130 @publishedAll |
|
131 @released |
|
132 |
|
133 A zero exponent value for a 64-bit floating point number. |
|
134 */ |
|
135 const TInt KTReal64ZeroExponent=0; |
|
136 // |
|
137 |
|
138 |
|
139 /** |
|
140 @publishedAll |
|
141 @released |
|
142 |
|
143 The minimum value of a 64-bit floating point number. |
|
144 */ |
|
145 const TReal KMinTReal=2.2250738585072015E-308; // changed from TReal64 |
|
146 |
|
147 |
|
148 /** |
|
149 @publishedAll |
|
150 @released |
|
151 |
|
152 The maximum value of a 64-bit floating point number. |
|
153 */ |
|
154 const TReal KMaxTReal=1.7976931348623157E+308; // |
|
155 // |
|
156 |
|
157 |
|
158 /** |
|
159 @publishedAll |
|
160 @released |
|
161 |
|
162 The minimum value of a 32-bit floating point number. |
|
163 */ |
|
164 const TReal32 KMinTReal32=1.17549435E-38f; |
|
165 |
|
166 |
|
167 /** |
|
168 @publishedAll |
|
169 @released |
|
170 |
|
171 The maximum value of a 32-bit floating point number. |
|
172 */ |
|
173 const TReal32 KMaxTReal32=3.4028234663852885981170418348452e+38f; |
|
174 // |
|
175 |
|
176 |
|
177 /** |
|
178 @publishedAll |
|
179 @released |
|
180 |
|
181 The minimum value of a 64-bit floating point number. |
|
182 */ |
|
183 const TReal64 KMinTReal64=2.2250738585072015E-308; |
|
184 |
|
185 |
|
186 /** |
|
187 @publishedAll |
|
188 @released |
|
189 |
|
190 The maximum value of a 64-bit floating point number. |
|
191 */ |
|
192 const TReal64 KMaxTReal64=1.7976931348623157E+308; |
|
193 // |
|
194 |
|
195 |
|
196 /** |
|
197 @publishedAll |
|
198 @released |
|
199 */ |
|
200 const TReal KSqhf=0.70710678118654752440; |
|
201 |
|
202 |
|
203 /** |
|
204 @publishedAll |
|
205 @released |
|
206 |
|
207 Log 2 to the base "e". |
|
208 */ |
|
209 const TReal KRln2=1.4426950408889634; |
|
210 |
|
211 |
|
212 /** |
|
213 @publishedAll |
|
214 @released |
|
215 |
|
216 Log 10 to the base "e". |
|
217 */ |
|
218 const TReal KRln10=0.4342944819032518; |
|
219 |
|
220 |
|
221 /** |
|
222 @publishedAll |
|
223 @released |
|
224 |
|
225 Log 2 to the base 10. |
|
226 */ |
|
227 const TReal KRlg2=0.3010299956639812; |
|
228 |
|
229 |
|
230 /** |
|
231 @publishedAll |
|
232 @released |
|
233 |
|
234 The mathematical constant Pi. |
|
235 */ |
|
236 const TReal KPi=3.1415926535897932; |
|
237 |
|
238 |
|
239 /** |
|
240 @publishedAll |
|
241 @released |
|
242 |
|
243 The reciprocal of the mathematical constant Pi. |
|
244 */ |
|
245 const TReal KPiInv=0.3183098861837907; |
|
246 |
|
247 |
|
248 /** |
|
249 @publishedAll |
|
250 @released |
|
251 |
|
252 The mathematical constant Pi divided by 2. |
|
253 */ |
|
254 const TReal KPiBy2=1.5707963267948966; |
|
255 |
|
256 |
|
257 /** |
|
258 @publishedAll |
|
259 @released |
|
260 |
|
261 Not used. |
|
262 */ |
|
263 const TReal KDrpi=0.6366197723675813; |
|
264 |
|
265 |
|
266 /** |
|
267 @publishedAll |
|
268 @released |
|
269 |
|
270 The square root of 3. |
|
271 */ |
|
272 const TReal KSqt3=1.7320508075688773; |
|
273 |
|
274 |
|
275 /** |
|
276 @publishedAll |
|
277 @released |
|
278 */ |
|
279 const TReal KMsq3=0.2679491924311227; |
|
280 |
|
281 |
|
282 /** |
|
283 @publishedAll |
|
284 @released |
|
285 |
|
286 The multiplying factor to convert radians to degrees. |
|
287 */ |
|
288 const TReal KRadToDeg=57.29577951308232; |
|
289 |
|
290 |
|
291 /** |
|
292 @publishedAll |
|
293 @released |
|
294 |
|
295 The multiplying factor to convert degrees to radians. |
|
296 */ |
|
297 const TReal KDegToRad=0.017453292519943296; |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 class TRealX |
|
303 /** |
|
304 @publishedAll |
|
305 @released |
|
306 |
|
307 A class encapsulating an extended precision real value. |
|
308 |
|
309 This class provides 64 bit precision and a dynamic range of approximately |
|
310 1E-9863 to 1E+9863. All member functions are optimized for speed. |
|
311 */ |
|
312 { |
|
313 public: |
|
314 enum TRealXOrder {ELessThan=1,EEqual=2,EGreaterThan=4,EUnordered=8}; |
|
315 public: |
|
316 IMPORT_C TRealX(); |
|
317 IMPORT_C TRealX(TInt aInt); |
|
318 IMPORT_C TRealX(TUint aInt); |
|
319 IMPORT_C TRealX(TUint aExp, TUint aMantHi, TUint aMantLo); |
|
320 IMPORT_C TRealX(const TInt64 &aInt); |
|
321 IMPORT_C TRealX(TReal32 aReal) __SOFTFP; |
|
322 IMPORT_C TRealX(TReal64 aReal) __SOFTFP; |
|
323 IMPORT_C TRealX &operator=(TInt aInt); |
|
324 IMPORT_C TRealX &operator=(TUint aInt); |
|
325 IMPORT_C TRealX &operator=(const TInt64& aInt); |
|
326 IMPORT_C TRealX &operator=(TReal32 aReal) __SOFTFP; |
|
327 IMPORT_C TRealX &operator=(TReal64 aReal) __SOFTFP; |
|
328 IMPORT_C TInt Set(TInt aInt); |
|
329 IMPORT_C TInt Set(TUint aInt); |
|
330 IMPORT_C TInt Set(const TInt64& aInt); |
|
331 IMPORT_C TInt Set(TReal32 aReal) __SOFTFP; |
|
332 IMPORT_C TInt Set(TReal64 aReal) __SOFTFP; |
|
333 IMPORT_C operator TInt() const; |
|
334 IMPORT_C operator TUint() const; |
|
335 IMPORT_C operator TInt64() const; |
|
336 IMPORT_C operator TReal32() const __SOFTFP; |
|
337 IMPORT_C operator TReal64() const __SOFTFP; |
|
338 IMPORT_C TInt GetTReal(TReal32 &aVal) const; |
|
339 IMPORT_C TInt GetTReal(TReal64 &aVal) const; |
|
340 IMPORT_C void SetZero(TBool aNegative=EFalse); |
|
341 IMPORT_C void SetNaN(); |
|
342 IMPORT_C void SetInfinite(TBool aNegative); |
|
343 IMPORT_C TBool IsZero() const; |
|
344 IMPORT_C TBool IsNaN() const; |
|
345 IMPORT_C TBool IsInfinite() const; |
|
346 IMPORT_C TBool IsFinite() const; |
|
347 IMPORT_C const TRealX &operator+=(const TRealX &aVal); |
|
348 IMPORT_C const TRealX &operator-=(const TRealX &aVal); |
|
349 IMPORT_C const TRealX &operator*=(const TRealX &aVal); |
|
350 IMPORT_C const TRealX &operator/=(const TRealX &aVal); |
|
351 IMPORT_C const TRealX &operator%=(const TRealX &aVal); |
|
352 IMPORT_C TInt AddEq(const TRealX &aVal); |
|
353 IMPORT_C TInt SubEq(const TRealX &aVal); |
|
354 IMPORT_C TInt MultEq(const TRealX &aVal); |
|
355 IMPORT_C TInt DivEq(const TRealX &aVal); |
|
356 IMPORT_C TInt ModEq(const TRealX &aVal); |
|
357 IMPORT_C TRealX operator+() const; |
|
358 IMPORT_C TRealX operator-() const; |
|
359 IMPORT_C TRealX &operator++(); |
|
360 IMPORT_C TRealX operator++(TInt); |
|
361 IMPORT_C TRealX &operator--(); |
|
362 IMPORT_C TRealX operator--(TInt); |
|
363 IMPORT_C TRealX operator+(const TRealX &aVal) const; |
|
364 IMPORT_C TRealX operator-(const TRealX &aVal) const; |
|
365 IMPORT_C TRealX operator*(const TRealX &aVal) const; |
|
366 IMPORT_C TRealX operator/(const TRealX &aVal) const; |
|
367 IMPORT_C TRealX operator%(const TRealX &aVal) const; |
|
368 IMPORT_C TInt Add(TRealX& aResult,const TRealX &aVal) const; |
|
369 IMPORT_C TInt Sub(TRealX& aResult,const TRealX &aVal) const; |
|
370 IMPORT_C TInt Mult(TRealX& aResult,const TRealX &aVal) const; |
|
371 IMPORT_C TInt Div(TRealX& aResult,const TRealX &aVal) const; |
|
372 IMPORT_C TInt Mod(TRealX& aResult,const TRealX &aVal) const; |
|
373 IMPORT_C TRealXOrder Compare(const TRealX& aVal) const; |
|
374 inline TBool operator==(const TRealX &aVal) const; |
|
375 inline TBool operator!=(const TRealX &aVal) const; |
|
376 inline TBool operator>=(const TRealX &aVal) const; |
|
377 inline TBool operator<=(const TRealX &aVal) const; |
|
378 inline TBool operator>(const TRealX &aVal) const; |
|
379 inline TBool operator<(const TRealX &aVal) const; |
|
380 public: |
|
381 /** |
|
382 The mantissa. |
|
383 */ |
|
384 // Represented as two adjacent 32 bit values, rather than one 64 value. |
|
385 // This is to avoid EABI introduced padding overheads and BC breakages. |
|
386 // This representation works because the mantissa is always accessed from |
|
387 // assembler code as two 32 bit quantities. The C++ code that accesses it |
|
388 // now constructs an automatic TInt64 with the two components. |
|
389 TUint32 iMantLo; |
|
390 TUint32 iMantHi; |
|
391 |
|
392 /** |
|
393 The sign: 0 for +, 1 for - |
|
394 */ |
|
395 TInt8 iSign; |
|
396 |
|
397 /** |
|
398 Flags: 0 for exact, 1 for rounded down, 2 for rounded up |
|
399 */ |
|
400 TUint8 iFlag; |
|
401 |
|
402 /** |
|
403 Exponent: biased by 32767, iExp=0 => zero, +65535 => infinity or NaN |
|
404 */ |
|
405 TUint16 iExp; |
|
406 }; |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 struct SPoly |
|
412 /** |
|
413 @publishedAll |
|
414 @released |
|
415 |
|
416 A structure containing the set of coefficients for a polynomial. |
|
417 |
|
418 @see Math::Poly |
|
419 */ |
|
420 { |
|
421 TInt num; |
|
422 TReal c[1]; |
|
423 }; |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 class Math |
|
429 /** |
|
430 @publishedAll |
|
431 @released |
|
432 |
|
433 A collection of mathematical functions. |
|
434 */ |
|
435 { |
|
436 public: |
|
437 IMPORT_C static TInt ACos(TReal &aTrg,const TReal &aSrc); |
|
438 IMPORT_C static TInt ASin(TReal &aTrg,const TReal &aSrc); |
|
439 IMPORT_C static TInt ATan(TReal &aTrg,const TReal &aSrc); |
|
440 IMPORT_C static TInt ATan(TReal &aTrg,const TReal &aSrcY,const TReal &aSrcX); |
|
441 IMPORT_C static TInt Cos(TReal &aTrg,const TReal &aSrc); |
|
442 |
|
443 /** |
|
444 This function is not implemented by Symbian OS. |
|
445 */ |
|
446 IMPORT_C static TInt DtoR(TReal &aTrg,const TDesC &aSrc,TInt &aPos,const TChar aPoint); |
|
447 IMPORT_C static TInt Exp(TReal &aTrg,const TReal &aSrc); |
|
448 IMPORT_C static TInt Frac(TReal &aTrg,const TReal &aSrc); |
|
449 IMPORT_C static TInt Int(TReal &aTrg,const TReal &aSrc); |
|
450 IMPORT_C static TInt Int(TInt16 &aTrg,const TReal &aSrc); |
|
451 IMPORT_C static TInt Int(TInt32 &aTrg,const TReal &aSrc); |
|
452 IMPORT_C static TInt Log(TReal &aTrg,const TReal &aSrc); |
|
453 IMPORT_C static TInt Ln(TReal &aTrg,const TReal &aSrc); |
|
454 IMPORT_C static TInt Mod(TReal &aTrg,const TReal &aSrc,const TReal &aModulus); |
|
455 IMPORT_C static TReal Poly(TReal aVal,const SPoly *aPoly) __SOFTFP; |
|
456 IMPORT_C static TInt Pow(TReal &aTrg,const TReal &aSrc,const TReal &aPower); |
|
457 IMPORT_C static TInt Pow10(TReal &aTrg,const TInt exp); |
|
458 IMPORT_C static TInt Rand(TInt64 &aSeed); |
|
459 IMPORT_C static TReal FRand(TInt64 &aSeed) __SOFTFP; |
|
460 IMPORT_C static TUint32 Random(); |
|
461 IMPORT_C static TInt Round(TReal &aTrg,const TReal &aSrc,TInt aDecimalPlaces); |
|
462 IMPORT_C static TInt Sin(TReal &aTrg,const TReal &aSrc); |
|
463 IMPORT_C static TInt Sqrt(TReal &aTrg,const TReal &aSrc); |
|
464 IMPORT_C static TInt Tan(TReal &aTrg,const TReal &aSrc); |
|
465 IMPORT_C static TBool IsZero(const TReal &aVal); |
|
466 IMPORT_C static TBool IsNaN(const TReal &aVal); |
|
467 IMPORT_C static TBool IsInfinite(const TReal &aVal); |
|
468 IMPORT_C static TBool IsFinite(const TReal &aVal); |
|
469 IMPORT_C static void PolyX(TRealX& aY, const TRealX& aX, TInt aDeg, const TRealX *aCoef); |
|
470 static TInt MultPow10X(TRealX& aTrg, TInt aPower); |
|
471 IMPORT_C static void Mul64(Int64 aX, Int64 aY, Int64& aOutH, Uint64& aOutL); |
|
472 IMPORT_C static void UMul64(Uint64 aX, Uint64 aY, Uint64& aOutH, Uint64& aOutL); |
|
473 IMPORT_C static Int64 DivMod64(Int64 aDividend, Int64 aDivisor, Int64& aRemainder); |
|
474 IMPORT_C static Uint64 UDivMod64(Uint64 aDividend, Uint64 aDivisor, Uint64& aRemainder); |
|
475 private: |
|
476 IMPORT_C static void SetZero(TReal &aVal,TInt aSign=0); |
|
477 IMPORT_C static void SetNaN(TReal &aVal); |
|
478 IMPORT_C static void SetInfinite(TReal &aVal,TInt aSign); |
|
479 }; |
|
480 |
|
481 #include <e32math.inl> |
|
482 |
|
483 #endif // __E32MATH_H__ |