首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求整型的cos函数代码!该如何解决

2012-02-09 
求整型的cos函数代码!!! 不用浮点不知道哪位老大有啊?[解决办法]帮你搜了一下/**Sine N,ICalculatessin(

求整型的cos函数代码!!!

不用浮点
不知道哪位老大有啊?

[解决办法]
帮你搜了一下
/*
* Sine <N,I> Calculates sin(2*Pi*I/N)
* Cos <N,I> Calculates cos(2*Pi*I/N)
*/

/*
* First, compile-time trig functions sin(x) and cos(x).
*/

template <unsigned N, unsigned I>
class Sine {
public:
static inline float sin()
{
// This is a series expansion for sin(I*2*M_PI/N)
// Since all of these quantities are known at compile time, it gets
// simplified to a single constant, which can be included in the code:
// mov dword ptr es:[bx],large 03F3504F3h

return (I*2*M_PI/N)*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/2/3*(1-(I*2*M_PI/N)*
(I*2*M_PI/N)/4/5*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/6/7*(1-(I*2*M_PI/N)*
(I*2*M_PI/N)/8/9*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/10/11*(1-(I*2*M_PI/N)*
(I*2*M_PI/N)/12/13*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/14/15*
(1-(I*2*M_PI/N)*(I*2*M_PI/N)/16/17*
(1-(I*2*M_PI/N)*(I*2*M_PI/N)/18/19*(1-(I*2*M_PI/N)*
(I*2*M_PI/N)/20/21))))))))));
}
};


template <unsigned N, unsigned I>
class Cosine {
public:
static inline float cos()
{
// This is a series expansion for cos(I*2*M_PI/N)
// Since all of these quantities are known at compile time, it gets
// simplified to a single number.
return 1-(I*2*M_PI/N)*(I*2*M_PI/N)/2*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/3/4*
(1-(I*2*M_PI/N)*(I*2*M_PI/N)/5/6*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/7/8*
(1-(I*2*M_PI/N)*(I*2*M_PI/N)/9/10*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/11/12*
(1-(I*2*M_PI/N)*(I*2*M_PI/N)/13/14*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/15/16*
(1-(I*2*M_PI/N)*(I*2*M_PI/N)/17/18*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/19/20*
(1-(I*2*M_PI/N)*(I*2*M_PI/N)/21/22*(1-(I*2*M_PI/N)*(I*2*M_PI/N)/23/24
)))))))))));
}

};
[解决办法]
查表吧 ······
[解决办法]
这个要用到函数的幂级数展开

cos(x)=[n为整数从0累加到正无穷](-1)的n次方,乘以(分子是x的2n次方,分母是2n的阶乘)

你可以取一个n比如100 1000之类保证精度 然后做一个循环累加就得到近似值了

还看不懂就翻翻本科时候的高等数学中级数一章吧
[解决办法]
查表

热点排行