scalar.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27Typedef
28 Foam::scalar
29
30Description
31 A floating-point number identical to float or double depending on
32 whether WM_SP, WM_SPDP or WM_DP is defined.
33
34SourceFiles
35 scalar.C
36
37\*---------------------------------------------------------------------------*/
38
39#ifndef scalar_H
40#define scalar_H
41
42#include "floatScalar.H"
43#include "doubleScalar.H"
44
45// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46
47// Typedefs (floatScalar, doubleScalar, scalar, solveScalar) in scalarFwd.H
48
49#if defined(WM_SP) || defined(WM_SPDP)
50
51// With scalar == (float), solveScalar == (float | double)
52
53namespace Foam
54{
55 constexpr scalar GREAT = floatScalarGREAT;
56 constexpr scalar ROOTGREAT = floatScalarROOTGREAT;
57 constexpr scalar VGREAT = floatScalarVGREAT;
58 constexpr scalar ROOTVGREAT = floatScalarROOTVGREAT;
59 constexpr scalar SMALL = floatScalarSMALL;
60 constexpr scalar ROOTSMALL = floatScalarROOTSMALL;
61 constexpr scalar VSMALL = floatScalarVSMALL;
62 constexpr scalar ROOTVSMALL = floatScalarROOTVSMALL;
63
64 #ifdef COMPAT_OPENFOAM_ORG
66 // Accommodate name changes from 2018-01
67 constexpr scalar great = floatScalarGREAT;
68 constexpr scalar rootGreat = floatScalarROOTGREAT;
69 constexpr scalar vGreat = floatScalarVGREAT;
70 constexpr scalar rootVGreat = floatScalarROOTVGREAT;
71 constexpr scalar small = floatScalarSMALL;
72 constexpr scalar rootSmall = floatScalarROOTSMALL;
73 constexpr scalar vSmall = floatScalarVSMALL;
74 constexpr scalar rootVSmall = floatScalarROOTVSMALL;
76 #endif
77
78
79 //- Read scalar from c-string and return value
80 inline scalar readScalar(const char* buf)
81 {
82 return readFloat(buf);
83 }
84
85 //- Read scalar from c-string into argument. Return true on success.
86 inline bool readScalar(const char* buf, scalar& val)
87 {
88 return readFloat(buf, val);
89 }
90
91 //- Read scalar from string and return value
92 inline scalar readScalar(const std::string& str)
93 {
94 return readFloat(str);
95 }
96
97 //- Read scalar from string into argument. Return true on success.
98 inline bool readScalar(const std::string& str, scalar& val)
99 {
100 return readFloat(str, val);
101 }
102
103 //- Read scalar from stream.
104 scalar readScalar(Istream& is);
105
106 //- Read raw scalar from binary stream.
107 // \note No internal check for binary vs ascii,
108 // the caller knows what they are doing
109 scalar readRawScalar(Istream& is);
110
111 //- Read raw scalar(s) from binary stream.
112 // \note No internal check for binary vs ascii,
113 // the caller knows what they are doing
114 void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
115}
116
117#elif defined(WM_DP)
118
119// With scalar == (double), solveScalar == (double)
120
121namespace Foam
122{
123 constexpr scalar GREAT = doubleScalarGREAT;
124 constexpr scalar ROOTGREAT = doubleScalarROOTGREAT;
125 constexpr scalar VGREAT = doubleScalarVGREAT;
126 constexpr scalar ROOTVGREAT = doubleScalarROOTVGREAT;
127 constexpr scalar SMALL = doubleScalarSMALL;
128 constexpr scalar ROOTSMALL = doubleScalarROOTSMALL;
129 constexpr scalar VSMALL = doubleScalarVSMALL;
130 constexpr scalar ROOTVSMALL = doubleScalarROOTVSMALL;
131
132 #ifdef COMPAT_OPENFOAM_ORG
134 // Accommodate name changes from 2018-01
135 constexpr scalar great = doubleScalarGREAT;
136 constexpr scalar rootGreat = doubleScalarROOTGREAT;
137 constexpr scalar vGreat = doubleScalarVGREAT;
138 constexpr scalar rootVGreat = doubleScalarROOTVGREAT;
139 constexpr scalar small = doubleScalarSMALL;
140 constexpr scalar rootSmall = doubleScalarROOTSMALL;
141 constexpr scalar vSmall = doubleScalarVSMALL;
142 constexpr scalar rootVSmall = doubleScalarROOTVSMALL;
144 #endif
145
146
147 //- Read scalar from c-string and return value
148 inline scalar readScalar(const char* buf)
149 {
150 return readDouble(buf);
151 }
152
153 //- Read scalar from c-string into argument. Return true on success.
154 inline bool readScalar(const char* buf, scalar& val)
155 {
156 return readDouble(buf, val);
157 }
158
159 //- Read scalar from string and return value
160 inline scalar readScalar(const std::string& str)
161 {
162 return readDouble(str);
163 }
164
165 //- Read scalar from string into argument. Return true on success.
166 inline bool readScalar(const std::string& str, scalar& val)
167 {
168 return readDouble(str, val);
169 }
170
171
172 //- Read scalar from stream.
173 scalar readScalar(Istream& is);
174
175 //- Read raw scalar from binary stream.
176 // \note No internal check for binary vs ascii,
177 // the caller knows what they are doing
178 scalar readRawScalar(Istream& is);
179
180 //- Read raw scalar(s) from binary stream.
181 // \note No internal check for binary vs ascii,
182 // the caller knows what they are doing
183 void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
184}
185
186#else
187// #error "PRECISION must be set to WM_SP, WM_SPDP or WM_DP"
188#endif
189
190// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
191
192// Type conversions (narrowing)
193
194namespace Foam
195{
196
197//- Type narrowing from double to float
198// Overflow: silently fix, or raise error?
199inline float narrowFloat(const double val)
200{
201 // Single statement - future constexpr?
202 return
203 (
206 : (val > -floatScalarVSMALL && val < floatScalarVSMALL) // underflow
207 ? 0
208 : static_cast<float>(val)
209 );
210}
211
212} // End namespace Foam
213
214
215// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216
217namespace Foam
218{
219 //- Type to use for extended precision
220 template<>
221 class typeOfSolve<scalar>
222 {
223 public:
224
225 typedef solveScalar type;
226 };
227}
228
229// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230
231#endif
232
233// ************************************************************************* //
The extended precision type (solveScalar for float)
Definition: products.H:79
Namespace for OpenFOAM.
constexpr floatScalar floatScalarVGREAT
Definition: floatScalar.H:59
float narrowFloat(const double val)
Type narrowing from double to float.
Definition: scalar.H:199
constexpr doubleScalar doubleScalarROOTVGREAT
Definition: doubleScalar.H:60
constexpr floatScalar floatScalarGREAT
Definition: floatScalar.H:57
constexpr floatScalar floatScalarROOTVGREAT
Definition: floatScalar.H:60
constexpr floatScalar floatScalarROOTVSMALL
Definition: floatScalar.H:64
constexpr floatScalar floatScalarSMALL
Definition: floatScalar.H:61
constexpr doubleScalar doubleScalarROOTSMALL
Definition: doubleScalar.H:62
constexpr doubleScalar doubleScalarVGREAT
Definition: doubleScalar.H:59
constexpr floatScalar floatScalarROOTSMALL
Definition: floatScalar.H:62
constexpr doubleScalar doubleScalarGREAT
Definition: doubleScalar.H:57
constexpr floatScalar floatScalarVSMALL
Definition: floatScalar.H:63
constexpr floatScalar floatScalarROOTGREAT
Definition: floatScalar.H:58
constexpr doubleScalar doubleScalarROOTGREAT
Definition: doubleScalar.H:58
constexpr doubleScalar doubleScalarVSMALL
Definition: doubleScalar.H:63
constexpr doubleScalar doubleScalarSMALL
Definition: doubleScalar.H:61
constexpr doubleScalar doubleScalarROOTVSMALL
Definition: doubleScalar.H:64