From 4aa40ed36fcdb13f73520945d804e6d3d8d52738 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@gnu.org>
Date: Sun, 27 Jul 2008 11:12:45 -0700
Subject: [PATCH] Use standard C99 isfinite, isnan, isinf in place of GSL
 substitutes.

In change a9afcdd22, "Use gsl_isnan instead of isnan, ...," isfinite,
isnan, and isinf were changed to use the GSL substitutes because of
lack of portability of the C99 versions.  Now, gnulib has portable
versions of all of these, so change them back.

This commit changes calls to gsl_finite() to call isfinite() instead
of the equivalent finite().  isfinite() is correct here: both gsl_finite()
and finite() return true for NaNs, which is a surprising result given
that a NaN is definitely not finite, but isfinite() returns false and,
more importantly, behaves as actually wanted in each place it is used
in PSPP code.

This commit requires upgrading gnulib to at least change 5183a0e3c,
"Add missing dependencies on new m4/exponent[fdl].m4 files," or later,
and re-running "make -f Smake".
---
 Smake                               | 2 ++
 src/data/data-out.c                 | 5 ++---
 src/data/por-file-writer.c          | 3 +--
 src/language/control/loop.c         | 6 ++----
 src/language/data-io/inpt-pgm.c     | 3 +--
 src/language/expressions/evaluate.c | 3 +--
 src/language/expressions/helpers.h  | 3 +--
 src/libpspp/hash.c                  | 3 +--
 src/math/linreg.c                   | 2 +-
 src/math/moments.c                  | 5 ++---
 src/math/ts/innovations.c           | 7 +++----
 11 files changed, 17 insertions(+), 25 deletions(-)

diff --git a/Smake b/Smake
index aa05962f..61c53b88 100644
--- a/Smake
+++ b/Smake
@@ -28,6 +28,8 @@ GNULIB_MODULES = \
 	gettext-h \
 	gettimeofday \
 	isfinite \
+	isinf \
+	isnan \
 	intprops \
 	inttostr \
 	localcharset \
diff --git a/src/data/data-out.c b/src/data/data-out.c
index cef44520..86688a67 100644
--- a/src/data/data-out.c
+++ b/src/data/data-out.c
@@ -20,7 +20,6 @@
 
 #include <ctype.h>
 #include <float.h>
-#include <gsl/gsl_math.h>
 #include <math.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -928,9 +927,9 @@ output_infinite (double number, const struct fmt_spec *format, char *output)
     {
       const char *s;
 
-      if (gsl_isnan (number))
+      if (isnan (number))
         s = "NaN";
-      else if (gsl_isinf (number))
+      else if (isinf (number))
         s = number > 0 ? "+Infinity" : "-Infinity";
       else
         s = "Unknown";
diff --git a/src/data/por-file-writer.c b/src/data/por-file-writer.c
index 75022ea7..8a27f6b7 100644
--- a/src/data/por-file-writer.c
+++ b/src/data/por-file-writer.c
@@ -20,7 +20,6 @@
 #include <ctype.h>
 #include <errno.h>
 #include <float.h>
-#include <gsl/gsl_math.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -778,7 +777,7 @@ format_trig_double (long double value, int base_10_precision, char output[])
      0...30**6, an invariant of the loop below. */
   errno = 0;
   base_2_sig = frexp (value, &base_2_exp);
-  if (errno != 0 || !gsl_finite (base_2_sig))
+  if (errno != 0 || !isfinite (base_2_sig))
     goto missing_value;
   if (base_2_exp == 0 && base_2_sig == 0.)
     goto zero;
diff --git a/src/language/control/loop.c b/src/language/control/loop.c
index c6309d42..40a33c11 100644
--- a/src/language/control/loop.c
+++ b/src/language/control/loop.c
@@ -18,8 +18,6 @@
 
 #include "control-stack.h"
 
-#include <gsl/gsl_math.h>
-
 #include <data/case.h>
 #include <data/dictionary.h>
 #include <data/procedure.h>
@@ -323,8 +321,8 @@ loop_trns_proc (void *loop_, struct ccase *c, casenumber case_num)
       case_data_rw (c, loop->index_var)->f = loop->cur;
 
       /* Throw out pathological cases. */
-      if (!gsl_finite (loop->cur) || !gsl_finite (loop->by)
-          || !gsl_finite (loop->last)
+      if (!isfinite (loop->cur) || !isfinite (loop->by)
+          || !isfinite (loop->last)
           || loop->by == 0.0
           || (loop->by > 0.0 && loop->cur > loop->last)
           || (loop->by < 0.0 && loop->cur < loop->last))
diff --git a/src/language/data-io/inpt-pgm.c b/src/language/data-io/inpt-pgm.c
index e000a0fd..609c9b2c 100644
--- a/src/language/data-io/inpt-pgm.c
+++ b/src/language/data-io/inpt-pgm.c
@@ -19,7 +19,6 @@
 #include <language/data-io/inpt-pgm.h>
 
 #include <float.h>
-#include <gsl/gsl_math.h>
 #include <stdlib.h>
 
 #include <data/case.h>
@@ -333,7 +332,7 @@ reread_trns_proc (void *t_, struct ccase *c, casenumber case_num)
   else
     {
       double column = expr_evaluate_num (t->column, c, case_num);
-      if (!gsl_finite (column) || column < 1)
+      if (!isfinite (column) || column < 1)
 	{
 	  msg (SE, _("REREAD: Column numbers must be positive finite "
 	       "numbers.  Column set to 1."));
diff --git a/src/language/expressions/evaluate.c b/src/language/expressions/evaluate.c
index 6402cc0a..fc5b74a3 100644
--- a/src/language/expressions/evaluate.c
+++ b/src/language/expressions/evaluate.c
@@ -18,7 +18,6 @@
 #include "private.h"
 
 #include <ctype.h>
-#include <gsl/gsl_math.h>
 #include <libpspp/assertion.h>
 #include <libpspp/message.h>
 #include "helpers.h"
@@ -64,7 +63,7 @@ expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
           break;
 
         case OP_return_number:
-          *(double *) result = gsl_finite (ns[-1]) ? ns[-1] : SYSMIS;
+          *(double *) result = isfinite (ns[-1]) ? ns[-1] : SYSMIS;
           return;
 
         case OP_return_string:
diff --git a/src/language/expressions/helpers.h b/src/language/expressions/helpers.h
index 447a6b82..895ec49b 100644
--- a/src/language/expressions/helpers.h
+++ b/src/language/expressions/helpers.h
@@ -4,7 +4,6 @@
 #include <ctype.h>
 #include <float.h>
 #include <gsl/gsl_cdf.h>
-#include <gsl/gsl_math.h>
 #include <gsl/gsl_randist.h>
 #include <gsl/gsl_sf.h>
 #include <limits.h>
@@ -68,7 +67,7 @@ struct substring copy_string (struct expression *,
 static inline bool
 is_valid (double d)
 {
-  return gsl_finite (d) && d != SYSMIS;
+  return isfinite (d) && d != SYSMIS;
 }
 
 size_t count_valid (double *, size_t);
diff --git a/src/libpspp/hash.c b/src/libpspp/hash.c
index 627751e1..9da3deb1 100644
--- a/src/libpspp/hash.c
+++ b/src/libpspp/hash.c
@@ -20,7 +20,6 @@
 #include "message.h"
 #include <assert.h>
 #include <ctype.h>
-#include <gsl/gsl_math.h>
 #include <limits.h>
 #include <stdbool.h>
 #include <stdlib.h>
@@ -134,7 +133,7 @@ hsh_hash_int (int i)
 unsigned
 hsh_hash_double (double d)
 {
-  if (!gsl_isnan (d))
+  if (!isnan (d))
     return hsh_hash_bytes (&d, sizeof d);
   else
     return 0;
diff --git a/src/math/linreg.c b/src/math/linreg.c
index b3ab5875..7e7d4a55 100644
--- a/src/math/linreg.c
+++ b/src/math/linreg.c
@@ -546,7 +546,7 @@ pspp_linreg_residual (const struct variable **predictors,
     }
   pred = pspp_linreg_predict (predictors, vals, c, n_vals);
 
-  result = gsl_isnan (pred) ? GSL_NAN : (obs->f - pred);
+  result = isnan (pred) ? GSL_NAN : (obs->f - pred);
   return result;
 }
 
diff --git a/src/math/moments.c b/src/math/moments.c
index 02208bca..d129f6ab 100644
--- a/src/math/moments.c
+++ b/src/math/moments.c
@@ -17,7 +17,6 @@
 #include <config.h>
 #include "moments.h"
 #include <assert.h>
-#include <gsl/gsl_math.h>
 #include <math.h>
 #include <stdlib.h>
 #include <libpspp/misc.h>
@@ -56,7 +55,7 @@ calc_moments (enum moment max_moment,
             {
               double s3 = s2 * sqrt (s2);
               double g1 = (w * d3) / ((w - 1.0) * (w - 2.0) * s3);
-              if (gsl_finite (g1))
+              if (isfinite (g1))
                 *skewness = g1;
             }
           if (max_moment >= MOMENT_KURTOSIS && kurtosis != NULL && w > 3.)
@@ -64,7 +63,7 @@ calc_moments (enum moment max_moment,
               double den = (w - 2.) * (w - 3.) * pow2 (s2);
               double g2 = (w * (w + 1) * d4 / (w - 1.) / den
                            - 3. * pow2 (d2) / den);
-              if (gsl_finite (g2))
+              if (isfinite (g2))
                 *kurtosis = g2;
             }
         }
diff --git a/src/math/ts/innovations.c b/src/math/ts/innovations.c
index 3e3be202..553e20e8 100644
--- a/src/math/ts/innovations.c
+++ b/src/math/ts/innovations.c
@@ -28,7 +28,6 @@
 #include <config.h>
 #include <gsl/gsl_matrix.h>
 #include <gsl/gsl_vector.h>
-#include <gsl/gsl_math.h>
 #include <stdlib.h>
 #include <libpspp/compiler.h>
 #include <math/coefficient.h>
@@ -56,7 +55,7 @@ get_mean (const gsl_matrix *data,
       for (n = 0; n < data->size2; n++)
 	{
 	  tmp = gsl_matrix_get (data, i, n);
-	  if (!gsl_isnan (tmp))
+	  if (!isnan (tmp))
 	    {
 	      est[n]->n_obs += 1.0;
 	      d = (tmp - est[n]->mean) / est[n]->n_obs;
@@ -77,9 +76,9 @@ update_cov (struct innovations_estimate **est, gsl_vector_const_view x,
     {
       xj = gsl_vector_get (&x.vector, j);
       yj = gsl_vector_get (&y.vector, j);
-      if (!gsl_isnan (xj))
+      if (!isnan (xj))
 	{
-	  if (!gsl_isnan (yj))
+	  if (!isnan (yj))
 	    {
 	      xj -= est[j]->mean;
 	      yj -= est[j]->mean;
-- 
2.30.2