Advertisement
Roniere

Creating of math library in C programming language

Aug 12th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. // header File: math.h
  2. #ifndef MATH_H_INCLUDED
  3.     #define MATH_H_INCLUDED
  4.  
  5.     struct root{
  6.             float x_1;
  7.             float x_2;
  8.     };
  9.     int square(int x);
  10.     int factorial(int value);
  11.     float convertDegreeToRadius(float degree);
  12.     float sine(float angle);
  13.     float square_root(float x);
  14.  
  15.     struct root rootsSecondDegreeEquation(float a, float b, float c);
  16. #endif // MATH_H_INCLUDED
  17.  
  18. // .C file - Functions of library
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include "math.h"
  22.  
  23. #define PI 3.14159265359
  24.  
  25. //Calculate the square of a number
  26. int square(int value){
  27.     int i =0, result = 1;
  28.     for(i = 0; i < 2; i++)
  29.         result = result * value;
  30.     return result;
  31. }
  32.  
  33. int factorial(int value){
  34.     int i, result;
  35.     while (value < 0){
  36.         printf("Invalid value! Insert other value again: ");
  37.         scanf("%d",&value);
  38.     }
  39.     if (value == 1 || value == 0)
  40.         return 1;
  41.     else{
  42.         for(i = 2; i <= value; i++)
  43.             result = result * i;
  44.         return result;
  45.     }
  46. }
  47.  
  48. float convertDegreeToRadius(float degree){
  49.     float radius = 0;
  50.     radius = (degree * PI)/360;
  51.     return radius;
  52. }
  53.  
  54. float sine(float angle){
  55.     int i = 0, angle_3 = 1, angle_5 = 1, angle_7 = 1;
  56.     float sine = 0;
  57.  
  58.     for(i = 0; i < 3; i++)
  59.         angle_3 = angle_3 * angle;
  60.  
  61.     for(i = 0; i < 5; i++)
  62.         angle_5 = angle_5 * angle;
  63.  
  64.     for(i = 0; i < 7; i++)
  65.         angle_7 = angle_7 * angle;
  66.  
  67.     sine = angle - (angle_3/factorial(3)) - (angle_5/factorial(5)) - (angle_7/factorial(7));
  68.  
  69.     return sine;
  70. }
  71.  
  72. float square_root(float x){
  73.     int i;
  74.     float recorre = x;
  75.  
  76.     for (i = 0; i < 10; i++)
  77.           recorre = recorre/2 + x/(2*recorre);
  78.  
  79.     return recorre;
  80. }
  81.  
  82. struct root rootsSecondDegreeEquation (float a, float b, float c){
  83.  
  84.     struct root math;
  85.  
  86.     float delta = 0;
  87.     delta = (b * b)- (4 * a * c);
  88.  
  89.     if (delta < 0)
  90.         return math;
  91.     else{
  92.         if (a == 0 && b != 0){
  93.             math.x_1 = c / b;
  94.             return math;
  95.         }else{
  96.             if (delta == 0){
  97.                 math.x_1 = (-b) / (2 * a);
  98.                 return math;
  99.             }else{
  100.                 math.x_1 = ((-b) + square_root(delta)) / (2 * a);
  101.                 math.x_2 = ((-b) - square_root(delta)) / (2 * a);
  102.  
  103.                 return math;
  104.             }
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement