Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // header File: math.h
- #ifndef MATH_H_INCLUDED
- #define MATH_H_INCLUDED
- struct root{
- float x_1;
- float x_2;
- };
- int square(int x);
- int factorial(int value);
- float convertDegreeToRadius(float degree);
- float sine(float angle);
- float square_root(float x);
- struct root rootsSecondDegreeEquation(float a, float b, float c);
- #endif // MATH_H_INCLUDED
- // .C file - Functions of library
- #include <stdio.h>
- #include <stdlib.h>
- #include "math.h"
- #define PI 3.14159265359
- //Calculate the square of a number
- int square(int value){
- int i =0, result = 1;
- for(i = 0; i < 2; i++)
- result = result * value;
- return result;
- }
- int factorial(int value){
- int i, result;
- while (value < 0){
- printf("Invalid value! Insert other value again: ");
- scanf("%d",&value);
- }
- if (value == 1 || value == 0)
- return 1;
- else{
- for(i = 2; i <= value; i++)
- result = result * i;
- return result;
- }
- }
- float convertDegreeToRadius(float degree){
- float radius = 0;
- radius = (degree * PI)/360;
- return radius;
- }
- float sine(float angle){
- int i = 0, angle_3 = 1, angle_5 = 1, angle_7 = 1;
- float sine = 0;
- for(i = 0; i < 3; i++)
- angle_3 = angle_3 * angle;
- for(i = 0; i < 5; i++)
- angle_5 = angle_5 * angle;
- for(i = 0; i < 7; i++)
- angle_7 = angle_7 * angle;
- sine = angle - (angle_3/factorial(3)) - (angle_5/factorial(5)) - (angle_7/factorial(7));
- return sine;
- }
- float square_root(float x){
- int i;
- float recorre = x;
- for (i = 0; i < 10; i++)
- recorre = recorre/2 + x/(2*recorre);
- return recorre;
- }
- struct root rootsSecondDegreeEquation (float a, float b, float c){
- struct root math;
- float delta = 0;
- delta = (b * b)- (4 * a * c);
- if (delta < 0)
- return math;
- else{
- if (a == 0 && b != 0){
- math.x_1 = c / b;
- return math;
- }else{
- if (delta == 0){
- math.x_1 = (-b) / (2 * a);
- return math;
- }else{
- math.x_1 = ((-b) + square_root(delta)) / (2 * a);
- math.x_2 = ((-b) - square_root(delta)) / (2 * a);
- return math;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement