GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <cmath>
31 #include <cstdlib>
32 
33 #include <limits>
34 
35 #include "lo-error.h"
36 #include "lo-ieee.h"
37 #include "mach-info.h"
38 
39 static double lo_inf;
40 static double lo_nan;
41 static double lo_na;
42 
43 static float lo_float_inf;
44 static float lo_float_nan;
45 static float lo_float_na;
46 
47 static int lo_ieee_hw;
48 static int lo_ieee_lw;
49 
50 int
52 {
54  t.value = x;
55  return (__lo_ieee_isnan (x) && t.word[lo_ieee_hw] == LO_IEEE_NA_HW
56  && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0;
57 }
58 
59 int
61 {
63  t.value = x;
65  && t.word[lo_ieee_hw] == LO_IEEE_NA_HW_OLD) ? 1 : 0;
66 }
67 
68 double
70 {
71  if (__lo_ieee_is_old_NA (x))
72  return lo_ieee_na_value ();
73  else
74  return x;
75 }
76 
77 double
79 {
81 
82  return lo_inf;
83 }
84 
85 double
87 {
89 
90  return lo_na;
91 }
92 
93 double
95 {
97 
98  return lo_nan;
99 }
100 
101 int
103 {
104  lo_ieee_float t;
105  t.value = x;
106  return (__lo_ieee_float_isnan (x) && (t.word == LO_IEEE_NA_FLOAT)) ? 1 : 0;
107 }
108 
109 float
111 {
112  octave_ieee_init ();
113 
114  return lo_float_inf;
115 }
116 
117 float
119 {
120  octave_ieee_init ();
121 
122  return lo_float_na;
123 }
124 
125 float
127 {
128  octave_ieee_init ();
129 
130  return lo_float_nan;
131 }
132 
133 void
135 {
136  static bool initialized = false;
137 
138  if (! initialized)
139  {
141 
142  switch (ff)
143  {
146  {
147  lo_nan = std::numeric_limits<double>::quiet_NaN ();
148  lo_inf = std::numeric_limits<double>::infinity ();
149 
150  lo_float_nan = std::numeric_limits<float>::quiet_NaN ();
151  lo_float_inf = std::numeric_limits<float>::infinity ();
152 
153  // The following is patterned after code in R.
154 
156  {
157  lo_ieee_hw = 0;
158  lo_ieee_lw = 1;
159  }
160  else
161  {
162  lo_ieee_hw = 1;
163  lo_ieee_lw = 0;
164  }
165 
166  lo_ieee_double t;
169 
170  lo_na = t.value;
171 
172  lo_ieee_float tf;
173  tf.word = LO_IEEE_NA_FLOAT;
174 
175  lo_float_na = tf.value;
176  }
177  break;
178 
179  default:
180  // If the format is unknown, then you will probably not have a
181  // useful system, so we will abort here. Anyone wishing to
182  // experiment with building Octave on a system without IEEE
183  // floating point should be capable of removing this check and
184  // the configure test.
185  //
186  // If the error handler returns, then we'll abort.
187 
188  (*current_liboctave_error_handler)
189  ("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?");
190 
191  abort ();
192  }
193 
194  initialized = true;
195  }
196 }
double lo_ieee_inf_value(void)
Definition: lo-ieee.cc:78
int __lo_ieee_float_is_NA(float x)
Definition: lo-ieee.cc:102
double lo_ieee_na_value(void)
Definition: lo-ieee.cc:86
float lo_ieee_float_inf_value(void)
Definition: lo-ieee.cc:110
int __lo_ieee_is_old_NA(double x)
Definition: lo-ieee.cc:60
static int lo_ieee_hw
Definition: lo-ieee.cc:47
void octave_ieee_init(void)
Definition: lo-ieee.cc:134
static float lo_float_nan
Definition: lo-ieee.cc:44
static float lo_float_inf
Definition: lo-ieee.cc:43
static double lo_na
Definition: lo-ieee.cc:41
static float lo_float_na
Definition: lo-ieee.cc:45
static int lo_ieee_lw
Definition: lo-ieee.cc:48
int __lo_ieee_is_NA(double x)
Definition: lo-ieee.cc:51
double lo_ieee_nan_value(void)
Definition: lo-ieee.cc:94
static double lo_inf
Definition: lo-ieee.cc:39
float lo_ieee_float_na_value(void)
Definition: lo-ieee.cc:118
float lo_ieee_float_nan_value(void)
Definition: lo-ieee.cc:126
double __lo_ieee_replace_old_NA(double x)
Definition: lo-ieee.cc:69
static double lo_nan
Definition: lo-ieee.cc:40
#define LO_IEEE_NA_HW_OLD
Definition: lo-ieee.h:70
#define LO_IEEE_NA_LW_OLD
Definition: lo-ieee.h:71
#define LO_IEEE_NA_HW
Definition: lo-ieee.h:75
#define LO_IEEE_NA_LW
Definition: lo-ieee.h:77
int __lo_ieee_float_isnan(float x)
Definition: lo-ieee.h:96
#define LO_IEEE_NA_FLOAT
Definition: lo-ieee.h:78
int __lo_ieee_isnan(double x)
Definition: lo-ieee.h:82
F77_RET_T const F77_DBLE * x
float_format native_float_format(void)
Definition: mach-info.cc:65
@ flt_fmt_ieee_little_endian
Definition: mach-info.h:43
double value
Definition: lo-ieee.h:60
unsigned int word[2]
Definition: lo-ieee.h:61
float value
Definition: lo-ieee.h:66
unsigned int word
Definition: lo-ieee.h:67