public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103304] New: tgmath returns incorrect results for float fma (fmaf)
@ 2021-11-17 15:37 oscar.smith at juliacomputing dot com
  2021-11-17 16:05 ` [Bug c++/103304] " redi at gcc dot gnu.org
  2021-11-17 16:07 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: oscar.smith at juliacomputing dot com @ 2021-11-17 15:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103304
           Summary: tgmath returns incorrect results for float fma (fmaf)
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: oscar.smith at juliacomputing dot com
  Target Milestone: ---

In the following program, g++ incorrectly believes that the fma call is a
double precision fma rather than a templated single precision fma, leading to
an incorrect answer due to double rounding. This program produces
-4167095.500000
-4167095.750000
instead of the correct result which would be
-4167095.750000
-4167095.750000

#include <ctgmath>
#include <cstdio>
int main() {
    float a = -1.9369631e13f, b = 2.1513551e-7f, c = -1.7354427e-24f;
    float f1 = fma(a, b, c);
    float f2 = fmaf(a, b, c);
    printf("%f\n", f1);
    printf("%f\n", f2);
    return 0;
}

For comparison, the identical C program

#include <tgmath.h>
#include <stdio.h>
int main() {
    float a = -1.9369631e13f, b = 2.1513551e-7f, c = -1.7354427e-24f;
    float f1 = fma(a, b, c);
    float f2 = fmaf(a, b, c);
    printf("%f\n", f1);
    printf("%f\n", f2);
    return 0;
}

produces
-4167095.750000
-4167095.750000

as expected.

These were compiled with g++ -o testoutcpp test.cpp -march=haswell -Wall
-Wextra -std=c++11
and gcc -o testoutc test.c -march=haswell -Wall -Wextra -lm -std=c11
respectively (although I get the same results for all combinations of flags
that I've tried.

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

* [Bug c++/103304] tgmath returns incorrect results for float fma (fmaf)
  2021-11-17 15:37 [Bug c++/103304] New: tgmath returns incorrect results for float fma (fmaf) oscar.smith at juliacomputing dot com
@ 2021-11-17 16:05 ` redi at gcc dot gnu.org
  2021-11-17 16:07 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2021-11-17 16:05 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is a bug in your program, combined with a bug in libstdc++ that allows
your invalid code to compile.

You have included <ctgmath> but then called fma and fmaf unqualified, which is
invalid. If you include <ctgmath> then they are only guaranteed to be defined
in namespace std. What happens is that you call ::fma which is the libc version
that takes double arguments. To get the overload for float arguments you need
to call std::fma (either using a qualified name, or following using std::fma or
using namespace std).

*** This bug has been marked as a duplicate of bug 89855 ***

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

* [Bug c++/103304] tgmath returns incorrect results for float fma (fmaf)
  2021-11-17 15:37 [Bug c++/103304] New: tgmath returns incorrect results for float fma (fmaf) oscar.smith at juliacomputing dot com
  2021-11-17 16:05 ` [Bug c++/103304] " redi at gcc dot gnu.org
@ 2021-11-17 16:07 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2021-11-17 16:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
N.B. the same applies to printf: you included <cstdio> so you should be using
std::printf. Or you should include <stdio.h>

Stop including C++ headers that put names in namespace std and then using them
as though they are not in namespace std. That is not guaranteed to work.

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

end of thread, other threads:[~2021-11-17 16:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-17 15:37 [Bug c++/103304] New: tgmath returns incorrect results for float fma (fmaf) oscar.smith at juliacomputing dot com
2021-11-17 16:05 ` [Bug c++/103304] " redi at gcc dot gnu.org
2021-11-17 16:07 ` redi at gcc dot gnu.org

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).