Hi @ll,

both the IEEE 754 and the ISO C standards define the (constant) expressions
1.0/0.0 and 0.0/0.0 to yield the floating-point constants INFINITY and NAN
alias INDEFINITE, to be provided as macros (ISO/IEC 9899:1999 7.12/4 INFINITY
and 7.12/5 NAN) in math.h.

JFTR: the statement "NAN is a GNU extension" given in
      <https://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html>
      is WRONG since the last millenium!

The ISO C standard also defines the behaviour of standard functions like
log() and their (constant and well-known) result for (constant) arguments
like -0.0, +0.0, -INFINITY, +INFINITY and NAN

GCC defines most of these standard functions as builtins, for example
log(), and evaluates them at compile time for constant arguments.

GCC but refuses the definition of constants using for example log(-0.0),
although the resulting function value is a well-defined constant!

--- bug.c ---
#include <math.h>

const double euler = log(1.0);
const double infinity = log(-0.0);
const double indefinite = log(-1.0);
const double log_euler = log(euler);
const double log_e = log(log(1.0));
const double log_phi = log(sqrt(5.0) * 0.5 + 0.5);
const double log_minusinf = log(-1.0 / 0.0);
const double log_plusinf = log(1.0 / 0.0);
const double log_nan = log(0.0 / 0.0);
--- EOF ---

$ gcc bug.c
bug.c:4:25: error: initializer element is not constant
    4 | const double infinity = log(-0.0);
      |                         ^~~
bug.c:5:27: error: initializer element is not constant
    5 | const double indefinite = log(-1.0);
      |                           ^~~
bug.c:7:26: error: initializer element is not constant
    6 | const double log_euler = log(euler);
      |                          ^~~
bug.c:9:22: error: initializer element is not constant
    7 | const double log_e = log(log(1.0));
      |                      ^~~
bug.c:11:29: error: initializer element is not constant
    9 | const double log_minusinf = log(-1.0 / 0.0);
      |                             ^~~
bug.c:12:28: error: initializer element is not constant
   10 | const double log_plusinf = log(1.0 / 0.0);
      |                            ^~~
bug.c:13:24: error: initializer element is not constant
   11 | const double log_nan = log(0.0 / 0.0);
      |                        ^~~

Especially compare the lines 7 and 8: while GCC fails on
log(log(1.0)) it but succeeds on log(sqrt(5.0) * 0.5 + 0.5)!

NOT amused
Stefan Kanthak

Reply via email to