C language-use Taylor formula to calculate the value of sinx (do not call library functions, define functions yourself)

tags: C# Brush Question Record [Beginner]  c language  algorithm

#include<stdio.h>
#include <math.h>
//First define a factorial function
double fac(double n){
    double f=0;
    if(n == 1 || n==0)
        f =1;
    else
        f = fac(n-1)*n;

    return f;
}
//An exponential function (power)
double kvat(double n,double ci){
    double chengf=1;
        for (int i=1; i<=ci; i++) {
            chengf = chengf*n;
        }
    return chengf;
}

double sinus(double x){
    int z =-1;
    int j =1;
    double sin=x;
    for (j =3;fabs( kvat(x,j)/fac(j) )>0.001 ; j+=2) {
//        printf("%lf\n",kvat(x, j));
        sin =sin + z*(kvat(x,j)/fac(j));
        z = -z;
    }
    return sin;
}
int main(){
    double jd,hudu;
    scanf("%lf",&jd);
    hudu = (jd*M_PI)/180;//M_PI here is pi pai in math library
    printf( "%.3lf",sinus(hudu));
    return 0;
}

Intelligent Recommendation

Character string turn, do not use any C language library functions

Today, a friend asked me, I wrote it, it is very simple, it is involved in some basic knowledge. The more you learn, the more you can't forget....

Calculate Cos value using Taylor formula

Calculate the cosine value from 0 to 359 degrees, and calculate the average need to calculate the size of the last n of the result, n starts from 0. As n increases, each item is also a very expensive ...

C Language: Realize string reverse functions (do not use library functions, only open space)

Record: strlen () function and printf ("% s", str) are all encountered with '\ 0' ending. And the parameters inside the printf () are from right to left. The result of running is 2,1 B,A...

Use c++ to call matlab functions written by yourself under Ubuntu

The blog is "Ubuntu related》The third part of the series of blogs. This series of blogs mainly records the installation of various software or libraries on Ubuntu, so as to facilitate the quick r...

More Recommendation

C language: calculate the solution of ax^2+bx+c=0, use three functions to calculate the root when delta is greater than, equal to, and less than 0 and store them in three sub-files respectively, enter the value of abc in the main function and Call functions

Source of the topic: Dagong Mu Lessonlink OF: Caleb Sung Topic requirements Calculate the solution of ax^2+bx+c=0, use three functions to calculate the root when delta is greater than, equal to, and l...

COSX SINX Taylor expansion C ++

COSX SINX Taylor expansion C ++ Common Taylor launches: The results are as follows: think:...

Functions in C language (library functions and custom functions)

The classification of functions in C language is generally two kinds of library functions and custom functions. First look at some examples of library functions: 1. In our C language learning process,...

c language library functions

Hello.txt file creation, and fill it writes "Hello, Software Weekly";...

C language: library functions

Task code: modf separates the integer part and the decimal part of a number: round rounds the decimal part to the integer part: Implementation: Knowledge summary: The library function is the function ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top