GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-ieee.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <cmath>
28 #include <cstdlib>
29 
30 #include <limits>
31 
32 #include "lo-error.h"
33 #include "lo-ieee.h"
34 #include "mach-info.h"
35 
36 static double lo_inf;
37 static double lo_nan;
38 static double lo_na;
39 
40 static float lo_float_inf;
41 static float lo_float_nan;
42 static float lo_float_na;
43 
44 static int lo_ieee_hw;
45 static int lo_ieee_lw;
46 
47 int
49 {
51  t.value = x;
52  return (__lo_ieee_isnan (x) && t.word[lo_ieee_hw] == LO_IEEE_NA_HW
53  && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0;
54 }
55 
56 int
58 {
60  t.value = x;
61  return (__lo_ieee_isnan (x) && t.word[lo_ieee_lw] == LO_IEEE_NA_LW_OLD
62  && t.word[lo_ieee_hw] == LO_IEEE_NA_HW_OLD) ? 1 : 0;
63 }
64 
65 double
67 {
68  if (__lo_ieee_is_old_NA (x))
69  return lo_ieee_na_value ();
70  else
71  return x;
72 }
73 
74 double
76 {
78 
79  return lo_inf;
80 }
81 
82 double
84 {
86 
87  return lo_na;
88 }
89 
90 double
92 {
94 
95  return lo_nan;
96 }
97 
98 int
100 {
102  t.value = x;
103  return (__lo_ieee_float_isnan (x) && (t.word == LO_IEEE_NA_FLOAT)) ? 1 : 0;
104 }
105 
106 float
108 {
109  octave_ieee_init ();
110 
111  return lo_float_inf;
112 }
113 
114 float
116 {
117  octave_ieee_init ();
118 
119  return lo_float_na;
120 }
121 
122 float
124 {
125  octave_ieee_init ();
126 
127  return lo_float_nan;
128 }
129 
130 void
132 {
133  bool initialized = false;
134 
135  if (! initialized)
136  {
138 
139  switch (ff)
140  {
143  {
144  lo_nan = std::numeric_limits<double>::quiet_NaN ();
145  lo_inf = std::numeric_limits<double>::infinity ();
146 
147  lo_float_nan = std::numeric_limits<float>::quiet_NaN ();
148  lo_float_inf = std::numeric_limits<float>::infinity ();
149 
150  // The following is patterned after code in R.
151 
153  {
154  lo_ieee_hw = 0;
155  lo_ieee_lw = 1;
156  }
157  else
158  {
159  lo_ieee_hw = 1;
160  lo_ieee_lw = 0;
161  }
162 
164  t.word[lo_ieee_hw] = LO_IEEE_NA_HW;
165  t.word[lo_ieee_lw] = LO_IEEE_NA_LW;
166 
167  lo_na = t.value;
168 
169  lo_ieee_float tf;
170  tf.word = LO_IEEE_NA_FLOAT;
171 
172  lo_float_na = tf.value;
173  }
174  break;
175 
176  default:
177  // If the format is unknown, then you will probably not have a
178  // useful system, so we will abort here. Anyone wishing to
179  // experiment with building Octave on a system without IEEE
180  // floating point should be capable of removing this check and
181  // the configure test.
182  //
183  // If the the error handler returns, then we'll abort.
184 
185  (*current_liboctave_error_handler)
186  ("lo_ieee_init: floating point format is not IEEE! Maybe DLAMCH is miscompiled, or you are using some strange system without IEEE floating point math?");
187 
188  abort ();
189  }
190 
191  initialized = true;
192  }
193 }
float lo_ieee_float_na_value(void)
Definition: lo-ieee.cc:115
double __lo_ieee_replace_old_NA(double x)
Definition: lo-ieee.cc:66
static float lo_float_nan
Definition: lo-ieee.cc:41
static double lo_inf
Definition: lo-ieee.cc:36
int __lo_ieee_float_is_NA(float x)
Definition: lo-ieee.cc:99
static int lo_ieee_hw
Definition: lo-ieee.cc:44
double lo_ieee_inf_value(void)
Definition: lo-ieee.cc:75
double lo_ieee_nan_value(void)
Definition: lo-ieee.cc:91
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function t
Definition: ov-usr-fcn.cc:997
static int lo_ieee_lw
Definition: lo-ieee.cc:45
static float lo_float_na
Definition: lo-ieee.cc:42
void octave_ieee_init(void)
Definition: lo-ieee.cc:131
double lo_ieee_na_value(void)
Definition: lo-ieee.cc:83
float value
Definition: lo-ieee.h:63
static double lo_na
Definition: lo-ieee.cc:38
static float lo_float_inf
Definition: lo-ieee.cc:40
float lo_ieee_float_inf_value(void)
Definition: lo-ieee.cc:107
float_format native_float_format(void)
Definition: mach-info.cc:62
static double lo_nan
Definition: lo-ieee.cc:37
#define LO_IEEE_NA_FLOAT
Definition: lo-ieee.h:71
#define LO_IEEE_NA_HW_OLD
Definition: lo-ieee.h:67
#define LO_IEEE_NA_HW
Definition: lo-ieee.h:69
static bool initialized
Definition: defaults.cc:48
float lo_ieee_float_nan_value(void)
Definition: lo-ieee.cc:123
#define LO_IEEE_NA_LW_OLD
Definition: lo-ieee.h:68
int __lo_ieee_isnan(double x)
Definition: lo-ieee.h:75
int __lo_ieee_is_NA(double x)
Definition: lo-ieee.cc:48
unsigned int word
Definition: lo-ieee.h:64
int __lo_ieee_float_isnan(float x)
Definition: lo-ieee.h:89
int __lo_ieee_is_old_NA(double x)
Definition: lo-ieee.cc:57
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
#define LO_IEEE_NA_LW
Definition: lo-ieee.h:70