在 Java 中制作自定义 Sin() 函数

发布于 2024-07-08 03:38:01 字数 328 浏览 10 评论 0原文

我必须在我的计算机科学课程中从头开始创建 sin 函数,并且我即将找到解决方案。 但是,我仍然遇到一些问题。 如果我输入 0.5PI 或更小的值,它就可以工作,但否则我会得到不正确的结果。 这是我到目前为止的代码:

double i=1;
double sinSoFar = 0;
int term = 1;
while(i >= .000001)
{
    i = pow(-1, term + 1) * pow(sinOf, 2*term-1) / factorial(2*term-1);
    sinSoFar=sinSoFar + i;
    term++;
}

I have to create the sin function from scratch in my Comp Sci class, and I am getting close to a solution. However, I am still having a few problems. If I put in a value of .5PI or less it works, but otherwise I get the incorrect result. Here is the code I have so far:

double i=1;
double sinSoFar = 0;
int term = 1;
while(i >= .000001)
{
    i = pow(-1, term + 1) * pow(sinOf, 2*term-1) / factorial(2*term-1);
    sinSoFar=sinSoFar + i;
    term++;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀子花开つ 2024-07-15 03:38:01

就像 Federico 指出的那样,问题可能出在你的 Factorial() 或 pow() 中。 我运行了一个测试,效果很好,用 Math 类中提供的 pow() 函数替换你的函数,以及这个阶乘():

public static long factorial(long n) {
        if      (n <  0) throw new RuntimeException("Underflow error in factorial");
        else if (n > 20) throw new RuntimeException("Overflow error in factorial");
        else if (n == 0) return 1;
        else             return n * factorial(n-1);
} 

Like Federico pointed, the problem probably is in your factorial() or pow(). I ran a test that worked fine replacing your functions with the pow() function provided in the Math class, and this factorial():

public static long factorial(long n) {
        if      (n <  0) throw new RuntimeException("Underflow error in factorial");
        else if (n > 20) throw new RuntimeException("Overflow error in factorial");
        else if (n == 0) return 1;
        else             return n * factorial(n-1);
} 
萌逼全场 2024-07-15 03:38:01

一些建议:

  • 从 term = 0 开始。规范的 MacLaurin 展开式也会
  • 在循环时计算幂和阶乘(即在每一步更新它们)。 也许问题出在 pow() 或 Factorial() 内。

编辑。 建议:计算完第 k 项后,您可以通过以下方式计算第 (k+1) 项:

  • (-1)
  • 乘以 sinOf^2
  • 除以 (2k+2)(2k+3)

乘以 这样您就可以完全避免计算幂和阶乘。

Some advices:

  • Start with term = 0. The canonical MacLaurin expansion also does
  • compute the powers and the factorial while you are cycling (that is, updating them at each step). Maybe the problem is within pow() or factorial().

EDIT. Suggestion: once you have computed the k-th term, you can compute the (k+1)-th one by:

  • Multiplying by (-1)
  • Multiplying by sinOf^2
  • Dividing by (2k+2)(2k+3)

In this way you can completely avoid the computation of powers and factorials.

面如桃花 2024-07-15 03:38:01

对于 0 - 1/2PI 之外的值,都可以根据该范围内的值进行计算。

// First, normalize argument angle (ang) to -PI to PI, 
// by adding/subtracting 2*PI until it's within range
if ( ang > 1/2PI ) {
    sin = sin ( PI - ang );
}
else if ( ang < 0 ) {
    sin = -1 * sin( -1 * ang );
}
else {
    // your original code
}

As far as values outside of 0 - 1/2PI, they can all be computed from values inside the range.

// First, normalize argument angle (ang) to -PI to PI, 
// by adding/subtracting 2*PI until it's within range
if ( ang > 1/2PI ) {
    sin = sin ( PI - ang );
}
else if ( ang < 0 ) {
    sin = -1 * sin( -1 * ang );
}
else {
    // your original code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文