4072
|
1 /* |
|
2 |
|
3 Copyright (C) 2002 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <float.h> |
|
28 #include <math.h> |
|
29 |
|
30 #if defined (HAVE_FLOATINGPOINT_H) |
|
31 #include <floatingpoint.h> |
|
32 #endif |
|
33 |
|
34 #if defined (HAVE_IEEEFP_H) |
|
35 #include <ieeefp.h> |
|
36 #endif |
|
37 |
|
38 #if defined (HAVE_NAN_H) |
|
39 #if defined (SCO) |
|
40 #define _IEEE 1 |
|
41 #endif |
|
42 #include <nan.h> |
|
43 #if defined (SCO) |
|
44 #undef _IEEE |
|
45 #endif |
|
46 #endif |
|
47 |
|
48 #include "lo-ieee.h" |
|
49 |
4074
|
50 #if defined (_AIX) && defined (__GNUG__) |
|
51 #undef finite |
|
52 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
53 #endif |
|
54 |
4102
|
55 /* Octave's idea of infinity. */ |
|
56 double octave_Inf; |
|
57 |
|
58 /* Octave's idea of a missing value. */ |
|
59 double octave_NA; |
|
60 |
|
61 /* Octave's idea of not a number. */ |
|
62 double octave_NaN; |
|
63 |
|
64 int lo_ieee_hw; |
|
65 int lo_ieee_lw; |
|
66 |
4072
|
67 #if defined (SCO) |
|
68 |
|
69 int |
|
70 isnan (double x) |
|
71 { |
|
72 return (IsNANorINF (x) && NaN (x) && ! IsINF (x)) ? 1 : 0; |
|
73 } |
|
74 |
|
75 int |
|
76 isinf (double x) |
|
77 { |
|
78 return (IsNANorINF (x) && IsINF (x)) ? 1 : 0; |
|
79 } |
|
80 |
|
81 #endif |
|
82 |
|
83 int |
4074
|
84 lo_ieee_isnan (double x) |
4072
|
85 { |
4074
|
86 #if defined (HAVE_ISNAN) |
4075
|
87 return isnan (x) ? ! lo_ieee_is_NA (x) : 0; |
4074
|
88 #else |
|
89 return 0; |
|
90 #endif |
4072
|
91 } |
|
92 |
|
93 int |
4074
|
94 lo_ieee_finite (double x) |
4072
|
95 { |
4074
|
96 #if defined (HAVE_FINITE) |
|
97 return finite (x) != 0 && ! lo_ieee_is_NaN_or_NA (x); |
|
98 #elif defined (HAVE_ISINF) |
|
99 return (! isinf (x) && ! lo_ieee_is_NaN_or_NA (x)); |
|
100 #else |
|
101 return ! lo_ieee_is_NaN_or_NA (x); |
|
102 #endif |
|
103 } |
|
104 |
|
105 int |
|
106 lo_ieee_isinf (double x) |
|
107 { |
|
108 #if defined (HAVE_ISINF) |
|
109 return isinf (x); |
|
110 #elif defined (HAVE_FINITE) |
|
111 return (! (finite (x) || lo_ieee_is_NaN_or_NA (x))); |
|
112 #else |
|
113 return 0; |
|
114 #endif |
4072
|
115 } |
|
116 |
4075
|
117 int |
|
118 lo_ieee_is_NA (double x) |
|
119 { |
4469
|
120 #if defined HAVE_ISNAN |
4075
|
121 lo_ieee_double t; |
|
122 t.value = x; |
|
123 return (isnan (x) && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0; |
4469
|
124 #else |
|
125 return 0; |
|
126 #endif |
4075
|
127 } |
|
128 |
|
129 int |
|
130 lo_ieee_is_NaN_or_NA (double x) |
|
131 { |
4498
|
132 /* Although NA really is an IEEE NaN value, lo_ieee_isnan pretends |
|
133 it is not, so we much check both... */ |
4469
|
134 |
|
135 return lo_ieee_isnan (x) || lo_ieee_is_NA (x); |
4075
|
136 } |
|
137 |
4102
|
138 double |
|
139 lo_ieee_inf_value (void) |
|
140 { |
|
141 return octave_Inf; |
|
142 } |
|
143 |
|
144 double |
|
145 lo_ieee_na_value (void) |
|
146 { |
|
147 return octave_NA; |
|
148 } |
|
149 |
|
150 double |
|
151 lo_ieee_nan_value (void) |
|
152 { |
|
153 return octave_NaN; |
|
154 } |
|
155 |
5098
|
156 #if ! (defined (signbit) || defined (HAVE_DECL_SIGNBIT)) && defined (HAVE_SIGNBIT) |
|
157 extern int signbit (double); |
|
158 #endif |
|
159 |
|
160 int |
|
161 lo_ieee_signbit (double x) |
|
162 { |
|
163 /* In the following definitions, only check x < 0 explicitly to avoid |
|
164 a function call when it looks like signbit or copysign are actually |
|
165 functions. */ |
|
166 |
|
167 #if defined (signbit) |
|
168 return signbit (x); |
|
169 #elif defined (HAVE_SIGNBIT) |
|
170 return (x < 0 || signbit (x)); |
|
171 #elif defined (copysign) |
|
172 return (copysign (1.0, x) < 0); |
|
173 #elif defined (HAVE_COPYSIGN) |
|
174 return (x < 0 || copysign (1.0, x) < 0); |
|
175 #else |
|
176 return x < 0; |
|
177 #endif |
|
178 } |
|
179 |
4072
|
180 /* |
|
181 ;;; Local Variables: *** |
|
182 ;;; mode: C++ *** |
|
183 ;;; End: *** |
|
184 */ |