GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
lo-ieee.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 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
39static double lo_inf;
40static double lo_nan;
41static double lo_na;
42
43static float lo_float_inf;
44static float lo_float_nan;
45static float lo_float_na;
46
47static int lo_ieee_hw;
48static int lo_ieee_lw;
49
50#if defined (HAVE_MIPS_NAN)
51 #define LO_IEEE_NA_HW 0x7FF040F4
52#else
53 #define LO_IEEE_NA_HW 0x7FF840F4
54#endif
55#define LO_IEEE_NA_LW 0x40000000
56#define LO_IEEE_NA_FLOAT 0x7FC207A2
57
58int
60{
62 t.value = x;
63 return (std::isnan (x) && t.word[lo_ieee_hw] == LO_IEEE_NA_HW
64 && t.word[lo_ieee_lw] == LO_IEEE_NA_LW) ? 1 : 0;
65}
66
67double
69{
71
72 return lo_inf;
73}
74
75double
77{
79
80 return lo_na;
81}
82
83double
85{
87
88 return lo_nan;
89}
90
91int
93{
95 t.value = x;
96 return (std::isnan (x) && (t.word == LO_IEEE_NA_FLOAT)) ? 1 : 0;
97}
98
99float
101{
103
104 return lo_float_inf;
105}
106
107float
109{
111
112 return lo_float_na;
113}
114
115float
117{
119
120 return lo_float_nan;
121}
122
123void
125{
126 static bool initialized = false;
127
128 if (! initialized)
129 {
130 octave::mach_info::float_format ff = octave::mach_info::native_float_format ();
131
132 switch (ff)
133 {
134 case octave::mach_info::flt_fmt_ieee_big_endian:
135 case octave::mach_info::flt_fmt_ieee_little_endian:
136 {
137 lo_nan = std::numeric_limits<double>::quiet_NaN ();
138 lo_inf = std::numeric_limits<double>::infinity ();
139
140 lo_float_nan = std::numeric_limits<float>::quiet_NaN ();
141 lo_float_inf = std::numeric_limits<float>::infinity ();
142
143 // The following is patterned after code in R.
144
145 if (ff == octave::mach_info::flt_fmt_ieee_big_endian)
146 {
147 lo_ieee_hw = 0;
148 lo_ieee_lw = 1;
149 }
150 else
151 {
152 lo_ieee_hw = 1;
153 lo_ieee_lw = 0;
154 }
155
157 t.word[lo_ieee_hw] = LO_IEEE_NA_HW;
158 t.word[lo_ieee_lw] = LO_IEEE_NA_LW;
159
160 lo_na = t.value;
161
162 lo_ieee_float tf;
164
165 lo_float_na = tf.value;
166 }
167 break;
168
169 default:
170 // If the format is unknown, then you will probably not have a
171 // useful system, so we will abort here. Anyone wishing to
172 // experiment with building Octave on a system without IEEE
173 // floating point should be capable of removing this check and
174 // the configure test.
175 //
176 // If the error handler returns, then we'll abort.
177
178 (*current_liboctave_error_handler)
179 ("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?");
180
181 abort ();
182 }
183
184 initialized = true;
185 }
186}
int __lo_ieee_float_is_NA(float x)
Definition lo-ieee.cc:92
double lo_ieee_na_value()
Definition lo-ieee.cc:76
float lo_ieee_float_nan_value()
Definition lo-ieee.cc:116
float lo_ieee_float_na_value()
Definition lo-ieee.cc:108
int __lo_ieee_is_NA(double x)
Definition lo-ieee.cc:59
double lo_ieee_nan_value()
Definition lo-ieee.cc:84
float lo_ieee_float_inf_value()
Definition lo-ieee.cc:100
#define LO_IEEE_NA_HW
Definition lo-ieee.cc:53
#define LO_IEEE_NA_LW
Definition lo-ieee.cc:55
#define LO_IEEE_NA_FLOAT
Definition lo-ieee.cc:56
double lo_ieee_inf_value()
Definition lo-ieee.cc:68
void octave_ieee_init()
Definition lo-ieee.cc:124
F77_RET_T const F77_DBLE * x
double value
Definition lo-ieee.h:62
unsigned int word[2]
Definition lo-ieee.h:63
float value
Definition lo-ieee.h:68
unsigned int word
Definition lo-ieee.h:69