mirror of
https://gitlab.gwdg.de/j.hahn02/university.git
synced 2026-01-01 06:44:25 -05:00
cleanup
This commit is contained in:
31
S1/GdCP/klausur_c_1/sqrtrec.c
Normal file
31
S1/GdCP/klausur_c_1/sqrtrec.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include<float.h>
|
||||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
|
||||
//#include "testing-0xx.h"
|
||||
|
||||
double sqrt_rec(double x, double p) {
|
||||
// Check for edge cases
|
||||
if (x == 0 || x < 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get the next element like the formula provided
|
||||
double next = 0.5 * (p + x/p);
|
||||
|
||||
// Decide whether to go further
|
||||
if (fabs(next - p) < DBL_EPSILON) {
|
||||
return next;
|
||||
}
|
||||
|
||||
// Recusrevely try the next element in the series
|
||||
return sqrt_rec(x, next);
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%f\n", sqrt_rec(2, 1));
|
||||
printf("%f\n", sqrt_rec(4, 1));
|
||||
printf("%f\n", sqrt_rec(8, 1));
|
||||
printf("%f\n", sqrt_rec(9, 1));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user