https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102498

            Bug ID: 102498
           Summary: wrong output of printf with long double constant and
                    non-default rounding mode
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zimmerma+gcc at loria dot fr
  Target Milestone: ---

with the following program:

#include <stdio.h>
#include <math.h>
#include <fenv.h>
const long double a = 0xc.90fdaa22168c235p-1l;
const long double b = 0xc.90fdaa22168c235p-2l;
int main()
{
  fesetround (FE_TONEAREST);
  printf ("FE_TONEAREST:  a=%La b=%La\n", a, b);
  fesetround (FE_TOWARDZERO);
  printf ("FE_TOWARDZERO: a=%La b=%La\n", a, b);
  fesetround (FE_UPWARD);
  printf ("FE_UPWARD:     a=%La b=%La\n", a, b);
  fesetround (FE_DOWNWARD);
  printf ("FE_DOWNWARD:   a=%La b=%La\n", a, b);
}

I get:

$ gcc -frounding-math -O0 -fno-builtin e.c -lm
$ ./a.out 
FE_TONEAREST:  a=0xc.90fdaa22168c235p-1 b=0xc.90fdaa22168c235p-2
FE_TOWARDZERO: a=0xc.90fdaa22168c235p-1 b=0xc.90fdaa22168c234p-2
FE_UPWARD:     a=0xc.90fdaa22168c235p-1 b=0xc.90fdaa22168c235p-2
FE_DOWNWARD:   a=0xc.90fdaa22168c235p-1 b=0xc.90fdaa22168c234p-2

The value 'a' is printed the same whatever the rounding mode, but 'b' (which
approximates pi to nearest) is wrongly printed for FE_TOWARDZERO and
FE_DOWNWARD.

According to Alexander Monakov, the same problem should happen with og_2 10,
log_2 e, log_10 2, and log_e 2, see
https://sourceware.org/pipermail/libc-alpha/2021-September/131411.html

Reply via email to