public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Are some builtin functions (for example log() vs. sqrt()) more equal than others?
@ 2021-07-30 15:30 Stefan Kanthak
  2021-07-30 16:33 ` Joseph Myers
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Kanthak @ 2021-07-30 15:30 UTC (permalink / raw)
  To: gcc

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-07-30 21:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-30 15:30 Are some builtin functions (for example log() vs. sqrt()) more equal than others? Stefan Kanthak
2021-07-30 16:33 ` Joseph Myers
2021-07-30 19:53   ` Stefan Kanthak
2021-07-30 20:51     ` Joseph Myers
2021-07-30 21:54       ` Stefan Kanthak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).