Heron something what the heck
This commit is contained in:
parent
f6ac67a6a6
commit
8532dd4657
1
modernc/03/.gitignore
vendored
1
modernc/03/.gitignore
vendored
@ -1 +1,2 @@
|
||||
challenge-1
|
||||
heron
|
||||
|
40
modernc/03/heron.c
Normal file
40
modernc/03/heron.c
Normal file
@ -0,0 +1,40 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* lower and upper iteration limits centered around 1.0 */
|
||||
static double const eps1m01 = 1.0 - 0x1P-01;
|
||||
static double const eps1p01 = 1.0 + 0x1P-01;
|
||||
static double const eps1m24 = 1.0 - 0x1P-24;
|
||||
static double const eps1p24 = 1.0 + 0x1P-24;
|
||||
|
||||
int main(int argc, char* argv[argc+1]) {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
double const a = strtod(argv[i], 0);
|
||||
double x = 1.0;
|
||||
|
||||
for (;;) {
|
||||
double prod = a*x;
|
||||
if (prod < eps1m01) {
|
||||
x *= 2.0;
|
||||
} else if (eps1p01 < prod) {
|
||||
x *= 0.5;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
double prod = a*x;
|
||||
if ((prod < eps1m24) || (eps1p24 < prod)) {
|
||||
x *= (2.0 - prod);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("heron: a=%.5e,\tx=%.5e,\ta*x=%.12f\n",
|
||||
a, x, a*x);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user