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