bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

modf math error reporting


From: Bruno Haible
Subject: modf math error reporting
Date: Mon, 27 Feb 2012 01:45:37 +0100
User-agent: KMail/4.7.4 (Linux/3.1.0-1.2-desktop; KDE/4.7.4; x86_64; ; )

After I wrote NaN and Inf tests for the modf() function, the test-modf-ieee
test fails on some platforms:

Cygwin 1.7.9   test-modf-ieee.h:27: assertion failed
IRIX 6.5       test-modf-ieee.h:35: assertion failed
FreeBSD 6.4    test-modf-ieee.h:35: assertion failed
NetBSD 5.1     test-modf-ieee.h:27: assertion failed
OpenBSD 4.9    test-modf-ieee.h:35: assertion failed
OSF/1 5.1      test-modf-ieee.h:43: assertion failed

This patch provides a workaround, triggered by the 'modf-ieee' module.
If you just ask for 'modf' you will get a modf function but without
guaranteed behaviour on NaN, Inf, and -0.0.


2012-02-26  Bruno Haible  <address@hidden>

        modf-ieee: Work around test failures on *BSD, IRIX, OSF/1, Cygwin.
        * m4/modf-ieee.m4: New file.
        * m4/modf.m4 (gl_FUNC_MODF): If gl_FUNC_MODF_IEEE is present, test
        whether modf works with NaN and Inf. Replace it if not.
        * lib/math.in.h (modf): New declaration.
        * lib/modf.c: New file.
        * m4/math_h.m4 (gl_MATH_H): Test whether modf is declared.
        (gl_MATH_H_DEFAULTS): Initialize GNULIB_MODF, REPLACE_MODF.
        * modules/math (Makefile.am): Substitute GNULIB_MODF, REPLACE_MODF.
        * modules/modf (Files): Add lib/modf.c.
        (Depends-on): Add math, isfinite, trunc, isinf.
        (configure.ac): Addrange to compile lib/modf.c if needed.
        * modules/modf-ieee (Files): Add m4/modf-ieee.m4, m4/minus-zero.m4,
        m4/signbit.m4.
        (configure.ac): Invoke gl_FUNC_MODF_IEEE.
        * tests/test-math-c++.cc: Check the declaration of modf.
        * doc/posix-functions/modf.texi: Mention the modf-ieee module.

================================= lib/modf.c ==================================
/* Get signed integer and fractional parts of a floating-point number.
   Copyright (C) 2012 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include <math.h>

double
modf (double x, double *iptr)
{
  if (isfinite (x))
    {
      double integer_part = trunc (x);
      *iptr = integer_part;
      return x - integer_part;
    }
  else
    {
      if (isinf (x))
        {
          *iptr = x;
          return 1.0 / x;
        }
      else /* isnand (x) */
        {
          *iptr = x;
          return x;
        }
    }
}
=============================== m4/modf-ieee.m4 ===============================
# modf-ieee.m4 serial 1
dnl Copyright (C) 2012 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

dnl This macro is in a separate file (not in modf.m4 and not inlined in the
dnl module description), so that gl_FUNC_MODF can test whether 'aclocal' has
dnl found uses of this macro.

AC_DEFUN([gl_FUNC_MODF_IEEE],
[
  m4_divert_text([INIT_PREPARE], [gl_modf_required=ieee])
  AC_REQUIRE([gl_FUNC_MODF])
])
===============================================================================
--- doc/posix-functions/modf.texi.orig  Mon Feb 27 01:32:38 2012
+++ doc/posix-functions/modf.texi       Mon Feb 27 00:51:43 2012
@@ -4,12 +4,22 @@
 
 POSIX specification:@* 
@url{http://www.opengroup.org/onlinepubs/9699919799/functions/modf.html}
 
-Gnulib module: modf
+Gnulib module: modf or modf-ieee
 
-Portability problems fixed by Gnulib:
+Portability problems fixed by either Gnulib module @code{modf} or 
@code{modf-ieee}:
 @itemize
 @end itemize
 
+Portability problems fixed by Gnulib module @code{modf-ieee}:
address@hidden
address@hidden
+This function has problems with a NaN argument on some platforms:
+NetBSD 5.1, Cygwin.
address@hidden
+This function has problems with infinite arguments on some platforms:
+FreeBSD 6.4, OpenBSD 4.9, IRIX 6.5, OSF/1 5.1.
address@hidden itemize
+
 Portability problems not fixed by Gnulib:
 @itemize
 @end itemize
--- lib/math.in.h.orig  Mon Feb 27 01:32:39 2012
+++ lib/math.in.h       Mon Feb 27 00:38:36 2012
@@ -880,6 +880,26 @@
 # endif
 #endif
 
+#if @GNULIB_MODF@
+# if @REPLACE_MODF@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef modf
+#   define modf rpl_modf
+#  endif
+_GL_FUNCDECL_RPL (modf, double, (double x, double *iptr) _GL_ARG_NONNULL 
((2)));
+_GL_CXXALIAS_RPL (modf, double, (double x, double *iptr));
+# else
+_GL_CXXALIAS_SYS (modf, double, (double x, double *iptr));
+# endif
+_GL_CXXALIASWARN (modf);
+#elif defined GNULIB_POSIXCHECK
+# undef modf
+# if HAVE_RAW_DECL_MODF
+_GL_WARN_ON_USE (modf, "modf has portability problems - "
+                 "use gnulib module modf for portability");
+# endif
+#endif
+
 #if @GNULIB_MODFL@
 # if address@hidden@
 #  undef modfl
--- m4/math_h.m4.orig   Mon Feb 27 01:32:39 2012
+++ m4/math_h.m4        Mon Feb 27 00:39:44 2012
@@ -1,4 +1,4 @@
-# math_h.m4 serial 63
+# math_h.m4 serial 64
 dnl Copyright (C) 2007-2012 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -42,7 +42,7 @@
     [acosf acosl asinf asinl atanf atanl
      ceilf ceill copysign copysignf copysignl cosf cosl coshf
      expf expl fabsf fabsl floorf floorl fma fmaf fmal fmodf fmodl frexpf 
frexpl
-     ldexpf ldexpl logb logf logl log10f log10l modff modfl powf
+     ldexpf ldexpl logb logf logl log10f log10l modf modff modfl powf
      remainder remainderf remainderl
      rint rintf rintl round roundf roundl sinf sinl sinhf sqrtf sqrtl
      tanf tanl tanhf trunc truncf truncl])
@@ -103,6 +103,7 @@
   GNULIB_LOGL=0;       AC_SUBST([GNULIB_LOGL])
   GNULIB_LOG10F=0;     AC_SUBST([GNULIB_LOG10F])
   GNULIB_LOG10L=0;     AC_SUBST([GNULIB_LOG10L])
+  GNULIB_MODF=0;       AC_SUBST([GNULIB_MODF])
   GNULIB_MODFF=0;      AC_SUBST([GNULIB_MODFF])
   GNULIB_MODFL=0;      AC_SUBST([GNULIB_MODFL])
   GNULIB_POWF=0;       AC_SUBST([GNULIB_POWF])
@@ -219,6 +220,7 @@
   REPLACE_ISINF=0;             AC_SUBST([REPLACE_ISINF])
   REPLACE_ISNAN=0;             AC_SUBST([REPLACE_ISNAN])
   REPLACE_LDEXPL=0;            AC_SUBST([REPLACE_LDEXPL])
+  REPLACE_MODF=0;              AC_SUBST([REPLACE_MODF])
   REPLACE_NAN=0;               AC_SUBST([REPLACE_NAN])
   REPLACE_ROUND=0;             AC_SUBST([REPLACE_ROUND])
   REPLACE_ROUNDF=0;            AC_SUBST([REPLACE_ROUNDF])
--- m4/modf.m4.orig     Mon Feb 27 01:32:39 2012
+++ m4/modf.m4  Mon Feb 27 01:26:19 2012
@@ -1,4 +1,4 @@
-# modf.m4 serial 1
+# modf.m4 serial 2
 dnl Copyright (C) 2011-2012 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,6 +6,71 @@
 
 AC_DEFUN([gl_FUNC_MODF],
 [
+  m4_divert_text([DEFAULTS], [gl_modf_required=plain])
+  AC_REQUIRE([gl_MATH_H_DEFAULTS])
+
   dnl Determine MODF_LIBM.
   gl_MATHFUNC([modf], [double], [(double, double *)])
+
+  m4_ifdef([gl_FUNC_MODF_IEEE], [
+    if test $gl_modf_required = ieee && test $REPLACE_MODF = 0; then
+      AC_CACHE_CHECK([whether modf works according to ISO C 99 with IEC 60559],
+        [gl_cv_func_modf_ieee],
+        [
+          save_LIBS="$LIBS"
+          LIBS="$LIBS $MODF_LIBM"
+          AC_RUN_IFELSE(
+            [AC_LANG_SOURCE([[
+#ifndef __NO_MATH_INLINES
+# define __NO_MATH_INLINES 1 /* for glibc */
+#endif
+#include <math.h>
+]gl_DOUBLE_MINUS_ZERO_CODE[
+]gl_DOUBLE_SIGNBIT_CODE[
+/* Compare two numbers with ==.
+   This is a separate function because IRIX 6.5 "cc -O" miscompiles an
+   'x == x' test.  */
+static int
+numeric_equal (double x, double y)
+{
+  return x == y;
+}
+static double dummy (double x, double *iptr) { return 0; }
+double zero;
+double minus_one = - 1.0;
+int main (int argc, char *argv[])
+{
+  double (*my_modf) (double, double *) = argc ? modf : dummy;
+  int result = 0;
+  double i;
+  double f;
+  /* Test modf(NaN,...).
+     This test fails on NetBSD 5.1, Cygwin.  */
+  f = my_modf (zero / zero, &i);
+  if (numeric_equal (f, f))
+    result |= 1;
+  /* Test modf(-Inf,...).
+     This test fails on FreeBSD 6.4, OpenBSD 4.9, IRIX 6.5, OSF/1 5.1.  */
+  f = my_modf (minus_one / zero, &i);
+  if (!(f == 0.0) || (signbitd (minus_zerod) && !signbitd (f)))
+    result |= 2;
+  return result;
+}
+            ]])],
+            [gl_cv_func_modf_ieee=yes],
+            [gl_cv_func_modf_ieee=no],
+            [gl_cv_func_modf_ieee="guessing no"])
+          LIBS="$save_LIBS"
+        ])
+      case "$gl_cv_func_modf_ieee" in
+        *yes) ;;
+        *) REPLACE_MODF=1 ;;
+      esac
+    fi
+  ])
+  if test $REPLACE_MODF = 1; then
+    dnl Find libraries needed to link lib/modf.c.
+    AC_REQUIRE([gl_FUNC_TRUNC])
+    MODF_LIBM="$TRUNC_LIBM"
+  fi
 ])
--- modules/math.orig   Mon Feb 27 01:32:39 2012
+++ modules/math        Mon Feb 27 00:40:31 2012
@@ -72,6 +72,7 @@
              -e 's/@''GNULIB_LOGL''@/$(GNULIB_LOGL)/g' \
              -e 's/@''GNULIB_LOG10F''@/$(GNULIB_LOG10F)/g' \
              -e 's/@''GNULIB_LOG10L''@/$(GNULIB_LOG10L)/g' \
+             -e 's/@''GNULIB_MODF''@/$(GNULIB_MODF)/g' \
              -e 's/@''GNULIB_MODFF''@/$(GNULIB_MODFF)/g' \
              -e 's/@''GNULIB_MODFL''@/$(GNULIB_MODFL)/g' \
              -e 's/@''GNULIB_POWF''@/$(GNULIB_POWF)/g' \
@@ -190,6 +191,7 @@
              -e 's|@''REPLACE_ISNAN''@|$(REPLACE_ISNAN)|g' \
              -e 's|@''REPLACE_ITOLD''@|$(REPLACE_ITOLD)|g' \
              -e 's|@''REPLACE_LDEXPL''@|$(REPLACE_LDEXPL)|g' \
+             -e 's|@''REPLACE_MODF''@|$(REPLACE_MODF)|g' \
              -e 's|@''REPLACE_NAN''@|$(REPLACE_NAN)|g' \
              -e 's|@''REPLACE_ROUND''@|$(REPLACE_ROUND)|g' \
              -e 's|@''REPLACE_ROUNDF''@|$(REPLACE_ROUNDF)|g' \
--- modules/modf.orig   Mon Feb 27 01:32:39 2012
+++ modules/modf        Mon Feb 27 00:44:30 2012
@@ -2,13 +2,22 @@
 modf() function: get signed integer and fractional parts.
 
 Files:
+lib/modf.c
 m4/modf.m4
 m4/mathfunc.m4
 
 Depends-on:
+math
+isfinite        [test $REPLACE_MODF = 1]
+trunc           [test $REPLACE_MODF = 1]
+isinf           [test $REPLACE_MODF = 1]
 
 configure.ac:
 gl_FUNC_MODF
+if test $REPLACE_MODF = 1; then
+  AC_LIBOBJ([modf])
+fi
+gl_MATH_MODULE_INDICATOR([modf])
 
 Makefile.am:
 
--- modules/modf-ieee.orig      Mon Feb 27 01:32:39 2012
+++ modules/modf-ieee   Mon Feb 27 01:11:56 2012
@@ -2,12 +2,16 @@
 modf() function according to ISO C 99 with IEC 60559.
 
 Files:
+m4/modf-ieee.m4
+m4/minus-zero.m4
+m4/signbit.m4
 
 Depends-on:
 modf
 fpieee
 
 configure.ac:
+gl_FUNC_MODF_IEEE
 
 Makefile.am:
 
--- tests/test-math-c++.cc.orig Mon Feb 27 01:32:39 2012
+++ tests/test-math-c++.cc      Mon Feb 27 01:32:35 2012
@@ -195,7 +195,9 @@
 #if GNULIB_TEST_MODFF
 SIGNATURE_CHECK (GNULIB_NAMESPACE::modff, float, (float, float *));
 #endif
-//SIGNATURE_CHECK (GNULIB_NAMESPACE::modf, double, (double, double *));
+#if GNULIB_TEST_MODF
+SIGNATURE_CHECK (GNULIB_NAMESPACE::modf, double, (double, double *));
+#endif
 #if GNULIB_TEST_MODFL
 SIGNATURE_CHECK (GNULIB_NAMESPACE::modfl, long double,
                  (long double, long double *));




reply via email to

[Prev in Thread] Current Thread [Next in Thread]