sin20度16分用计算机怎么算,使用20项计算sin(x)的近似值

我试图用泰勒近似来评估1.89的sin的近似值。我将输出与Math.sin(x)的值进行了比较;然而,在大约14个术语之后,我的价值偏离很大并且变得错误。我尝试了x的较小值(< 0.5)的近似值,并且这些值相匹配。使用20项计算sin(x)的近似值

我只是想弄清楚为什么在Mac OSx上使用崇高和通过bash编译的Java偏离了真正的输出。

public class Test {

public static void main (String[] args) {

double x = 1.89;

double sinx = 0;

int n = 20;

int countOdd = 1;

int counter = 1;

int result = 0;

int value = 0;

while (countOdd <= n) {

if (counter%2 != 0) {

// Even term odd number

if (countOdd%2 == 0) {

sinx = sinx - (Math.pow(x,counter)/(double)factorial(counter));

System.out.println(" counter even odd term = " + countOdd);

countOdd++;

System.out.println(" sinx = " + sinx);

}

// Odd term odd number

else {

sinx = sinx + (Math.pow(x,counter)/(double)factorial(counter));

System.out.println(" counter odd odd term = " + countOdd);

countOdd++;

System.out.println(" sinx = " + sinx);

}

}

// Update the result and reset the value

//sinx = sinx + value;

//value = 0;

System.out.println(" counter = " + counter);

counter++;

}

System.out.println(" sinx = " + sinx);

System.out.println(" sinx from math library = " + Math.sin(x));

}

/** calcutes and returns n!

@param n : a positive integer number

@return n!

*/

public static int factorial(int n)

{

// your code goes here

int result = 1; // if n = 0, while loop is by passed and 0 is returned

while (n >= 1) {

result = result * (n);

n--;

}

return result;

}

}

2017-08-25

Diante

+2

尝试打印出''n'的'factorial'函数的结果。 –

+0

@AndyTurner,感谢您的建议,我将运行一个循环,并比较以查看函数是否按预期工作,我尝试了单点值并在初始停止 –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值