cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
round.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_VALUE_FUNCTIONS_ROUND_H
4#define pRC_CORE_VALUE_FUNCTIONS_ROUND_H
5
6#include <cmath>
7
12
13namespace pRC
14{
15 template<std::int64_t D = 0, class T, If<IsFloat<T>> = 0>
16 static inline constexpr auto round(T const &a)
17 {
18 if constexpr(D >= 0)
19 {
20 constexpr T scale = iPow(Size(10), Size(D));
21 return Float(std::round((a * scale)())) / scale;
22 }
23 else
24 {
25 constexpr T scale = iPow(Size(10), Size(-D));
26 auto aSign = sign(a);
27 auto aAbs = abs(a);
28 auto rem = fmod(aAbs, scale);
29 if(pRC::unit<T>(2) * rem >= scale)
30 {
31 return aSign * (aAbs - rem + scale);
32 }
33 else
34 {
35 return aSign * (aAbs - rem);
36 }
37 }
38 }
39
40 template<std::int64_t D = 0, class T, If<IsInteger<T>> = 0>
41 static inline constexpr auto round(T const &a)
42 {
43 if constexpr(D >= 0)
44 {
45 return a;
46 }
47 else
48 {
49 constexpr T scale = iPow(Size(10), Size(-D));
50 auto aSign = sign(a);
51 auto aAbs = abs(a);
52 auto rem = aAbs % scale;
53 if(pRC::unit<T>(2) * rem >= scale)
54 {
55 return aSign * (aAbs - rem + scale);
56 }
57 else
58 {
59 return aSign * (aAbs - rem);
60 }
61 }
62 }
63}
64#endif // pRC_CORE_VALUE_FUNCTIONS_ROUND_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Definition cholesky.hpp:18
Float(Float< 16 >::Fundamental const) -> Float< 16 >
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
std::size_t Size
Definition type_traits.hpp:20
static constexpr auto sign(Complex< T > const &a)
Definition sign.hpp:13
static constexpr B iPow(B const base, N const exp)
Definition ipow.hpp:12
static constexpr auto abs(Complex< T > const &a)
Definition abs.hpp:12
static constexpr auto round(Complex< T > const &a)
Definition round.hpp:12
static constexpr auto fmod(XA &&a, XB &&b)
Definition fmod.hpp:15