lo-cieee.c

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2002-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <float.h>
00028 
00029 #if defined (HAVE_FLOATINGPOINT_H)
00030 #include <floatingpoint.h>
00031 #endif
00032 
00033 #if defined (HAVE_IEEEFP_H)
00034 #include <ieeefp.h>
00035 #endif
00036 
00037 #include "lo-ieee.h"
00038 #include "lo-math.h"
00039 
00040 #if ! defined (HAVE_ISNAN) && defined (HAVE__ISNAN)
00041 #define isnan _isnan
00042 #define HAVE_ISNAN 1
00043 #endif
00044 
00045 #if ! defined (HAVE_FINITE) && defined (HAVE__FINITE)
00046 #define finite _finite
00047 #define HAVE_FINITE 1
00048 #endif
00049 
00050 #if defined (_AIX) && defined (__GNUG__)
00051 #undef finite
00052 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX)
00053 #endif
00054 
00055 /* Octave's idea of infinity.  */
00056 double octave_Inf;
00057 float octave_Float_Inf;
00058 
00059 /* Octave's idea of a missing value.  */
00060 double octave_NA;
00061 float octave_Float_NA;
00062 
00063 /* Octave's idea of not a number.  */
00064 double octave_NaN;
00065 float octave_Float_NaN;
00066 
00067 int lo_ieee_hw;
00068 int lo_ieee_lw;
00069 
00070 int
00071 __lo_ieee_isnan (double x)
00072 {
00073 #if defined (HAVE_ISNAN)
00074   return isnan (x);
00075 #else
00076   return 0;
00077 #endif
00078 }
00079 
00080 int
00081 __lo_ieee_finite (double x)
00082 {
00083 #if defined (HAVE_FINITE)
00084   return finite (x) != 0 && ! __lo_ieee_isnan (x);
00085 #elif defined (HAVE_ISINF)
00086   return (! isinf (x) && ! __lo_ieee_isnan (x));
00087 #else
00088   return ! __lo_ieee_isnan (x);
00089 #endif
00090 }
00091 
00092 int
00093 __lo_ieee_isinf (double x)
00094 {
00095 #if defined (HAVE_ISINF)
00096   return isinf (x);
00097 #elif defined (HAVE_FINITE)
00098   return (! (finite (x) || __lo_ieee_isnan (x)));
00099 #else
00100   return 0;
00101 #endif
00102 }
00103 
00104 int
00105 __lo_ieee_is_NA (double x)
00106 {
00107 #if defined (HAVE_ISNAN)
00108   lo_ieee_double t;
00109   t.value = x;
00110   return (isnan (x) && t.word[lo_ieee_hw] == LO_IEEE_NA_HW
00111           && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0;
00112 #else
00113   return 0;
00114 #endif
00115 }
00116 
00117 int
00118 __lo_ieee_is_old_NA (double x)
00119 {
00120 #if defined (HAVE_ISNAN)
00121   lo_ieee_double t;
00122   t.value = x;
00123   return (isnan (x) && t.word[lo_ieee_lw] == LO_IEEE_NA_LW_OLD
00124           && t.word[lo_ieee_hw] == LO_IEEE_NA_HW_OLD) ? 1 : 0;
00125 #else
00126   return 0;
00127 #endif
00128 }
00129 
00130 double
00131 __lo_ieee_replace_old_NA (double x)
00132 {
00133   if (__lo_ieee_is_old_NA (x))
00134     return lo_ieee_na_value ();
00135   else
00136     return x;
00137 }
00138 
00139 int
00140 __lo_ieee_is_NaN_or_NA (double x)
00141 {
00142   return __lo_ieee_isnan (x);
00143 }
00144 
00145 double
00146 lo_ieee_inf_value (void)
00147 {
00148   return octave_Inf;
00149 }
00150 
00151 double
00152 lo_ieee_na_value (void)
00153 {
00154   return octave_NA;
00155 }
00156 
00157 double
00158 lo_ieee_nan_value (void)
00159 {
00160   return octave_NaN;
00161 }
00162 
00163 #if ! (defined (signbit) || defined (HAVE_DECL_SIGNBIT)) && defined (HAVE_SIGNBIT)
00164 extern int signbit (double);
00165 #endif
00166 
00167 int
00168 __lo_ieee_signbit (double x)
00169 {
00170 /* In the following definitions, only check x < 0 explicitly to avoid
00171    a function call when it looks like signbit or copysign are actually
00172    functions.  */
00173 
00174 #if defined (signbit)
00175   return signbit (x);
00176 #elif defined (HAVE_SIGNBIT)
00177   return (x < 0 || signbit (x));
00178 #elif defined (copysign)
00179   return (copysign (1.0, x) < 0);
00180 #elif defined (HAVE_COPYSIGN)
00181   return (x < 0 || copysign (1.0, x) < 0);
00182 #else
00183   return x < 0;
00184 #endif
00185 }
00186 
00187 int
00188 __lo_ieee_float_isnan (float x)
00189 {
00190 #if defined (HAVE_ISNAN)
00191   return isnan (x);
00192 #else
00193   return 0;
00194 #endif
00195 }
00196 
00197 int
00198 __lo_ieee_float_finite (float x)
00199 {
00200 #if defined (HAVE_FINITE)
00201   return finite (x) != 0 && ! __lo_ieee_float_isnan (x);
00202 #elif defined (HAVE_ISINF)
00203   return (! isinf (x) && ! __lo_ieee_float_isnan (x));
00204 #else
00205   return ! __lo_ieee_float_isnan (x);
00206 #endif
00207 }
00208 
00209 int
00210 __lo_ieee_float_isinf (float x)
00211 {
00212 #if defined (HAVE_ISINF)
00213   return isinf (x);
00214 #elif defined (HAVE_FINITE)
00215   return (! (finite (x) || __lo_ieee_float_isnan (x)));
00216 #else
00217   return 0;
00218 #endif
00219 }
00220 
00221 int
00222 __lo_ieee_float_is_NA (float x)
00223 {
00224 #if defined (HAVE_ISNAN)
00225   lo_ieee_float t;
00226   t.value = x;
00227   return (isnan (x) && (t.word == LO_IEEE_NA_FLOAT)) ? 1 : 0;
00228 #else
00229   return 0;
00230 #endif
00231 }
00232 
00233 int
00234 __lo_ieee_float_is_NaN_or_NA (float x)
00235 {
00236   return __lo_ieee_float_isnan (x);
00237 }
00238 
00239 float
00240 lo_ieee_float_inf_value (void)
00241 {
00242   return octave_Float_Inf;
00243 }
00244 
00245 float
00246 lo_ieee_float_na_value (void)
00247 {
00248   return octave_Float_NA;
00249 }
00250 
00251 float
00252 lo_ieee_float_nan_value (void)
00253 {
00254   return octave_Float_NaN;
00255 }
00256 
00257 #if ! (defined (signbit) || defined (HAVE_DECL_SIGNBIT)) && defined (HAVE_SIGNBIT)
00258 extern int signbit (float);
00259 #endif
00260 
00261 int
00262 __lo_ieee_float_signbit (float x)
00263 {
00264 /* In the following definitions, only check x < 0 explicitly to avoid
00265    a function call when it looks like signbit or copysign are actually
00266    functions.  */
00267 
00268 #if defined (signbit)
00269   return signbit (x);
00270 #elif defined (HAVE_SIGNBIT)
00271   return (x < 0 || signbit (x));
00272 #elif defined (copysign)
00273   return (copysign (1.0, x) < 0);
00274 #elif defined (HAVE_COPYSIGN)
00275   return (x < 0 || copysign (1.0, x) < 0);
00276 #else
00277   return x < 0;
00278 #endif
00279 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines