c - 为什么我得到结果=0.00000

标签 c variables

我正在尝试在不使用数学库的情况下计算正弦和余弦,泰勒级数女巫是
sinx =Σn=0 到 n=∞ (-1)^n * x^(2n+1)/(2n+1)!
此代码会生成任何输入sin 为 0,00000 cos 为 1,00000 这段代码的问题在哪里

#include <stdio.h>
#include <stdlib.h>

double fakt(int);
double power(int,int);
int negative_positive(int);
double calculate_sin(int,int);
double calculate_cos(int,int);
double calculate_radyan(int);

int main() {
    int degree,number;
    char command;
    do{
        scanf("%c",&command);
        if(command=='d' || command=='D'){
            scanf("%d %d",&degree,&number);
            double radyan = calculate_radyan(degree);
            printf("%lf \n%lf ",calculate_sin(radyan,number),calculate_cos(radyan,number));
        }
    }while(command!='e' && command!='E');

    return 0;
}

double fakt(int n){
    int i;
    double result=1;
    for(i=1;i<n;i++)
        result = result*i;

    return result;
}

double power(int base, int exponent){
    int i;
    double result=1;
    for(i=0;i<exponent;i++)
        result = result*base;

    return result;
}

int negative_positive(int number){
    if(number % 2 == 0)
        return 1;
    else
        return -1;
}

double calculate_sin(int degree , int n){
    int i;
    double result=0;
    double tmp=0;
    for(i=0;i<n;i++){
        tmp=negative_positive(i)*power(degree,2*i+1)/fakt(2*i+1);
        result=tmp+result;}
    return result;
}

double calculate_cos(int degree , int n){
    int i;
    double result=0;
    double tmp=0;
    for(i=0;i<n;i++)
    {
        tmp=negative_positive(i)*power(degree,i*2)/fakt(2*i);
        result=tmp+result;
    }

    return result;
}

double calculate_radyan(int degree){
    double result,pi=3.14159;
    result =pi/180*degree;

    return result;
}

最佳答案

我会回答我自己的问题,但我找到了解决方案。当程序尝试使用双弧度变量使用 power 方法时,它会将该变量转换为 0,因为该方法只接受整数变量 power(double base,int exponent) 解决了我的问题

关于c - 为什么我得到结果=0.00000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26759230/

相关文章:

c++ - 我应该使用图形库吗?

c - 逐行读取数组中的文件

c++ - 在程序中获取AIX中进程的虚拟内存大小

java - 函数调用之间的 OR 运算符

C#:在运行时获取值类型变量的大小?

arrays - 如何将 csv 中的特定元素存储到变量中?

c - C中的错误输出

PHP 5.5 : accessing a static class member of a dynamic class stored in an object

javascript - 从 div 属性获取变量值

python - 递归:看起来像相同的函数,但打印出不同的执行流程