From b3e9e62599532050fc776c5e8f076915b56c2235 Mon Sep 17 00:00:00 2001 From: David Faulkner Date: Fri, 7 Aug 2026 23:40:47 -0500 Subject: Import official C23 code examples for Modern C (Jens Gustedt, 2024) - Add official C source files, Makefile, c23-fallback.h, and LICENSE - Update README.md with study mirror notice --- heron.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 heron.c (limited to 'heron.c') diff --git a/heron.c b/heron.c new file mode 100644 index 0000000..4dae846 --- /dev/null +++ b/heron.c @@ -0,0 +1,36 @@ +#include "c23-fallback.h" +#include + +/* lower and upper iteration limits centered around 1.0 */ +constexpr double eps1m01 = 1.0 - 0x1P-01; +constexpr double eps1p01 = 1.0 + 0x1P-01; +constexpr double eps1m24 = 1.0 - 0x1P-24; +constexpr double eps1p24 = 1.0 + 0x1P-24; + +int main(int argc, char* argv[argc+1]) { + for (int i = 1; i < argc; ++i) { // process args + double const a = strtod(argv[i], nullptr); // arg -> double + double x = 1.0; + for (;;) { // by powers of 2 + double prod = a*x; + if (prod < eps1m01) { + x *= 2.0; + } else if (eps1p01 < prod) { + x *= 0.5; + } else { + break; + } + } + for (;;) { // Heron approximation + 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; +} -- cgit v1.2.3