#include #include #include #include #include #include #include volatile uint32_t counter; // convert to non-nul-terminated bytes in a signal-handler-safe way static char * u32toa(uint32_t n, char *s) { uint32_t m = n/10; if (m) s = u32toa(m, s); *s = '0' + n % 10; return s + 1; } // signal handler to report status static void handler(int sig) { static char buf[16]; char *end = u32toa(counter, buf); *end++ = '\n'; write(1, buf, end - buf); } float cuberoot(float y); // from floatcbrt.c int main(int argc, char **argv) { double worst_err = 0; uint32_t worst = 0xdeadbeef; assert(sizeof(float) == sizeof counter); signal(SIGQUIT, handler); do { float y; uint32_t n = counter; // https://stackoverflow.com/questions/4328342/float-bits-and-strict-aliasing memcpy(&y, &n, sizeof y); float x = cuberoot(y); if (!isnan(x) && !isnan(y)) { double err = fabs(y - x*x*x); if (err > worst_err) { worst_err = err; worst = counter; } } counter++; } while (counter); float worst_y; memcpy(&worst_y, &worst, sizeof worst); printf("Worst error, at %g (0x%08x): %g\n", worst_y, worst, worst_err); return 0; }