Created
December 22, 2011 06:30
-
-
Save manishym/1509239 to your computer and use it in GitHub Desktop.
Square root by newtons method in c
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 <stdlib.h> | |
#include <stdio.h> | |
#include <math.h> | |
#define EPS 0.0001 | |
float average (float x, float y) ; | |
float improve (float guess, float x) ; | |
int good_enough (float guess, float x) ; | |
float my_square_root (float x, float guess) ; | |
float square (float x) ; | |
int main (int argc, char* argv[]) { | |
float x = atof(argv[1]) ; | |
if (!x) | |
return -1 ; | |
printf ("%f\n", my_square_root(x, 1.0)) ; | |
return 0; | |
} | |
float my_square_root (float x, float guess) { | |
if (good_enough(guess, x) ) { | |
return guess ; | |
} | |
else { | |
return my_square_root (x, improve (guess, x)) ; | |
} | |
} | |
int good_enough (float guess, float x) { | |
if (fabsf ( square (guess) - x) < EPS) | |
return 1 ; | |
else | |
return 0 ; | |
} | |
float improve (float guess, float x) { | |
float improved ; | |
return improved = average (guess, x/guess) ; | |
} | |
float average (float x, float y) { | |
return ( x + y ) / 2 ; | |
} | |
float square (float x) | |
{ | |
return (x * x) ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment