Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Nth-root/C/nth-root.c
Normal file
34
Task/Nth-root/C/nth-root.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <float.h>
|
||||
|
||||
double pow_ (double x, int e) {
|
||||
int i;
|
||||
double r = 1;
|
||||
for (i = 0; i < e; i++) {
|
||||
r *= x;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
double root (int n, double x) {
|
||||
double d, r = 1;
|
||||
if (!x) {
|
||||
return 0;
|
||||
}
|
||||
if (n < 1 || (x < 0 && !(n&1))) {
|
||||
return 0.0 / 0.0; /* NaN */
|
||||
}
|
||||
do {
|
||||
d = (x / pow_(r, n - 1) - r) / n;
|
||||
r += d;
|
||||
}
|
||||
while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
|
||||
return r;
|
||||
}
|
||||
|
||||
int main () {
|
||||
int n = 15;
|
||||
double x = pow_(-3.14159, 15);
|
||||
printf("root(%d, %g) = %g\n", n, x, root(n, x));
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue