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 |
4074
|
51 #if defined (_AIX) && defined (__GNUG__) |
|
52 #undef finite |
|
53 #define finite(x) ((x) < DBL_MAX && (x) > -DBL_MAX) |
|
54 #endif |
|
55 |
4102
|
56 /* Octave's idea of infinity. */ |
|
57 double octave_Inf; |
|
58 |
|
59 /* Octave's idea of a missing value. */ |
|
60 double octave_NA; |
|
61 |
|
62 /* Octave's idea of not a number. */ |
|
63 double octave_NaN; |
|
64 |
|
65 int lo_ieee_hw; |
|
66 int lo_ieee_lw; |
|
67 |
4072
|
68 #if defined (SCO) |
|
69 |
|
70 int |
|
71 isnan (double x) |
|
72 { |
|
73 return (IsNANorINF (x) && NaN (x) && ! IsINF (x)) ? 1 : 0; |
|
74 } |
|
75 |
|
76 int |
|
77 isinf (double x) |
|
78 { |
|
79 return (IsNANorINF (x) && IsINF (x)) ? 1 : 0; |
|
80 } |
|
81 |
|
82 #endif |
|
83 |
|
84 int |
4074
|
85 lo_ieee_isnan (double x) |
4072
|
86 { |
4074
|
87 #if defined (HAVE_ISNAN) |
5389
|
88 return isnan (x); |
4074
|
89 #else |
|
90 return 0; |
|
91 #endif |
4072
|
92 } |
|
93 |
|
94 int |
4074
|
95 lo_ieee_finite (double x) |
4072
|
96 { |
4074
|
97 #if defined (HAVE_FINITE) |
5389
|
98 return finite (x) != 0 && ! lo_ieee_isnan (x); |
4074
|
99 #elif defined (HAVE_ISINF) |
5389
|
100 return (! isinf (x) && ! lo_ieee_isnan (x)); |
4074
|
101 #else |
5389
|
102 return ! lo_ieee_isnan (x); |
4074
|
103 #endif |
|
104 } |
|
105 |
|
106 int |
|
107 lo_ieee_isinf (double x) |
|
108 { |
|
109 #if defined (HAVE_ISINF) |
|
110 return isinf (x); |
|
111 #elif defined (HAVE_FINITE) |
5389
|
112 return (! (finite (x) || lo_ieee_isnan (x))); |
4074
|
113 #else |
|
114 return 0; |
|
115 #endif |
4072
|
116 } |
|
117 |
4075
|
118 int |
|
119 lo_ieee_is_NA (double x) |
|
120 { |
4469
|
121 #if defined HAVE_ISNAN |
4075
|
122 lo_ieee_double t; |
|
123 t.value = x; |
|
124 return (isnan (x) && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0; |
4469
|
125 #else |
|
126 return 0; |
|
127 #endif |
4075
|
128 } |
|
129 |
|
130 int |
|
131 lo_ieee_is_NaN_or_NA (double x) |
|
132 { |
5389
|
133 return lo_ieee_isnan (x); |
4075
|
134 } |
|
135 |
4102
|
136 double |
|
137 lo_ieee_inf_value (void) |
|
138 { |
|
139 return octave_Inf; |
|
140 } |
|
141 |
|
142 double |
|
143 lo_ieee_na_value (void) |
|
144 { |
|
145 return octave_NA; |
|
146 } |
|
147 |
|
148 double |
|
149 lo_ieee_nan_value (void) |
|
150 { |
|
151 return octave_NaN; |
|
152 } |
|
153 |
5098
|
154 #if ! (defined (signbit) || defined (HAVE_DECL_SIGNBIT)) && defined (HAVE_SIGNBIT) |
|
155 extern int signbit (double); |
|
156 #endif |
|
157 |
|
158 int |
|
159 lo_ieee_signbit (double x) |
|
160 { |
|
161 /* In the following definitions, only check x < 0 explicitly to avoid |
|
162 a function call when it looks like signbit or copysign are actually |
|
163 functions. */ |
|
164 |
|
165 #if defined (signbit) |
|
166 return signbit (x); |
|
167 #elif defined (HAVE_SIGNBIT) |
|
168 return (x < 0 || signbit (x)); |
|
169 #elif defined (copysign) |
|
170 return (copysign (1.0, x) < 0); |
|
171 #elif defined (HAVE_COPYSIGN) |
|
172 return (x < 0 || copysign (1.0, x) < 0); |
|
173 #else |
|
174 return x < 0; |
|
175 #endif |
|
176 } |
|
177 |
4072
|
178 /* |
|
179 ;;; Local Variables: *** |
|
180 ;;; mode: C++ *** |
|
181 ;;; End: *** |
|
182 */ |