/* * Copyright (C) 2018 Tomasz Kramkowski * SPDX-License-Identifier: MIT */ #include #include #include #include #include "ieee754.h" static unsigned long naiive(float n) { union { float f; uint32_t u; } u = { .f = n }; return u.u; } void test(float a) { float b = ieee754f(naiive(a)); if (!((isnan(a) && isnan(b)) || a == b)) { fprintf(stderr, "%lu: %.40f != %.40f\n", naiive(a), a, b); exit(EXIT_FAILURE); } } int main(void) { test(NAN); test(+INFINITY); test(-INFINITY); test(0x1p127f); test(0x7fffffp-149f); for (float n = -0.000000001; n <= 1; n = nextafterf(n, 2)) test(n); }