Last active
September 30, 2017 17:19
-
-
Save Dimanaux/7db9d3cc4d21e95be878a3e697523a4c to your computer and use it in GitHub Desktop.
math analisys homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
#define true 1 | |
typedef double (*mathFn)(double); | |
const double EPS = 1e-9; | |
double f(double); | |
double binarySearch(double, double, double, mathFn); | |
int main(int argc, char const *argv[]) | |
{ | |
mathFn homeTask = &f; | |
double result = binarySearch(-3.0, -1.0, 0.0, homeTask); | |
printf("%.15f\n", result); | |
return 0; | |
} | |
double f(double x) | |
{ | |
return sin(x) - x - 1; | |
} | |
double binarySearch(double leftThreshold, | |
double rightThreshold, | |
double requiredValue, | |
mathFn func) | |
{ | |
double root; | |
double val; | |
while (true) | |
{ | |
root = (leftThreshold + rightThreshold) / 2.0; | |
val = func(root); | |
if (fabs(val) < EPS) | |
{ | |
return root; | |
} | |
else if (val < 0) | |
{ | |
rightThreshold = root; | |
} | |
else | |
{ | |
leftThreshold = root; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment