ENH: add (C++11) user-literal for degrees to radians conversion

- Programming convenience.
  Eg, cos(45_deg) vs cos(degToRad(45))

STYLE: conversions marked as constexpr and noexcept
This commit is contained in:
Mark Olesen 2017-05-31 10:16:19 +02:00
parent 0be69605fd
commit 62e7ddccdc
4 changed files with 73 additions and 6 deletions

View File

@ -0,0 +1,3 @@
Test-unitConversion.C
EXE = $(FOAM_USER_APPBIN)/Test-unitConversion

View File

@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
Test-unitConversion
\*---------------------------------------------------------------------------*/
#include "IOstreams.H"
#include "unitConversion.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "30_deg: " << 30_deg << nl;
Info<< "30.0_deg: " << 30.0_deg << nl;
Info<< "3e+1_deg: " << 3e+1_deg << nl;
Info<< "degToRad(30): " << degToRad(30) << nl;
Info<< "cos(30_deg): " << ::cos(30_deg) << nl;
return 0;
}
// ************************************************************************* //

View File

@ -42,29 +42,43 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//- Conversion from degrees to radians
inline scalar degToRad(const scalar deg)
inline constexpr scalar degToRad(const scalar deg) noexcept
{
return (deg*constant::mathematical::pi/180.0);
return (deg*Foam::constant::mathematical::pi/180.0);
}
//- Conversion from radians to degrees
inline scalar radToDeg(const scalar rad)
inline constexpr scalar radToDeg(const scalar rad) noexcept
{
return (rad*180.0/constant::mathematical::pi);
return (rad*180.0/Foam::constant::mathematical::pi);
}
//- Conversion from atm to Pa
inline scalar atmToPa(const scalar atm)
inline constexpr scalar atmToPa(const scalar atm) noexcept
{
return (atm*101325.0);
}
//- Conversion from atm to Pa
inline scalar paToAtm(const scalar pa)
inline constexpr scalar paToAtm(const scalar pa) noexcept
{
return (pa/101325.0);
}
//- User literal for degrees to radians conversion (integers)
inline constexpr scalar operator "" _deg(unsigned long long int deg) noexcept
{
return (deg*Foam::constant::mathematical::pi/180.0);
}
//- User literal for degrees to radians conversion (floats)
inline constexpr scalar operator "" _deg(long double deg) noexcept
{
return (deg*Foam::constant::mathematical::pi/180.0);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam