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-2020 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 VGREAT = floatScalarVGREAT;
57  constexpr scalar ROOTVGREAT = floatScalarROOTVGREAT;
58  constexpr scalar SMALL = floatScalarSMALL;
59  constexpr scalar ROOTSMALL = floatScalarROOTSMALL;
60  constexpr scalar VSMALL = floatScalarVSMALL;
61  constexpr scalar ROOTVSMALL = floatScalarROOTVSMALL;
62 
63  inline scalar readScalar(const char* buf)
64  {
65  return readFloat(buf);
66  }
67 
68  inline bool readScalar(const char* buf, scalar& val)
69  {
70  return readFloat(buf, val);
71  }
72 
73  inline scalar readScalar(const std::string& str)
74  {
75  return readFloat(str);
76  }
77 
78  inline bool readScalar(const std::string& str, scalar& val)
79  {
80  return readFloat(str, val);
81  }
82 
83  //- Read scalar from stream.
84  scalar readScalar(Istream& is);
85 
86  //- Read raw scalar from binary stream.
87  // \note No internal check for binary vs ascii,
88  // the caller knows what they are doing
89  scalar readRawScalar(Istream& is);
90 
91  //- Read raw scalar(s) from binary stream.
92  // \note No internal check for binary vs ascii,
93  // the caller knows what they are doing
94  void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
95 }
96 
97 #elif defined(WM_DP)
98 
99 // With scalar == (double), solveScalar == (double)
100 
101 namespace Foam
102 {
103  constexpr scalar GREAT = doubleScalarGREAT;
104  constexpr scalar VGREAT = doubleScalarVGREAT;
105  constexpr scalar ROOTVGREAT = doubleScalarROOTVGREAT;
106  constexpr scalar SMALL = doubleScalarSMALL;
107  constexpr scalar ROOTSMALL = doubleScalarROOTSMALL;
108  constexpr scalar VSMALL = doubleScalarVSMALL;
109  constexpr scalar ROOTVSMALL = doubleScalarROOTVSMALL;
110 
111  inline scalar readScalar(const char* buf)
112  {
113  return readDouble(buf);
114  }
115 
116  inline bool readScalar(const char* buf, scalar& val)
117  {
118  return readDouble(buf, val);
119  }
120 
121  inline scalar readScalar(const std::string& str)
122  {
123  return readDouble(str);
124  }
125 
126  inline bool readScalar(const std::string& str, scalar& val)
127  {
128  return readDouble(str, val);
129  }
130 
131 
132  //- Read scalar from stream.
133  scalar readScalar(Istream& is);
134 
135  //- Read raw scalar from binary stream.
136  // \note No internal check for binary vs ascii,
137  // the caller knows what they are doing
138  scalar readRawScalar(Istream& is);
139 
140  //- Read raw scalar(s) from binary stream.
141  // \note No internal check for binary vs ascii,
142  // the caller knows what they are doing
143  void readRawScalar(Istream& is, scalar* data, size_t nElem = 1);
144 }
145 
146 #else
147 // #error "PRECISION must be set to WM_SP, WM_SPDP or WM_DP"
148 #endif
149 
150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151 
152 // Type conversions (narrowing)
153 
154 namespace Foam
155 {
156 
157 //- Type narrowing from double to float
158 // Overflow: silently fix, or raise error?
159 inline float narrowFloat(const double val)
160 {
161  // Single statement - future constexpr?
162  return
163  (
166  : (val > -floatScalarVSMALL && val < floatScalarVSMALL) // underflow
167  ? 0
168  : static_cast<float>(val)
169  );
170 }
171 
172 } // End namespace Foam
173 
174 
175 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
176 // Additional transcendental functions and specialisations
177 
178 namespace Foam
179 {
180  //- Inverse normalized incomplete gamma function
181  scalar invIncGamma(const scalar a, const scalar P);
182 
183  //- Normalized upper incomplete gamma function
184  scalar incGammaRatio_Q(const scalar a, const scalar x);
185 
186  //- Normalized lower incomplete gamma function
187  scalar incGammaRatio_P(const scalar a, const scalar x);
188 
189  //- Upper incomplete gamma function
190  scalar incGamma_Q(const scalar a, const scalar x);
191 
192  //- Lower incomplete gamma function
193  scalar incGamma_P(const scalar a, const scalar x);
194 
195  //- Type to use for extended precision
196  template<>
197  class typeOfSolve<scalar>
198  {
199  public:
200 
201  typedef solveScalar type;
202  };
203 }
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 #endif
208 
209 // ************************************************************************* //
Foam::doubleScalarROOTVGREAT
constexpr doubleScalar doubleScalarROOTVGREAT
Definition: doubleScalar.H:59
Foam::incGamma_Q
scalar incGamma_Q(const scalar a, const scalar x)
Upper incomplete gamma function.
Definition: incGamma.C:443
Foam::doubleScalarROOTSMALL
constexpr doubleScalar doubleScalarROOTSMALL
Definition: doubleScalar.H:61
Foam::typeOfSolve
The extended precision type (solveScalar for float)
Definition: products.H:78
Foam::doubleScalarSMALL
constexpr doubleScalar doubleScalarSMALL
Definition: doubleScalar.H:60
Foam::invIncGamma
scalar invIncGamma(const scalar a, const scalar P)
Inverse normalized incomplete gamma function.
Definition: invIncGamma.C:108
Foam::typeOfSolve< scalar >::type
solveScalar type
Definition: scalar.H:201
Foam::incGammaRatio_Q
scalar incGammaRatio_Q(const scalar a, const scalar x)
Normalized upper incomplete gamma function.
Definition: incGamma.C:223
Foam::floatScalarVSMALL
constexpr floatScalar floatScalarVSMALL
Definition: floatScalar.H:62
doubleScalar.H
Foam::doubleScalarVGREAT
constexpr doubleScalar doubleScalarVGREAT
Definition: doubleScalar.H:58
Foam::floatScalarGREAT
constexpr floatScalar floatScalarGREAT
Definition: floatScalar.H:57
Foam::floatScalarROOTSMALL
constexpr floatScalar floatScalarROOTSMALL
Definition: floatScalar.H:61
Foam::incGamma_P
scalar incGamma_P(const scalar a, const scalar x)
Lower incomplete gamma function.
Definition: incGamma.C:449
Foam::narrowFloat
float narrowFloat(const double val)
Type narrowing from double to float.
Definition: scalar.H:159
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::doubleScalarVSMALL
constexpr doubleScalar doubleScalarVSMALL
Definition: doubleScalar.H:62
Foam::incGammaRatio_P
scalar incGammaRatio_P(const scalar a, const scalar x)
Normalized lower incomplete gamma function.
Definition: incGamma.C:437
floatScalar.H
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::floatScalarROOTVGREAT
constexpr floatScalar floatScalarROOTVGREAT
Definition: floatScalar.H:59
Foam::doubleScalarROOTVSMALL
constexpr doubleScalar doubleScalarROOTVSMALL
Definition: doubleScalar.H:63
Foam::floatScalarROOTVSMALL
constexpr floatScalar floatScalarROOTVSMALL
Definition: floatScalar.H:63
Foam::floatScalarSMALL
constexpr floatScalar floatScalarSMALL
Definition: floatScalar.H:60
Foam::floatScalarVGREAT
constexpr floatScalar floatScalarVGREAT
Definition: floatScalar.H:58
Foam::doubleScalarGREAT
constexpr doubleScalar doubleScalarGREAT
Definition: doubleScalar.H:57