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" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\include\e32math.h
27 const TInt KMaxPrecision=15;
32 const TInt KPrecisionLimit=12;
37 const TInt KMantissaBits=53;
42 const TInt KMaxExponent=1023;
47 const TInt KExponentBias=1022;
52 const TInt KSpecialExponent=2047;
60 The maximum exponent for a 32-bit floating point number.
62 const TInt KTReal32MaxExponent=128; // changed from 127
69 The minimum exponent for a 32-bit floating point number.
71 const TInt KTReal32MinExponent=-125;
76 const TInt KTReal32ExponentBias=126;
81 const TInt KTReal32SpecialExponent=255; // changed from KTReal32ExponentBad
88 A zero exponent value for a 32-bit floating point number.
90 const TInt KTReal32ZeroExponent=0;
98 The maximum exponent for a 64-bit floating point number.
100 const TInt KTReal64MaxExponent=1024; // changed from 1023
107 The minimum exponent for a 64-bit floating point number.
109 const TInt KTReal64MinExponent=-1021;
116 const TInt KTReal64ExponentBias=1022;
123 const TInt KTReal64SpecialExponent=2047; // changed from KTReal64BadExponent
130 A zero exponent value for a 64-bit floating point number.
132 const TInt KTReal64ZeroExponent=0;
140 The minimum value of a 64-bit floating point number.
142 const TReal KMinTReal=2.2250738585072015E-308; // changed from TReal64
149 The maximum value of a 64-bit floating point number.
151 const TReal KMaxTReal=1.7976931348623157E+308; //
159 The minimum value of a 32-bit floating point number.
161 const TReal32 KMinTReal32=1.17549435E-38f;
168 The maximum value of a 32-bit floating point number.
170 const TReal32 KMaxTReal32=3.4028234663852885981170418348452e+38f;
178 The minimum value of a 64-bit floating point number.
180 const TReal64 KMinTReal64=2.2250738585072015E-308;
187 The maximum value of a 64-bit floating point number.
189 const TReal64 KMaxTReal64=1.7976931348623157E+308;
197 const TReal KSqhf=0.70710678118654752440;
204 Log 2 to the base "e".
206 const TReal KRln2=1.4426950408889634;
213 Log 10 to the base "e".
215 const TReal KRln10=0.4342944819032518;
222 Log 2 to the base 10.
224 const TReal KRlg2=0.3010299956639812;
231 The mathematical constant Pi.
233 const TReal KPi=3.1415926535897932;
240 The reciprocal of the mathematical constant Pi.
242 const TReal KPiInv=0.3183098861837907;
249 The mathematical constant Pi divided by 2.
251 const TReal KPiBy2=1.5707963267948966;
260 const TReal KDrpi=0.6366197723675813;
267 The square root of 3.
269 const TReal KSqt3=1.7320508075688773;
276 const TReal KMsq3=0.2679491924311227;
283 The multiplying factor to convert radians to degrees.
285 const TReal KRadToDeg=57.29577951308232;
292 The multiplying factor to convert degrees to radians.
294 const TReal KDegToRad=0.017453292519943296;
304 A class encapsulating an extended precision real value.
306 This class provides 64 bit precision and a dynamic range of approximately
307 1E-9863 to 1E+9863. All member functions are optimized for speed.
311 enum TRealXOrder {ELessThan=1,EEqual=2,EGreaterThan=4,EUnordered=8};
314 IMPORT_C TRealX(TInt aInt);
315 IMPORT_C TRealX(TUint aInt);
316 IMPORT_C TRealX(TUint aExp, TUint aMantHi, TUint aMantLo);
317 IMPORT_C TRealX(const TInt64 &aInt);
318 IMPORT_C TRealX(TReal32 aReal) __SOFTFP;
319 IMPORT_C TRealX(TReal64 aReal) __SOFTFP;
320 IMPORT_C TRealX &operator=(TInt aInt);
321 IMPORT_C TRealX &operator=(TUint aInt);
322 IMPORT_C TRealX &operator=(const TInt64& aInt);
323 IMPORT_C TRealX &operator=(TReal32 aReal) __SOFTFP;
324 IMPORT_C TRealX &operator=(TReal64 aReal) __SOFTFP;
325 IMPORT_C TInt Set(TInt aInt);
326 IMPORT_C TInt Set(TUint aInt);
327 IMPORT_C TInt Set(const TInt64& aInt);
328 IMPORT_C TInt Set(TReal32 aReal) __SOFTFP;
329 IMPORT_C TInt Set(TReal64 aReal) __SOFTFP;
330 IMPORT_C operator TInt() const;
331 IMPORT_C operator TUint() const;
332 IMPORT_C operator TInt64() const;
333 IMPORT_C operator TReal32() const __SOFTFP;
334 IMPORT_C operator TReal64() const __SOFTFP;
335 IMPORT_C TInt GetTReal(TReal32 &aVal) const;
336 IMPORT_C TInt GetTReal(TReal64 &aVal) const;
337 IMPORT_C void SetZero(TBool aNegative=EFalse);
338 IMPORT_C void SetNaN();
339 IMPORT_C void SetInfinite(TBool aNegative);
340 IMPORT_C TBool IsZero() const;
341 IMPORT_C TBool IsNaN() const;
342 IMPORT_C TBool IsInfinite() const;
343 IMPORT_C TBool IsFinite() const;
344 IMPORT_C const TRealX &operator+=(const TRealX &aVal);
345 IMPORT_C const TRealX &operator-=(const TRealX &aVal);
346 IMPORT_C const TRealX &operator*=(const TRealX &aVal);
347 IMPORT_C const TRealX &operator/=(const TRealX &aVal);
348 IMPORT_C const TRealX &operator%=(const TRealX &aVal);
349 IMPORT_C TInt AddEq(const TRealX &aVal);
350 IMPORT_C TInt SubEq(const TRealX &aVal);
351 IMPORT_C TInt MultEq(const TRealX &aVal);
352 IMPORT_C TInt DivEq(const TRealX &aVal);
353 IMPORT_C TInt ModEq(const TRealX &aVal);
354 IMPORT_C TRealX operator+() const;
355 IMPORT_C TRealX operator-() const;
356 IMPORT_C TRealX &operator++();
357 IMPORT_C TRealX operator++(TInt);
358 IMPORT_C TRealX &operator--();
359 IMPORT_C TRealX operator--(TInt);
360 IMPORT_C TRealX operator+(const TRealX &aVal) const;
361 IMPORT_C TRealX operator-(const TRealX &aVal) const;
362 IMPORT_C TRealX operator*(const TRealX &aVal) const;
363 IMPORT_C TRealX operator/(const TRealX &aVal) const;
364 IMPORT_C TRealX operator%(const TRealX &aVal) const;
365 IMPORT_C TInt Add(TRealX& aResult,const TRealX &aVal) const;
366 IMPORT_C TInt Sub(TRealX& aResult,const TRealX &aVal) const;
367 IMPORT_C TInt Mult(TRealX& aResult,const TRealX &aVal) const;
368 IMPORT_C TInt Div(TRealX& aResult,const TRealX &aVal) const;
369 IMPORT_C TInt Mod(TRealX& aResult,const TRealX &aVal) const;
370 IMPORT_C TRealXOrder Compare(const TRealX& aVal) const;
371 inline TBool operator==(const TRealX &aVal) const;
372 inline TBool operator!=(const TRealX &aVal) const;
373 inline TBool operator>=(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;
381 // Represented as two adjacent 32 bit values, rather than one 64 value.
382 // This is to avoid EABI introduced padding overheads and BC breakages.
383 // This representation works because the mantissa is always accessed from
384 // assembler code as two 32 bit quantities. The C++ code that accesses it
385 // now constructs an automatic TInt64 with the two components.
390 The sign: 0 for +, 1 for -
395 Flags: 0 for exact, 1 for rounded down, 2 for rounded up
400 Exponent: biased by 32767, iExp=0 => zero, +65535 => infinity or NaN
413 A structure containing the set of coefficients for a polynomial.
430 A collection of mathematical functions.
434 IMPORT_C static TInt ACos(TReal &aTrg,const TReal &aSrc);
435 IMPORT_C static TInt ASin(TReal &aTrg,const TReal &aSrc);
436 IMPORT_C static TInt ATan(TReal &aTrg,const TReal &aSrc);
437 IMPORT_C static TInt ATan(TReal &aTrg,const TReal &aSrcY,const TReal &aSrcX);
438 IMPORT_C static TInt Cos(TReal &aTrg,const TReal &aSrc);
441 This function is not implemented by Symbian OS.
443 IMPORT_C static TInt DtoR(TReal &aTrg,const TDesC &aSrc,TInt &aPos,const TChar aPoint);
444 IMPORT_C static TInt Exp(TReal &aTrg,const TReal &aSrc);
445 IMPORT_C static TInt Frac(TReal &aTrg,const TReal &aSrc);
446 IMPORT_C static TInt Int(TReal &aTrg,const TReal &aSrc);
447 IMPORT_C static TInt Int(TInt16 &aTrg,const TReal &aSrc);
448 IMPORT_C static TInt Int(TInt32 &aTrg,const TReal &aSrc);
449 IMPORT_C static TInt Log(TReal &aTrg,const TReal &aSrc);
450 IMPORT_C static TInt Ln(TReal &aTrg,const TReal &aSrc);
451 IMPORT_C static TInt Mod(TReal &aTrg,const TReal &aSrc,const TReal &aModulus);
452 IMPORT_C static TReal Poly(TReal aVal,const SPoly *aPoly) __SOFTFP;
453 IMPORT_C static TInt Pow(TReal &aTrg,const TReal &aSrc,const TReal &aPower);
454 IMPORT_C static TInt Pow10(TReal &aTrg,const TInt exp);
455 IMPORT_C static TInt Rand(TInt64 &aSeed);
456 IMPORT_C static TReal FRand(TInt64 &aSeed) __SOFTFP;
457 IMPORT_C static TUint32 Random();
458 IMPORT_C static TInt Round(TReal &aTrg,const TReal &aSrc,TInt aDecimalPlaces);
459 IMPORT_C static TInt Sin(TReal &aTrg,const TReal &aSrc);
460 IMPORT_C static TInt Sqrt(TReal &aTrg,const TReal &aSrc);
461 IMPORT_C static TInt Tan(TReal &aTrg,const TReal &aSrc);
462 IMPORT_C static TBool IsZero(const TReal &aVal);
463 IMPORT_C static TBool IsNaN(const TReal &aVal);
464 IMPORT_C static TBool IsInfinite(const TReal &aVal);
465 IMPORT_C static TBool IsFinite(const TReal &aVal);
466 IMPORT_C static void PolyX(TRealX& aY, const TRealX& aX, TInt aDeg, const TRealX *aCoef);
467 static TInt MultPow10X(TRealX& aTrg, TInt aPower);
468 IMPORT_C static void Mul64(Int64 aX, Int64 aY, Int64& aOutH, Uint64& aOutL);
469 IMPORT_C static void UMul64(Uint64 aX, Uint64 aY, Uint64& aOutH, Uint64& aOutL);
470 IMPORT_C static Int64 DivMod64(Int64 aDividend, Int64 aDivisor, Int64& aRemainder);
471 IMPORT_C static Uint64 UDivMod64(Uint64 aDividend, Uint64 aDivisor, Uint64& aRemainder);
473 IMPORT_C static void SetZero(TReal &aVal,TInt aSign=0);
474 IMPORT_C static void SetNaN(TReal &aVal);
475 IMPORT_C static void SetInfinite(TReal &aVal,TInt aSign);
478 #include <e32math.inl>
480 #endif // __E32MATH_H__