Bug 14686 - remainder sometimes fails to raise INVALID exception with -lieee
Summary: remainder sometimes fails to raise INVALID exception with -lieee
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: math (show other bugs)
Version: 2.16
: P2 normal
Target Milestone: 2.18
Assignee: Thomas Schwinge
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-10-08 22:48 UTC by Joseph Myers
Modified: 2014-06-17 04:06 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph Myers 2012-10-08 22:48:43 UTC
The following test, on x86_64, with -fno-builtin -lieee, shows the remainder function failing to raise the INVALID exception.

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#include <float.h>
#include <complex.h>

volatile double a = INFINITY, b = 2.0;

int
main (void)
{
  feclearexcept (FE_ALL_EXCEPT);
  errno = 0;
  volatile double r = remainder (a, b);
  if (fetestexcept (FE_DIVBYZERO))
    printf ("DIVBYZERO ");
  if (fetestexcept (FE_INEXACT))
    printf ("INEXACT ");
  if (fetestexcept (FE_INVALID))
    printf ("INVALID ");
  if (fetestexcept (FE_OVERFLOW))
    printf ("OVERFLOW ");
  if (fetestexcept (FE_UNDERFLOW))
    printf ("UNDERFLOW ");
  printf ("%.18g %m\n", r);
  return 0;
}

The problem is the code in dbl-64/e_remainder.c

      if (kx == 0x7ff00000 && u.i[LOW_HALF] == 0 && y == 1.0)
        return x / x;

which acts only when y == 1.0, but should act for any non-NaN value of y (finite or infinite).  Any remainder (non-NaN, 0) or remainder (Inf, non-NaN) should return NaN and raise the INVALID exception.  In the absence of -lieee, this issue is masked by the w_remainder.c wrapper.
Comment 1 Joseph Myers 2012-10-08 22:49:59 UTC
The same issue applies for remainder (1.0, 0.0), which likewise needs logic to raise the INVALID exception rather than just quietly returning a NaN.
Comment 2 Thomas Schwinge 2013-04-05 21:02:45 UTC
commit a1cbf437a53b24f2ff1f6af42028b607f6aa279d
Author: Thomas Schwinge <thomas@codesourcery.com>
Date:   Thu Apr 4 17:35:12 2013 +0200

    [BZ #14686, #15336] Fix standard compliance.  Don't use hard-coded qNaN values.

Additional tests in
<http://news.gmane.org/find-root.php?message_id=%3C87r4iqxq1n.fsf%40schwinge.name%3E>
not yet committed.