| functions/my-sqrt-incorrect.c - En forkert implementation af my_sqrt. | Lektion 7 - slide 7 : 25 Program 1 |
#include <stdio.h>
#include <math.h>
/* #define NDEBUG 1 */ /* if NDBUG is defined, the assert facility is disabled */
#include <assert.h>
int isSmallNumber(double x){
return (fabs(x) < 0.0000001);
}
double my_sqrt(double x){
double res;
assert(x >= 0); /* PRECONDITION */
res = x / 2;
assert(isSmallNumber(res*res - x)); /* POSTCONDITION */
return res;
}
int main(void) {
printf("my_sqrt(15.0): %lf\n", my_sqrt(15.0));
printf("my_sqrt(20.0): %lf\n", my_sqrt(20.0));
printf("my_sqrt(2.0): %lf\n", my_sqrt(2.0));
printf("my_sqrt(16.0): %lf\n", my_sqrt(16.0));
printf("my_sqrt(-3.0): %lf\n", my_sqrt(-3.0));
return 0;
}