Elements  5.8
A C++ base framework for the Euclid Software.
Number.h
Go to the documentation of this file.
1 
26 #ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_H_
27 #define ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_H_
28 
29 #include <type_traits> // for is_floating_point, is_integral
30 #include <cmath> // for round
31 
32 #include "ElementsKernel/Export.h" // ELEMENTS_API
33 
34 namespace Elements {
35 
46 template<typename TargetType, typename SourceType>
47 ELEMENTS_API TargetType numberCast(const SourceType& s) {
48 
50  using std::is_integral;
51  using std::round;
52 
53  TargetType t = static_cast<TargetType>(s);
54 
55  if (is_floating_point<SourceType>::value && is_integral<TargetType>::value) {
56  t = static_cast<TargetType>(round(s));
57  }
58 
59  return t;
60 }
61 
62 } // namespace Elements
63 
64 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_H_
65 
Export.h
defines the macros to be used for explicit export of the symbols
ELEMENTS_API
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
Definition: Export.h:74
std::is_integral
std::round
T round(T... args)
std::is_floating_point
Elements::Units::s
constexpr double s
Definition: SystemOfUnits.h:121
Elements::numberCast
ELEMENTS_API TargetType numberCast(const SourceType &s)
this function is a number cast. It behaves exactly as a static_cast except when casting from a floati...
Definition: Number.h:47
Elements
Definition: Auxiliary.h:43