c言語実装sin,cos,sqrt,pow関数

7269 ワード

 1 float abs(float x) 

 2 {

 3     if(x<0) x=0-x;

 4     return x;

 5 }

 6 

 7 

 8 float sin(float x) 

 9 

10 {

11 

12  const float B = 1.2732395447; 

13  const float C = -0.4052847346;

14  const float P = 0.2310792853;//0.225; 

15  float y = B * x + C * x * abs(x);

16  y = P * (y * abs(y) - y) + y;

17  return y;

18  }

19 

20 

21 float cos(float x) 

22 {

23   const float Q = 1.5707963268;

24   const float PI =3.1415926536;

25   x += Q; 

26 

27   if(x > PI) 

28     x -= 2 * PI; 

29 

30  return( sin(x));

31 }

32 

33 

34 float sqrt(float a)

35  {

36    double x,y;

37    x=0.0;

38    y=a/2;

39    while(x!=y)

40    {

41      x=y;

42      y=(x+a/x)/2;

43    }

44    return x;

45  }

46 

47 

48 float pow(float a,int b)

49 {

50     float r=a;

51     if(b>0)

52     {

53       while(--b)

54          r*=a;

55 

56     }

57     else if(b<0)

58     {

59         while(++b)     r*=a;

60          r=1.0/r;

61     }

62     else r=0;

63     return r;

64 }