summaryrefslogtreecommitdiff
path: root/lib/modf.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2012-02-27 01:44:57 +0100
committerBruno Haible <bruno@clisp.org>2012-02-27 01:44:57 +0100
commitb5cc9885b4b5a247870adf0ed9b9f4a5a3347a26 (patch)
treec9f852a62c0086a9c8f4552f7db42c67781b18f5 /lib/modf.c
parent8f32a5c4f21e3a360b1a756a88880985215d67db (diff)
downloadgnulib-b5cc9885b4b5a247870adf0ed9b9f4a5a3347a26.tar.gz
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.
Diffstat (limited to 'lib/modf.c')
-rw-r--r--lib/modf.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/modf.c b/lib/modf.c
new file mode 100644
index 0000000000..19989e5b8d
--- /dev/null
+++ b/lib/modf.c
@@ -0,0 +1,44 @@
+/* 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;
+ }
+ }
+}