Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Last active September 30, 2017 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimanaux/7db9d3cc4d21e95be878a3e697523a4c to your computer and use it in GitHub Desktop.
Save Dimanaux/7db9d3cc4d21e95be878a3e697523a4c to your computer and use it in GitHub Desktop.
math analisys homework
#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