那位大侠能给我sin(x)函数的算法?

js_nicle 2004-06-23 10:29:17
我已经前用数学函数都是用编程语言自带的数学函数库,现在因为要用到一门新的查询语言,没有数学函数,需要自己写,请各位大侠帮忙写几个三角函数的算法(sin cos)
...全文
480 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
shines77 2004-07-08
  • 打赏
  • 举报
回复
up
js_nicle 2004-07-01
  • 打赏
  • 举报
回复
谢谢各位,我采用了NowCan(((((( ★ )))))) 的方法,谢谢如果有更好的算法请mail给我
js_nicle@hotmail.com 在另外开贴酬谢,

本贴就此揭帖,再次谢谢各位。
NowCan 2004-06-24
  • 打赏
  • 举报
回复
没有用过XQuery。其实cos要是精度要求不高的话,很简单的。抄来的。

#define R1 -0.1666665668e+0
#define R2 0.8333025139e-2
#define R3 -0.1980741872e-3
#define R4 0.2601903036e-5

/****************************************************************************/
/* COS() - Cosine */
/* */
/* Based on the algorithm from "Software Manual for the Elementary */
/* Functions", Cody and Waite, Prentice Hall 1980, chapter 8. */
/* */
/* N = round(x / PI + 1/2) - 0.5 */
/* f = x - N * PI */
/* g = f * f */
/* R = polynomial expression */
/* */
/* result = f + f * R */
/* if N is even, result = - result */
/* */
/* This will return the wrong result for x >= MAXINT * PI */
/****************************************************************************/
double cos(double x)
{
int sign; /* the sign of the result */
double xn, f, g, rg, result;
int n;

/**************************************************************************/
/* cos(x) = cos(-x) */
/**************************************************************************/
x = fabs(x);

/**************************************************************************/
/* n = round(x/PI + 1/2) (can be rounded this way, since positive number) */
/**************************************************************************/
n = (int) (((x + HALFPI) * INVSPI) + 0.5);
xn = (double) n - 0.5;

/**************************************************************************/
/* if n is odd, negate the sign */
/**************************************************************************/
sign = n % 2;

/**************************************************************************/
/* f = x - xn * PI (but more mathematically stable) */
/**************************************************************************/
f = (x - xn * C1) - xn * C2;

/**************************************************************************/
/* determine polynomial expression */
/**************************************************************************/
g = f * f;

rg = (((R4 * g + R3) * g + R2) * g + R1) * g;

result = f + f * rg;
return sign ? -result : result;
}
programfanny 2004-06-24
  • 打赏
  • 举报
回复
对不起,是FCOS
programfanny 2004-06-24
  • 打赏
  • 举报
回复
在汇编中使用协处理器的指令超越指令FSIN SCOS FSINCOS FPTAN FPATAN 做
js_nicle 2004-06-24
  • 打赏
  • 举报
回复
泰勒级数展开式是一种方法,但是在我这里不可用

不知道大家有没有用过XQuery语言,XQuery不支持主要的数学函数,我要用到的时候需要自己定义一个函数来代替三角函数,而Xquery处理起泰勒展开式,天才知道会出现什么结果。
NowCan 2004-06-24
  • 打赏
  • 举报
回复
缺几个
#define INVSPI 0.31830988618379067154
#define HALFPI 1.57079632679489661923

#define C1 3.140625
#define C2 9.67653589793e-4


/****************************************************************************/
/* SIN() - sine */
/* */
/* Based on the algorithm from "Software Manual for the Elementary */
/* Functions", Cody and Waite, Prentice Hall 1980, chapter 8. */
/* */
/* N = round(x / PI) */
/* f = x - N * PI */
/* g = f * f */
/* R = polynomial expansion */
/* */
/* result = f + f * R */
/* */
/* if x < 0, result = - result */
/* if N is even, result = - result */
/* */
/* This will return the wrong result for x >= MAXINT * PI */
/****************************************************************************/
double sin(double x)
{
double xn, f, g, rg, result;
int sign = (x < 0);
int n;

x = fabs(x);
n = (int) ((x * INVSPI) + 0.5);
xn = (double) n;

/*************************************************************************/
/* if n is odd, negate the sign */
/*************************************************************************/
sign ^= n % 2;

/*************************************************************************/
/* f = x - xn * PI (but mathematically more stable) */
/*************************************************************************/
f = (x - xn * C1) - xn * C2;

/*************************************************************************/
/* determine polynomial expression */
/*************************************************************************/
g = f * f;

rg = (((R4 * g + R3) * g + R2) * g + R1) * g;

result = f + f * rg;
return sign ? -result : result;
}
solomon1 2004-06-23
  • 打赏
  • 举报
回复
用泰勒级数展开就可以算了
whycadi 2004-06-23
  • 打赏
  • 举报
回复
基本上,没有谁真正用以上两个式子来算,因为它们的收敛速度是很“悲哀”的。
如果是X86体系的计算机,它的协处理器(FPU)是有三角函数以及指数函数的硬件计算指令的。
FSIN,FCOS,FSINCOS,FATAN……等等。我不是很了解汇编,搂主可以到网上查找一下,也可以到汇编网站上去问。

如果不是X86体系的计算机,可以去找本C语言程序集,里面一般都有这些函数。
  • 打赏
  • 举报
回复
对啊
同意楼上的
展开
mmmcd 2004-06-23
  • 打赏
  • 举报
回复
sin(x)=x - x^3/3! + x^5/5! - x^7/7! + ... + (-1)^(k+1)* x^(2k-1)/(2k-1)!

cos(x)=1 - x^2/2! + x^4/4! - x^6/6! + ... + (-1)^k * x^(2k)/(2k)!

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧