Recommended Posts

Hi, im pretty new with boostC, and I was wondering if there is a way to get the cosinus, sinus and tangens of a value. Thx in advance.

Share on other sites

I don't believe there is a native math library that has trigonometric functions for BoostC.

You can always create a lookup table method similar to this:

http://plans.thefrankes.com/tutorials/picmicrotrig/

The author used assembly, but you can use the theory and the lookup table he already generated. I think what he did was a pretty clever idea to overcome the limitation of doing trigonometry on an 8-bit controller.

- Bill

Share on other sites

Here is a little bit of code I was playing around with last year. It may need some tweaking and refining but should do the job if you are lucky.

```#define PI_OVER_TWO	1.570796326794896

/************************************************************
*   Calculate cosine of the fp number x.					*
*														   *
*   x is in radians.										*
************************************************************/
float cos(float x)
{
float_rounding_mode = float_round_down;

float dnom = {			// Denominator table.
-0.5,					// -1/2! = -1/2
0.041666666667,		//  1/4! =  1/24
-0.0013888888888,		// -1/6! = -1/720
0.0000248015873016		//  1/8! =  1/40320
};
float x2;
float acc;					// accumulator.
float temp;
unsigned char i;			// iteration counter.

acc = x;					// take local copy of x.
asm {
bcf _acc+3,7				// make x a positive value.
}

// get total number of quadrants in our angle.
temp = float32_div(acc , PI_OVER_TWO); // x/(PI/2) or x/90deg.
// get whole quadrant count. (Hopefully < 256)
// get the quadrant for our angle.

case 0:
case 2:
x2 = float32_mul(x2, PI_OVER_TWO);
break;
case 1:
x2 = float32_mul(float32_sub(1.0, x2), PI_OVER_TWO);
break;
default:
x2 = float32_mul(float32_sub(x2, 1.0), PI_OVER_TWO);
break;
}

//	Do Taylor series over 4 iterations.
//	1 - x2/2! + x4/4! - x6/6! + x8/8!
temp = acc = 1.0;			// set initial value for series = 1.
x2 = float32_mul(x2, x2);		// calc. x2 (x squared).

for (i = 0; i <= 3; i++)
{
temp = float32_mul(temp, x2); // calc. numerator (x2, x4, x6, x8).
acc = float32_add(acc, float32_mul(dnom[i], temp)); // accumulate results.
}

asm {
movlw 0x80				  // correct sign
xorwf _acc+3,F
}

return acc; // return cos(x).
}

/************************************************************
*   Calculate sine of the fp number x.					  *
*														   *
*   x is in radians.										*
************************************************************/
float sin(float x)
{
float_rounding_mode = float_round_down;

return cos(float32_sub(x, PI_OVER_TWO)); // return cos(x) - PI/2.
}

/************************************************************
*   Calculate tangent of the fp number x.				   *
*														   *
*   x is in radians.										*
************************************************************/
float tan(float x)
{
float c, s;

float_rounding_mode = float_round_down;

c = cos(x);
if (float32_eq(c, 0.0))
return 1000000000000000000000000000000000000.0;//(1.0e+36)

s = sin(x);
return(float32_div(s, c)); // return sin(x)/cos(x).
}```

You could use a Pade approximation rather than Taylor series. Don't expect super accuracy, you have to decide on speed or accuracy.

Cheers

Reynard

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.