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