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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4072
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <float.h> |
|
29 #include <math.h> |
|
30 |
|
31 #if defined (HAVE_FLOATINGPOINT_H) |
|
32 #include <floatingpoint.h> |
|
33 #endif |
|
34 |
|
35 #if defined (HAVE_IEEEFP_H) |
|
36 #include <ieeefp.h> |
|
37 #endif |
|
38 |
|
39 #if defined (HAVE_NAN_H) |
|
40 #if defined (SCO) |
|
41 #define _IEEE 1 |
|
42 #endif |
|
43 #include <nan.h> |
|
44 #if defined (SCO) |
|
45 #undef _IEEE |
|
46 #endif |
|
47 #endif |
|
48 |
|
49 #include "lo-ieee.h" |
|
50 |
6061
|
51 #if ! defined (HAVE_ISNAN) && defined (HAVE__ISNAN) |
|
52 #define isnan _isnan |
|
53 #define HAVE_ISNAN 1 |
|
54 #endif |
|
55 |
|
56 #if ! defined (HAVE_FINITE) && defined (HAVE__FINITE) |
|
57 #define finite _finite |
|
58 #define HAVE_FINITE 1 |
|
59 #endif |
|
60 |
|
61 #if ! defined (HAVE_COPYSIGN) && defined (HAVE__COPYSIGN) |
|
62 #define copysign _copysign |
|
63 #define HAVE_COPYSIGN 1 |
|
64 #endif |
|
65 |
4074
|
66 #if defined (_AIX) && defined (__GNUG__) |
|
67 #undef finite |
|
68 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
69 #endif |
|
70 |
4102
|
71 /* Octave's idea of infinity. */ |
|
72 double octave_Inf; |
|
73 |
|
74 /* Octave's idea of a missing value. */ |
|
75 double octave_NA; |
|
76 |
|
77 /* Octave's idea of not a number. */ |
|
78 double octave_NaN; |
|
79 |
|
80 int lo_ieee_hw; |
|
81 int lo_ieee_lw; |
|
82 |
4072
|
83 #if defined (SCO) |
|
84 |
|
85 int |
|
86 isnan (double x) |
|
87 { |
|
88 return (IsNANorINF (x) && NaN (x) && ! IsINF (x)) ? 1 : 0; |
|
89 } |
|
90 |
|
91 int |
|
92 isinf (double x) |
|
93 { |
|
94 return (IsNANorINF (x) && IsINF (x)) ? 1 : 0; |
|
95 } |
|
96 |
|
97 #endif |
|
98 |
|
99 int |
4074
|
100 lo_ieee_isnan (double x) |
4072
|
101 { |
4074
|
102 #if defined (HAVE_ISNAN) |
5389
|
103 return isnan (x); |
4074
|
104 #else |
|
105 return 0; |
|
106 #endif |
4072
|
107 } |
|
108 |
|
109 int |
4074
|
110 lo_ieee_finite (double x) |
4072
|
111 { |
4074
|
112 #if defined (HAVE_FINITE) |
5389
|
113 return finite (x) != 0 && ! lo_ieee_isnan (x); |
4074
|
114 #elif defined (HAVE_ISINF) |
5389
|
115 return (! isinf (x) && ! lo_ieee_isnan (x)); |
4074
|
116 #else |
5389
|
117 return ! lo_ieee_isnan (x); |
4074
|
118 #endif |
|
119 } |
|
120 |
|
121 int |
|
122 lo_ieee_isinf (double x) |
|
123 { |
|
124 #if defined (HAVE_ISINF) |
|
125 return isinf (x); |
|
126 #elif defined (HAVE_FINITE) |
5389
|
127 return (! (finite (x) || lo_ieee_isnan (x))); |
4074
|
128 #else |
|
129 return 0; |
|
130 #endif |
4072
|
131 } |
|
132 |
4075
|
133 int |
|
134 lo_ieee_is_NA (double x) |
|
135 { |
6061
|
136 #if defined (HAVE_ISNAN) |
4075
|
137 lo_ieee_double t; |
|
138 t.value = x; |
|
139 return (isnan (x) && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0; |
4469
|
140 #else |
|
141 return 0; |
|
142 #endif |
4075
|
143 } |
|
144 |
|
145 int |
|
146 lo_ieee_is_NaN_or_NA (double x) |
|
147 { |
5389
|
148 return lo_ieee_isnan (x); |
4075
|
149 } |
|
150 |
4102
|
151 double |
|
152 lo_ieee_inf_value (void) |
|
153 { |
|
154 return octave_Inf; |
|
155 } |
|
156 |
|
157 double |
|
158 lo_ieee_na_value (void) |
|
159 { |
|
160 return octave_NA; |
|
161 } |
|
162 |
|
163 double |
|
164 lo_ieee_nan_value (void) |
|
165 { |
|
166 return octave_NaN; |
|
167 } |
|
168 |
5098
|
169 #if ! (defined (signbit) || defined (HAVE_DECL_SIGNBIT)) && defined (HAVE_SIGNBIT) |
|
170 extern int signbit (double); |
|
171 #endif |
|
172 |
|
173 int |
|
174 lo_ieee_signbit (double x) |
|
175 { |
|
176 /* In the following definitions, only check x < 0 explicitly to avoid |
|
177 a function call when it looks like signbit or copysign are actually |
|
178 functions. */ |
|
179 |
|
180 #if defined (signbit) |
|
181 return signbit (x); |
|
182 #elif defined (HAVE_SIGNBIT) |
|
183 return (x < 0 || signbit (x)); |
|
184 #elif defined (copysign) |
|
185 return (copysign (1.0, x) < 0); |
|
186 #elif defined (HAVE_COPYSIGN) |
|
187 return (x < 0 || copysign (1.0, x) < 0); |
|
188 #else |
|
189 return x < 0; |
|
190 #endif |
|
191 } |
|
192 |
4072
|
193 /* |
|
194 ;;; Local Variables: *** |
|
195 ;;; mode: C++ *** |
|
196 ;;; End: *** |
|
197 */ |