sl@0
|
1 |
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32\euser\maths\um_tan.cpp
|
sl@0
|
15 |
// Tangent.
|
sl@0
|
16 |
//
|
sl@0
|
17 |
//
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "um_std.h"
|
sl@0
|
20 |
|
sl@0
|
21 |
#if defined(__USE_VFP_MATH) && !defined(__CPU_HAS_VFP)
|
sl@0
|
22 |
#error __USE_VFP_MATH was defined but not __CPU_HAS_VFP - impossible combination, check variant.mmh
|
sl@0
|
23 |
#endif
|
sl@0
|
24 |
|
sl@0
|
25 |
|
sl@0
|
26 |
#ifndef __USE_VFP_MATH
|
sl@0
|
27 |
|
sl@0
|
28 |
LOCAL_D const TUint32 TanCoeffs[] =
|
sl@0
|
29 |
{
|
sl@0
|
30 |
0x2168C235,0xC90FDAA2,0x7FFF0000, // polynomial approximation to tan((pi/2)*x)
|
sl@0
|
31 |
0x2DF4707D,0xA55DE731,0x7FFF0000, // for |x|<=0.25
|
sl@0
|
32 |
0xA9A1A71A,0xA335E33B,0x7FFF0000,
|
sl@0
|
33 |
0x0BB9E431,0xA2FFFCDD,0x7FFF0000,
|
sl@0
|
34 |
0x3E523A39,0xA2FA3863,0x7FFF0000,
|
sl@0
|
35 |
0x8A35C401,0xA2F9D38B,0x7FFF0000,
|
sl@0
|
36 |
0x91269411,0xA2F16003,0x7FFF0000,
|
sl@0
|
37 |
0xDA32CC78,0xA3A93B13,0x7FFF0000,
|
sl@0
|
38 |
0x4FB88317,0x9A146197,0x7FFF0000,
|
sl@0
|
39 |
0x0D787ECE,0xE131DEE5,0x7FFF0000
|
sl@0
|
40 |
};
|
sl@0
|
41 |
|
sl@0
|
42 |
LOCAL_D const TUint32 Onedata[] = {0x00000000,0x80000000,0x7FFF0000}; // 1.0
|
sl@0
|
43 |
LOCAL_D const TUint32 Halfdata[] = {0x00000000,0x80000000,0x7FFE0000}; // 0.5
|
sl@0
|
44 |
LOCAL_D const TUint32 PiBy2Invdata[] = {0x4E44152A,0xA2F9836E,0x7FFE0000}; // 2/pi
|
sl@0
|
45 |
|
sl@0
|
46 |
|
sl@0
|
47 |
|
sl@0
|
48 |
|
sl@0
|
49 |
EXPORT_C TInt Math::Tan(TReal& aTrg, const TReal& aSrc)
|
sl@0
|
50 |
/**
|
sl@0
|
51 |
Calculates the tangent of a number.
|
sl@0
|
52 |
|
sl@0
|
53 |
@param aTrg A reference containing the result.
|
sl@0
|
54 |
@param aSrc The argument of the tan function in radians.
|
sl@0
|
55 |
|
sl@0
|
56 |
@return KErrNone if successful, otherwise another of
|
sl@0
|
57 |
the system-wide error codes.
|
sl@0
|
58 |
*/
|
sl@0
|
59 |
{
|
sl@0
|
60 |
// Calculate tan(aSrc) and write result to aTrg.
|
sl@0
|
61 |
// Algorithm:
|
sl@0
|
62 |
// Let x=aSrc/(pi/2). Throw away integer part, but if integer part odd
|
sl@0
|
63 |
// then replace final result y with -1/y
|
sl@0
|
64 |
// ( use identities tan(x+n*pi)=tan(x), tan(x+pi/2)=-1/tan(x) )
|
sl@0
|
65 |
// Replace x with fractional part after division.
|
sl@0
|
66 |
// If x>=0.5, replace x with 1-x and replace result y with 1/y
|
sl@0
|
67 |
// ( use identity tan(pi/2-x)=1/tan(x) )
|
sl@0
|
68 |
// If x>=0.25, replace x with 0.5-x and replace result y with (1-y)/(1+y)
|
sl@0
|
69 |
// ( use identity tan(pi/4-x)=(1-tan(x))/(1+tan(x)) )
|
sl@0
|
70 |
// Use polynomial approximation to calculate tan(pi*x/2) for |x|<=0.25
|
sl@0
|
71 |
|
sl@0
|
72 |
const TRealX& One = *(const TRealX*)Onedata;
|
sl@0
|
73 |
const TRealX& Half = *(const TRealX*)Halfdata;
|
sl@0
|
74 |
const TRealX& PiBy2Inv = *(const TRealX*)PiBy2Invdata;
|
sl@0
|
75 |
|
sl@0
|
76 |
TRealX x;
|
sl@0
|
77 |
TInt r=x.Set(aSrc);
|
sl@0
|
78 |
if (r==KErrNone)
|
sl@0
|
79 |
{
|
sl@0
|
80 |
TInt8 sign=x.iSign;
|
sl@0
|
81 |
x.iSign=0;
|
sl@0
|
82 |
x*=PiBy2Inv;
|
sl@0
|
83 |
TInt n=(TInt)x;
|
sl@0
|
84 |
if (n<KMaxTInt && n>KMinTInt)
|
sl@0
|
85 |
{
|
sl@0
|
86 |
TInt flags=(n&1)<<1;
|
sl@0
|
87 |
x-=TRealX(n);
|
sl@0
|
88 |
if (x.iExp>=0x7FFE)
|
sl@0
|
89 |
{
|
sl@0
|
90 |
x=One-x;
|
sl@0
|
91 |
flags^=2;
|
sl@0
|
92 |
}
|
sl@0
|
93 |
if (x.iExp>=0x7FFD)
|
sl@0
|
94 |
{
|
sl@0
|
95 |
x=Half-x;
|
sl@0
|
96 |
flags^=1;
|
sl@0
|
97 |
}
|
sl@0
|
98 |
TRealX y;
|
sl@0
|
99 |
PolyX(y,x*x,9,(const TRealX*)TanCoeffs);
|
sl@0
|
100 |
y*=x;
|
sl@0
|
101 |
if (flags==3)
|
sl@0
|
102 |
y=(One+y)/(One-y);
|
sl@0
|
103 |
else if (flags==2)
|
sl@0
|
104 |
y=One/y;
|
sl@0
|
105 |
else if (flags==1)
|
sl@0
|
106 |
y=(One-y)/(One+y);
|
sl@0
|
107 |
y.iSign=TInt8(sign ^ (n&1));
|
sl@0
|
108 |
return y.GetTReal(aTrg);
|
sl@0
|
109 |
}
|
sl@0
|
110 |
}
|
sl@0
|
111 |
SetNaN(aTrg);
|
sl@0
|
112 |
return KErrArgument;
|
sl@0
|
113 |
}
|
sl@0
|
114 |
|
sl@0
|
115 |
#else // __USE_VFP_MATH
|
sl@0
|
116 |
|
sl@0
|
117 |
// definitions come from RVCT math library
|
sl@0
|
118 |
extern "C" TReal tan(TReal);
|
sl@0
|
119 |
|
sl@0
|
120 |
EXPORT_C TInt Math::Tan(TReal& aTrg, const TReal& aSrc)
|
sl@0
|
121 |
{
|
sl@0
|
122 |
if (aSrc<KMaxTInt && aSrc>KMinTInt)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
aTrg = tan(aSrc);
|
sl@0
|
125 |
if (Math::IsFinite(aTrg))
|
sl@0
|
126 |
return KErrNone;
|
sl@0
|
127 |
}
|
sl@0
|
128 |
SetNaN(aTrg);
|
sl@0
|
129 |
return KErrArgument;
|
sl@0
|
130 |
}
|
sl@0
|
131 |
|
sl@0
|
132 |
#endif
|