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 -------------------------------------------------------------------------------
11 License
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 
27 Typedef
28  Foam::scalar
29 
30 Description
31  A floating-point number identical to float or double depending on
32  whether WM_SP, WM_SPDP or WM_DP is defined.
33 
34 SourceFiles
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 
53 namespace 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
65  // 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 scalar from stream if present or return default value
107  scalar readScalarOrDefault(Istream& is, const scalar defaultValue);
108 
109  //- Read raw scalar from binary stream.
110  // \note No internal check for binary vs ascii,
111  // the caller knows what they are doing
112  scalar readRawScalar(Istream& is);
113 
114  //- Read raw scalar(s) from binary stream.
115  // \note No internal check for binary vs ascii,
116  // the caller knows what they are doing
117  void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
118 }
119 
120 #elif defined(WM_DP)
121 
122 // With scalar == (double), solveScalar == (double)
123 
124 namespace Foam
125 {
126  constexpr scalar GREAT = doubleScalarGREAT;
127  constexpr scalar ROOTGREAT = doubleScalarROOTGREAT;
128  constexpr scalar VGREAT = doubleScalarVGREAT;
129  constexpr scalar ROOTVGREAT = doubleScalarROOTVGREAT;
130  constexpr scalar SMALL = doubleScalarSMALL;
131  constexpr scalar ROOTSMALL = doubleScalarROOTSMALL;
132  constexpr scalar VSMALL = doubleScalarVSMALL;
133  constexpr scalar ROOTVSMALL = doubleScalarROOTVSMALL;
134 
135  #ifdef COMPAT_OPENFOAM_ORG
136  // Accommodate name changes from 2018-01
138  constexpr scalar great = doubleScalarGREAT;
139  constexpr scalar rootGreat = doubleScalarROOTGREAT;
140  constexpr scalar vGreat = doubleScalarVGREAT;
141  constexpr scalar rootVGreat = doubleScalarROOTVGREAT;
142  constexpr scalar small = doubleScalarSMALL;
143  constexpr scalar rootSmall = doubleScalarROOTSMALL;
144  constexpr scalar vSmall = doubleScalarVSMALL;
145  constexpr scalar rootVSmall = doubleScalarROOTVSMALL;
147  #endif
148 
149 
150  //- Read scalar from c-string and return value
151  inline scalar readScalar(const char* buf)
152  {
153  return readDouble(buf);
154  }
155 
156  //- Read scalar from c-string into argument. Return true on success.
157  inline bool readScalar(const char* buf, scalar& val)
158  {
159  return readDouble(buf, val);
160  }
161 
162  //- Read scalar from string and return value
163  inline scalar readScalar(const std::string& str)
164  {
165  return readDouble(str);
166  }
167 
168  //- Read scalar from string into argument. Return true on success.
169  inline bool readScalar(const std::string& str, scalar& val)
170  {
171  return readDouble(str, val);
172  }
173 
174 
175  //- Read scalar from stream.
176  scalar readScalar(Istream& is);
177 
178  //- Read scalar from stream if present or return default value
179  scalar readScalarOrDefault(Istream& is, const scalar defaultValue);
180 
181  //- Read raw scalar from binary stream.
182  // \note No internal check for binary vs ascii,
183  // the caller knows what they are doing
184  scalar readRawScalar(Istream& is);
185 
186  //- Read raw scalar(s) from binary stream.
187  // \note No internal check for binary vs ascii,
188  // the caller knows what they are doing
189  void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
190 }
191 
192 #else
193 // #error "PRECISION must be set to WM_SP, WM_SPDP or WM_DP"
194 #endif
195 
196 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
197 
198 // Type conversions (narrowing)
199 
200 namespace Foam
201 {
202 
203 //- Type narrowing from double to float
204 // Overflow: silently fix, or raise error?
205 inline float narrowFloat(const double val)
206 {
207  // Single statement - future constexpr?
208  return
209  (
212  : (val > -floatScalarVSMALL && val < floatScalarVSMALL) // underflow
213  ? 0
214  : static_cast<float>(val)
215  );
216 }
217 
218 } // End namespace Foam
219 
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 
223 namespace Foam
224 {
225  //- Type to use for extended precision
226  template<>
227  class typeOfSolve<scalar>
228  {
229  public:
230 
231  typedef solveScalar type;
232  };
233 }
234 
235 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
236 
237 #endif
238 
239 // ************************************************************************* //
constexpr floatScalar floatScalarROOTSMALL
Definition: floatScalar.H:59
float narrowFloat(const double val)
Type narrowing from double to float.
Definition: scalar.H:240
constexpr floatScalar floatScalarROOTVGREAT
Definition: floatScalar.H:57
constexpr doubleScalar doubleScalarVSMALL
Definition: doubleScalar.H:60
constexpr floatScalar floatScalarROOTVSMALL
Definition: floatScalar.H:61
constexpr floatScalar floatScalarSMALL
Definition: floatScalar.H:58
constexpr floatScalar floatScalarROOTGREAT
Definition: floatScalar.H:55
constexpr floatScalar floatScalarVSMALL
Definition: floatScalar.H:60
constexpr doubleScalar doubleScalarROOTVSMALL
Definition: doubleScalar.H:61
constexpr doubleScalar doubleScalarROOTVGREAT
Definition: doubleScalar.H:57
constexpr doubleScalar doubleScalarGREAT
Definition: doubleScalar.H:54
constexpr doubleScalar doubleScalarVGREAT
Definition: doubleScalar.H:56
constexpr doubleScalar doubleScalarSMALL
Definition: doubleScalar.H:58
constexpr doubleScalar doubleScalarROOTSMALL
Definition: doubleScalar.H:59
constexpr floatScalar floatScalarGREAT
Definition: floatScalar.H:54
constexpr floatScalar floatScalarVGREAT
Definition: floatScalar.H:56
constexpr doubleScalar doubleScalarROOTGREAT
Definition: doubleScalar.H:55
Namespace for OpenFOAM.