sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\include\e32math.h sl@0: // sl@0: // sl@0: sl@0: #ifndef __E32MATH_H__ sl@0: #define __E32MATH_H__ sl@0: #include sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KMaxPrecision=15; sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: This constant specifies the maximum number of significant digits available with floating sl@0: point computations. Rounding and string formatting methods will not use more digits than this. sl@0: */ sl@0: const TInt KPrecisionLimit=12; sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: Let D be the set of real numbers exactly representable by an IEEE-754 'double' sl@0: For any positive integer n let X_n be the set of real numbers with an exact sl@0: decimal representation using n significant digits. sl@0: Let r_n : D -> X_n be defined by r_n(x)=y such that sl@0: |y-x| = inf { |z-x| : z in X_n } sl@0: and (in the case where two such y exist) that the last significant digit in the sl@0: decimal representation of y is even. sl@0: This constant is the least n such that r_n is injective. sl@0: */ sl@0: const TInt KIEEEDoubleInjectivePrecision=17; sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KMantissaBits=53; sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KMaxExponent=1023; sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KExponentBias=1022; sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KSpecialExponent=2047; sl@0: // sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The maximum exponent for a 32-bit floating point number. sl@0: */ sl@0: const TInt KTReal32MaxExponent=128; // changed from 127 sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The minimum exponent for a 32-bit floating point number. sl@0: */ sl@0: const TInt KTReal32MinExponent=-125; sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KTReal32ExponentBias=126; sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KTReal32SpecialExponent=255; // changed from KTReal32ExponentBad sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A zero exponent value for a 32-bit floating point number. sl@0: */ sl@0: const TInt KTReal32ZeroExponent=0; sl@0: // sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The maximum exponent for a 64-bit floating point number. sl@0: */ sl@0: const TInt KTReal64MaxExponent=1024; // changed from 1023 sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The minimum exponent for a 64-bit floating point number. sl@0: */ sl@0: const TInt KTReal64MinExponent=-1021; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KTReal64ExponentBias=1022; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TInt KTReal64SpecialExponent=2047; // changed from KTReal64BadExponent sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A zero exponent value for a 64-bit floating point number. sl@0: */ sl@0: const TInt KTReal64ZeroExponent=0; sl@0: // sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The minimum value of a 64-bit floating point number. sl@0: */ sl@0: const TReal KMinTReal=2.2250738585072015E-308; // changed from TReal64 sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The maximum value of a 64-bit floating point number. sl@0: */ sl@0: const TReal KMaxTReal=1.7976931348623157E+308; // sl@0: // sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The minimum value of a 32-bit floating point number. sl@0: */ sl@0: const TReal32 KMinTReal32=1.17549435E-38f; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The maximum value of a 32-bit floating point number. sl@0: */ sl@0: const TReal32 KMaxTReal32=3.4028234663852885981170418348452e+38f; sl@0: // sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The minimum value of a 64-bit floating point number. sl@0: */ sl@0: const TReal64 KMinTReal64=2.2250738585072015E-308; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The maximum value of a 64-bit floating point number. sl@0: */ sl@0: const TReal64 KMaxTReal64=1.7976931348623157E+308; sl@0: // sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TReal KSqhf=0.70710678118654752440; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: Log 2 to the base "e". sl@0: */ sl@0: const TReal KRln2=1.4426950408889634; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: Log 10 to the base "e". sl@0: */ sl@0: const TReal KRln10=0.4342944819032518; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: Log 2 to the base 10. sl@0: */ sl@0: const TReal KRlg2=0.3010299956639812; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The mathematical constant Pi. sl@0: */ sl@0: const TReal KPi=3.1415926535897932; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The reciprocal of the mathematical constant Pi. sl@0: */ sl@0: const TReal KPiInv=0.3183098861837907; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The mathematical constant Pi divided by 2. sl@0: */ sl@0: const TReal KPiBy2=1.5707963267948966; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: Not used. sl@0: */ sl@0: const TReal KDrpi=0.6366197723675813; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The square root of 3. sl@0: */ sl@0: const TReal KSqt3=1.7320508075688773; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: const TReal KMsq3=0.2679491924311227; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The multiplying factor to convert radians to degrees. sl@0: */ sl@0: const TReal KRadToDeg=57.29577951308232; sl@0: sl@0: sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: The multiplying factor to convert degrees to radians. sl@0: */ sl@0: const TReal KDegToRad=0.017453292519943296; sl@0: sl@0: sl@0: sl@0: sl@0: class TRealX sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A class encapsulating an extended precision real value. sl@0: sl@0: This class provides 64 bit precision and a dynamic range of approximately sl@0: 1E-9863 to 1E+9863. All member functions are optimized for speed. sl@0: */ sl@0: { sl@0: public: sl@0: enum TRealXOrder {ELessThan=1,EEqual=2,EGreaterThan=4,EUnordered=8}; sl@0: public: sl@0: IMPORT_C TRealX(); sl@0: IMPORT_C TRealX(TInt aInt); sl@0: IMPORT_C TRealX(TUint aInt); sl@0: IMPORT_C TRealX(TUint aExp, TUint aMantHi, TUint aMantLo); sl@0: IMPORT_C TRealX(const TInt64 &aInt); sl@0: IMPORT_C TRealX(TReal32 aReal) __SOFTFP; sl@0: IMPORT_C TRealX(TReal64 aReal) __SOFTFP; sl@0: IMPORT_C TRealX &operator=(TInt aInt); sl@0: IMPORT_C TRealX &operator=(TUint aInt); sl@0: IMPORT_C TRealX &operator=(const TInt64& aInt); sl@0: IMPORT_C TRealX &operator=(TReal32 aReal) __SOFTFP; sl@0: IMPORT_C TRealX &operator=(TReal64 aReal) __SOFTFP; sl@0: IMPORT_C TInt Set(TInt aInt); sl@0: IMPORT_C TInt Set(TUint aInt); sl@0: IMPORT_C TInt Set(const TInt64& aInt); sl@0: IMPORT_C TInt Set(TReal32 aReal) __SOFTFP; sl@0: IMPORT_C TInt Set(TReal64 aReal) __SOFTFP; sl@0: IMPORT_C operator TInt() const; sl@0: IMPORT_C operator TUint() const; sl@0: IMPORT_C operator TInt64() const; sl@0: IMPORT_C operator TReal32() const __SOFTFP; sl@0: IMPORT_C operator TReal64() const __SOFTFP; sl@0: IMPORT_C TInt GetTReal(TReal32 &aVal) const; sl@0: IMPORT_C TInt GetTReal(TReal64 &aVal) const; sl@0: IMPORT_C void SetZero(TBool aNegative=EFalse); sl@0: IMPORT_C void SetNaN(); sl@0: IMPORT_C void SetInfinite(TBool aNegative); sl@0: IMPORT_C TBool IsZero() const; sl@0: IMPORT_C TBool IsNaN() const; sl@0: IMPORT_C TBool IsInfinite() const; sl@0: IMPORT_C TBool IsFinite() const; sl@0: IMPORT_C const TRealX &operator+=(const TRealX &aVal); sl@0: IMPORT_C const TRealX &operator-=(const TRealX &aVal); sl@0: IMPORT_C const TRealX &operator*=(const TRealX &aVal); sl@0: IMPORT_C const TRealX &operator/=(const TRealX &aVal); sl@0: IMPORT_C const TRealX &operator%=(const TRealX &aVal); sl@0: IMPORT_C TInt AddEq(const TRealX &aVal); sl@0: IMPORT_C TInt SubEq(const TRealX &aVal); sl@0: IMPORT_C TInt MultEq(const TRealX &aVal); sl@0: IMPORT_C TInt DivEq(const TRealX &aVal); sl@0: IMPORT_C TInt ModEq(const TRealX &aVal); sl@0: IMPORT_C TRealX operator+() const; sl@0: IMPORT_C TRealX operator-() const; sl@0: IMPORT_C TRealX &operator++(); sl@0: IMPORT_C TRealX operator++(TInt); sl@0: IMPORT_C TRealX &operator--(); sl@0: IMPORT_C TRealX operator--(TInt); sl@0: IMPORT_C TRealX operator+(const TRealX &aVal) const; sl@0: IMPORT_C TRealX operator-(const TRealX &aVal) const; sl@0: IMPORT_C TRealX operator*(const TRealX &aVal) const; sl@0: IMPORT_C TRealX operator/(const TRealX &aVal) const; sl@0: IMPORT_C TRealX operator%(const TRealX &aVal) const; sl@0: IMPORT_C TInt Add(TRealX& aResult,const TRealX &aVal) const; sl@0: IMPORT_C TInt Sub(TRealX& aResult,const TRealX &aVal) const; sl@0: IMPORT_C TInt Mult(TRealX& aResult,const TRealX &aVal) const; sl@0: IMPORT_C TInt Div(TRealX& aResult,const TRealX &aVal) const; sl@0: IMPORT_C TInt Mod(TRealX& aResult,const TRealX &aVal) const; sl@0: IMPORT_C TRealXOrder Compare(const TRealX& aVal) const; sl@0: inline TBool operator==(const TRealX &aVal) const; sl@0: inline TBool operator!=(const TRealX &aVal) const; sl@0: inline TBool operator>=(const TRealX &aVal) const; sl@0: inline TBool operator<=(const TRealX &aVal) const; sl@0: inline TBool operator>(const TRealX &aVal) const; sl@0: inline TBool operator<(const TRealX &aVal) const; sl@0: public: sl@0: /** sl@0: The mantissa. sl@0: */ sl@0: // Represented as two adjacent 32 bit values, rather than one 64 value. sl@0: // This is to avoid EABI introduced padding overheads and BC breakages. sl@0: // This representation works because the mantissa is always accessed from sl@0: // assembler code as two 32 bit quantities. The C++ code that accesses it sl@0: // now constructs an automatic TInt64 with the two components. sl@0: TUint32 iMantLo; sl@0: TUint32 iMantHi; sl@0: sl@0: /** sl@0: The sign: 0 for +, 1 for - sl@0: */ sl@0: TInt8 iSign; sl@0: sl@0: /** sl@0: Flags: 0 for exact, 1 for rounded down, 2 for rounded up sl@0: */ sl@0: TUint8 iFlag; sl@0: sl@0: /** sl@0: Exponent: biased by 32767, iExp=0 => zero, +65535 => infinity or NaN sl@0: */ sl@0: TUint16 iExp; sl@0: }; sl@0: sl@0: sl@0: sl@0: sl@0: struct SPoly sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A structure containing the set of coefficients for a polynomial. sl@0: sl@0: @see Math::Poly sl@0: */ sl@0: { sl@0: TInt num; sl@0: TReal c[1]; sl@0: }; sl@0: sl@0: sl@0: sl@0: sl@0: class Math sl@0: /** sl@0: @publishedAll sl@0: @released sl@0: sl@0: A collection of mathematical functions. sl@0: */ sl@0: { sl@0: public: sl@0: IMPORT_C static TInt ACos(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt ASin(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt ATan(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt ATan(TReal &aTrg,const TReal &aSrcY,const TReal &aSrcX); sl@0: IMPORT_C static TInt Cos(TReal &aTrg,const TReal &aSrc); sl@0: sl@0: /** sl@0: This function is not implemented by Symbian OS. sl@0: */ sl@0: IMPORT_C static TInt DtoR(TReal &aTrg,const TDesC &aSrc,TInt &aPos,const TChar aPoint); sl@0: IMPORT_C static TInt Exp(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Frac(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Int(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Int(TInt16 &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Int(TInt32 &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Log(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Ln(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Mod(TReal &aTrg,const TReal &aSrc,const TReal &aModulus); sl@0: IMPORT_C static TReal Poly(TReal aVal,const SPoly *aPoly) __SOFTFP; sl@0: IMPORT_C static TInt Pow(TReal &aTrg,const TReal &aSrc,const TReal &aPower); sl@0: IMPORT_C static TInt Pow10(TReal &aTrg,const TInt exp); sl@0: IMPORT_C static TInt Rand(TInt64 &aSeed); sl@0: IMPORT_C static TReal FRand(TInt64 &aSeed) __SOFTFP; sl@0: IMPORT_C static TUint32 Random(); sl@0: IMPORT_C static TInt Round(TReal &aTrg,const TReal &aSrc,TInt aDecimalPlaces); sl@0: IMPORT_C static TInt Sin(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Sqrt(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TInt Tan(TReal &aTrg,const TReal &aSrc); sl@0: IMPORT_C static TBool IsZero(const TReal &aVal); sl@0: IMPORT_C static TBool IsNaN(const TReal &aVal); sl@0: IMPORT_C static TBool IsInfinite(const TReal &aVal); sl@0: IMPORT_C static TBool IsFinite(const TReal &aVal); sl@0: IMPORT_C static void PolyX(TRealX& aY, const TRealX& aX, TInt aDeg, const TRealX *aCoef); sl@0: static TInt MultPow10X(TRealX& aTrg, TInt aPower); sl@0: IMPORT_C static void Mul64(Int64 aX, Int64 aY, Int64& aOutH, Uint64& aOutL); sl@0: IMPORT_C static void UMul64(Uint64 aX, Uint64 aY, Uint64& aOutH, Uint64& aOutL); sl@0: IMPORT_C static Int64 DivMod64(Int64 aDividend, Int64 aDivisor, Int64& aRemainder); sl@0: IMPORT_C static Uint64 UDivMod64(Uint64 aDividend, Uint64 aDivisor, Uint64& aRemainder); sl@0: private: sl@0: IMPORT_C static void SetZero(TReal &aVal,TInt aSign=0); sl@0: IMPORT_C static void SetNaN(TReal &aVal); sl@0: IMPORT_C static void SetInfinite(TReal &aVal,TInt aSign); sl@0: }; sl@0: sl@0: #include sl@0: sl@0: #endif // __E32MATH_H__