Guys and gals,
This is more a question for those C programmers out there. We are
programming in Dynamic C uploading code to a AVLS (vehicle location system)
microprocessor. The guys have been having some problems....as described
below. If anybody has any thoughts - who works in this kind of low-level
environement we would so very grateful to hear from you!
Gareth Hardman
Saturn Technologies
-----Original Message-----
From: Mark Jones [SMTP:[EMAIL PROTECTED]]
Sent: Thursday, January 18, 2001 11:38 AM
To: Gareth Hardman; [EMAIL PROTECTED]
Subject: Calculating the Cosine fn
This is the code that supposedly calculates cosine. It's error compared to
the built-in cos function in Visual C can be summed up by the fact that it
calculates cos(0) as 0.94921... Error across the entire domain of -pi/2 to
+pi/2 is unacceptably large. The results should be accurate to 16 DP as >
16 iterations are performed and double-precision floating point numbers are
being used.
The algorithm uses the CORDIC method utilised by calculators.
I have attached an html doc holding links to all my sources on CORDIC
algorithms.
double cordic_cos(double z)
{
// domain of z: -pi/2 to +pi/2, measured in radians
int iPrecision=26;
double x, y, d;
x=1.0/1.646760258121065L;
y=0;
double dblPowerOf2=1;
for (int i=0; i<=iPrecision; i++)
{
d = (z>=0 ? 1 : -1);
x = x - d * y * dblPowerOf2;
y = y + d * x * dblPowerOf2;
z = z - d * atan(dblPowerOf2);
dblPowerOf2/=2;
}
return x;
}
|
This is the code that supposedly calculates cosine.
It's error compared to the built-in cos function in Visual C can be summed up by
the fact that it calculates cos(0) as 0.94921... Error across the entire domain
of -pi/2 to +pi/2 is unacceptably large. The results should be accurate to 16 DP
as > 16 iterations are performed and double-precision floating point
numbers are being used.
The algorithm uses the CORDIC method utilised
by calculators.
I have attached an html doc holding links to all my
sources on CORDIC algorithms.
double cordic_cos(double z)
{ // domain of z: -pi/2 to +pi/2, measured in radians int iPrecision=26;
double x, y, d; x=1.0/1.646760258121065L;
y=0; double dblPowerOf2=1;
for (int i=0; i<=iPrecision;
i++)
{ d = (z>=0 ? 1 : -1); x = x - d * y *
dblPowerOf2;
y = y + d * x * dblPowerOf2; z = z - d * atan(dblPowerOf2); dblPowerOf2/=2;
} return x;
} |
Bookmarks
- Approximating Arcsin
- Methods for computing trig functions
- CORDIC Method
- CORDIC (COordinate Rotation DIgital Computer)
- References on CORDIC Methods
- Dr Dobbs Cordic code in C
- atan2(y, x) definition
- Calculating ArcSin
- Getting ArcSin from ArcTan
- Cordic Algorithm Survey PDF
- Survey of Cordic Articles
- Methods for computing trig functions
