Elements 6.3.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
Number.tpp
Go to the documentation of this file.
1
21// IWYU pragma: private, include "ElementsKernel/Number.h"
22
23#ifndef ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_IMPL_
24#error "This file should not be included directly! Use ElementsKernel/Number.h instead"
25#else
26
27#include <cmath> // for round
28#include <type_traits> // for is_floating_point, is_integral
29
30namespace Elements {
31
32template <typename TargetType, typename SourceType>
33TargetType numberCast(const SourceType& s) {
34
36 using std::is_integral;
37 using std::round;
38
39 TargetType t = static_cast<TargetType>(s);
40
41 if (is_floating_point<SourceType>::value && is_integral<TargetType>::value) {
42 t = static_cast<TargetType>(round(s));
43 }
44
45 return t;
46}
47
48} // namespace Elements
49
50#endif // ELEMENTSKERNEL_ELEMENTSKERNEL_NUMBER_IMPL_
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...
T round(T... args)