简体   繁体   English

用Java计算正弦和余弦

[英]Computing sine and cosine in Java

import java.util.*;
import java.math.*;
public class SinCos{
    public static void main(String args[]){
        Scanner kb=new Scanner(System.in);
        System.out.println("Enter the angle for cosine: ");
        double anglecos=kb.nextDouble();
        System.out.println("Enter the number of expansions required:");
        double n=kb.nextDouble();
        System.out.println("Enter the angle for sine:");
        double anglesin=kb.nextDouble();
        System.out.println("Enter the number of expansions required:");
        double n2=kb.nextDouble();
        System.out.println("Cosine: "+workCos(anglecos,n));
        System.out.println("Sine: " +workSin(anglesin,n2));
    }

    public static double workCos(double angle, double num){
        double ans = 1;
        double ans2=0;

        for(int n = 1;((Math.round(ans * 10000.0) / 10000.0)==ans2)&&n>10; n++) {
            double times=2*n;
            ans2=Math.round(ans * 10000.0) / 10000.0;
            ans += Math.pow(-1, n) * Math.pow(angle, 2*n) / fact(times);
        }

        return ans;
    }

    public static double workSin(double angle, double num){
        double ans = 0;
        double ans2=0;
        double t;
        double t2;

        for(int k=0;(Math.round(ans * 10000.0) / 10000.0)==ans2;k++){
            ans2=Math.round(ans * 10000.0) / 10000.0;
            double times=2*k+1;
            ans += Math.pow(-1, k - 1) * Math.pow(angle, 2*k - 1) / fact(times);


        } return ans;
    }

    public static double fact(double num){
        if(num==0||num==1){
            return 1;
        }
        else{
            return num* fact(num-1);
        } 
    }
}

I have been trying to compute sine and cosine using Taylor's theorem. 我一直在尝试使用泰勒定理计算正弦和余弦。 However, the answers I am given at the end are nowhere near the actual answer. 但是,我最后给出的答案与实际答案相去甚远。

The last method in my code gives the factorials. 我代码中的最后一个方法提供阶乘。 The other two methods compute the values using Taylor's theorem. 另两种方法使用泰勒定理计算值。 The idea in these two methods is to keep computing until the answers don't vary too much from each other. 这两种方法的想法是保持计算,直到答案彼此之间的差异不大。

I've long forgotten the Taylor's theorem (tho I knew it a while back), so I won't delve deeper into your implementation of it. 我早就忘记了泰勒定理(我早就知道了),所以我不会更深入地研究您的实现。 However, I do see this: 但是,我确实看到了这一点:

for(int n = 1;((Math.round(ans * 10000.0) / 10000.0)==ans2)&&n>10; n++)

Which makes no sense since this loop will exit immediately because of && n>10 which is not true in the first passing through it. 这是没有意义的,因为&& n>10在第一个循环中不正确,因此该循环将立即退出。 Also check the second for loop since it also does not check the value of int k = 0 declared in the for loop. 还要检查第二个for循环,因为它也不检查for循环中声明的int k = 0的值。 I'm certain that is the problem, might not be the only one though. 我敢肯定这就是问题所在,虽然可能不是唯一的问题。

Firstly, I agree with Vucko, you should get rid of n>10 but I assume this is just a mistake. 首先,我同意Vucko的观点,您应该摆脱n> 10,但是我认为这只是一个错误。

I'm assuming this part is an attempt to avoid an overflow error. 我假设这部分是为了避免溢出错误。

((Math.round(ans * 10000.0) / 10000.0)==ans2)

This should be changed since you probably want the loop to stop once this is true and keep going if this is false. 应当更改此设置,因为您可能希望一旦正确就停止循环,如果错误就继续执行。 In a for loop for(A;B;C), the loop will keep going if B is true, hence you should change it to the following. 在for(A; B; C)的for循环中,如果B为true,则循环将继续进行,因此应将其更改为以下内容。

((Math.round(ans * 10000.0) / 10000.0)!=ans2)

How about manually checking for Nans instead (I cleaned up your code a little while I was at it although it could be a lot more efficient) 手动检查Nans怎么样(我在整理代码的同时清理了一点,尽管它可能更有效)

public static double workCos(double angle, double num){
    double ans = 0;
    for(int n = 0; n < num; n++) {
        double currAns = Math.pow(-1, n) * Math.pow(angle, 2*n) / fact(2*n);
        if(Double.isNaN(currAns))
            break;
        ans += currAns;
    }
    return ans;
}

对于泰勒级数,输入必须为弧度而不是度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM