First public contribution.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32\euser\maths\um_tan.cpp
21 #if defined(__USE_VFP_MATH) && !defined(__CPU_HAS_VFP)
22 #error __USE_VFP_MATH was defined but not __CPU_HAS_VFP - impossible combination, check variant.mmh
26 #ifndef __USE_VFP_MATH
28 LOCAL_D const TUint32 TanCoeffs[] =
30 0x2168C235,0xC90FDAA2,0x7FFF0000, // polynomial approximation to tan((pi/2)*x)
31 0x2DF4707D,0xA55DE731,0x7FFF0000, // for |x|<=0.25
32 0xA9A1A71A,0xA335E33B,0x7FFF0000,
33 0x0BB9E431,0xA2FFFCDD,0x7FFF0000,
34 0x3E523A39,0xA2FA3863,0x7FFF0000,
35 0x8A35C401,0xA2F9D38B,0x7FFF0000,
36 0x91269411,0xA2F16003,0x7FFF0000,
37 0xDA32CC78,0xA3A93B13,0x7FFF0000,
38 0x4FB88317,0x9A146197,0x7FFF0000,
39 0x0D787ECE,0xE131DEE5,0x7FFF0000
42 LOCAL_D const TUint32 Onedata[] = {0x00000000,0x80000000,0x7FFF0000}; // 1.0
43 LOCAL_D const TUint32 Halfdata[] = {0x00000000,0x80000000,0x7FFE0000}; // 0.5
44 LOCAL_D const TUint32 PiBy2Invdata[] = {0x4E44152A,0xA2F9836E,0x7FFE0000}; // 2/pi
49 EXPORT_C TInt Math::Tan(TReal& aTrg, const TReal& aSrc)
51 Calculates the tangent of a number.
53 @param aTrg A reference containing the result.
54 @param aSrc The argument of the tan function in radians.
56 @return KErrNone if successful, otherwise another of
57 the system-wide error codes.
60 // Calculate tan(aSrc) and write result to aTrg.
62 // Let x=aSrc/(pi/2). Throw away integer part, but if integer part odd
63 // then replace final result y with -1/y
64 // ( use identities tan(x+n*pi)=tan(x), tan(x+pi/2)=-1/tan(x) )
65 // Replace x with fractional part after division.
66 // If x>=0.5, replace x with 1-x and replace result y with 1/y
67 // ( use identity tan(pi/2-x)=1/tan(x) )
68 // If x>=0.25, replace x with 0.5-x and replace result y with (1-y)/(1+y)
69 // ( use identity tan(pi/4-x)=(1-tan(x))/(1+tan(x)) )
70 // Use polynomial approximation to calculate tan(pi*x/2) for |x|<=0.25
72 const TRealX& One = *(const TRealX*)Onedata;
73 const TRealX& Half = *(const TRealX*)Halfdata;
74 const TRealX& PiBy2Inv = *(const TRealX*)PiBy2Invdata;
84 if (n<KMaxTInt && n>KMinTInt)
99 PolyX(y,x*x,9,(const TRealX*)TanCoeffs);
107 y.iSign=TInt8(sign ^ (n&1));
108 return y.GetTReal(aTrg);
115 #else // __USE_VFP_MATH
117 // definitions come from RVCT math library
118 extern "C" TReal tan(TReal);
120 EXPORT_C TInt Math::Tan(TReal& aTrg, const TReal& aSrc)
122 if (aSrc<KMaxTInt && aSrc>KMinTInt)
125 if (Math::IsFinite(aTrg))