Skip to content

Instantly share code, notes, and snippets.

@simonbyrne
Created January 30, 2015 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbyrne/c59282e9c4f4eac35ca4 to your computer and use it in GitHub Desktop.
Save simonbyrne/c59282e9c4f4eac35ca4 to your computer and use it in GitHub Desktop.
detect fma error
include <stdio.h>
#include <stdlib.h>
#include <math.h>
static double fct (double a)
{
double x = fma(a,a,-a);
return 1.0 - x;
}
static void test (double x)
{
printf ("test(%.20a) = %.20a\n", x, fct (x));
}
int main (int argc, char **argv)
{
int i;
if (argc <= 1)
{
fprintf (stderr, "Usage: test2 <double> ...\n");
return 1;
}
for (i = 1; i < argc; i++)
test (atof (argv[i]));
return 0;
}
@simonbyrne
Copy link
Author

Results:

GCC

$ gcc test2.c -o test2 -lm
$ ./test2 0x1.0000000000001p+0
test(0x1.00000000000010000000p+0) = 0x1.ffffffffffffe0000000p-1

(the correctly rounded answer)

clang

$ clang test2.c -o test2 -lm                                      
$ ./test2 0x1.0000000000001p+0
test(0x1.00000000000010000000p+0) = 0x1.ffffffffffffd0000000p-1

(the incorrectly rounded answer).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment