-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathex3.c
44 lines (34 loc) · 967 Bytes
/
ex3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* 3. Modify Program 7.8 so that the value of epsilon is passed as an argument
* to the function. Try experimenting with different values of epsilon to see
* the effect that it has on the value of the square root.
*
* By Faisal Saadatmand
*/
#include <stdio.h>
/* functions */
float absoluteValue(float);
float squareRoot(float, const float);
/* Function to calculate the absolute value of a number */
float absoluteValue(float x)
{
if (x < 0)
x = -x;
return x;
}
/* Function to compute the square root of a number */
float squareRoot(float x, const float epsilon)
{
float guess = 1.0;
while (absoluteValue(guess * guess - x) >= epsilon)
guess = (x / guess + guess) / 2.0;
return guess;
}
int main(void)
{
const float epsilon = .00001;
printf("squareRoot(2.0) = %f\n", squareRoot(2.0, epsilon));
printf("squareRoot(144.0) = %f\n", squareRoot(144.0, epsilon));
printf("squareRoot(17.5) = %f\n", squareRoot(17.5, epsilon));
return 0;
}