C :: Aufgabe #166
1 Lösung
Wurzel ziehen mit Intervallschachtelung
Anfänger - C
von Felix
- 11.07.2017 um 21:30 Uhr
Schreibe eine Methode die aus einer Zahl die Wurzel zieht, benutze dafür die Intervallschachtelung.
Lösungen:
/*********************************************
* sqrtni.c Wurzel mit Intervallschachtelung
*********************************************/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv) {
double fa, flower, fupper, step;
uint a, lower, upper;
int n;
if (argc < 2) {
printf("Usage: sqrtni <Zahl>\n");
return 0;
}
a = atoi(argv[1]);
fa = (double)a;
/* Vorkomma */
lower = 0;
upper = 1;
while (upper*upper <= a) {
++lower;
++upper;
}
/* Nachkomma */
flower=(double)lower;
step=0.1;
for (n=0; n<8; n++) {
fupper = flower + step;
while (fupper*fupper <= fa) {
flower += step;
fupper += step;
}
step /= 10.;
}
printf("Wurzel mit Intervallschachtelung : %.8f\n", (flower+fupper)/2.);
printf("Wurzel mit Bibliotheksfunktion : %.8f\n", sqrt(fa));
return 0;
}
