pRC
multi-purpose Tensor Train library for C++
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{
31 template<std::int64_t D = 0, class T, If<IsFloat<T>> = 0>
32 static inline constexpr auto round(T const &a)
33 {
34 if constexpr(D >= 0)
35 {
36 constexpr T scale = iPow(Size(10), Size(D));
37 return Float(std::round((a * scale)())) / scale;
38 }
39 else
40 {
41 constexpr T scale = iPow(Size(10), Size(-D));
42 auto aSign = sign(a);
43 auto aAbs = abs(a);
44 auto rem = fmod(aAbs, scale);
45 if(pRC::unit<T>(2) * rem >= scale)
46 {
47 return aSign * (aAbs - rem + scale);
48 }
49 else
50 {
51 return aSign * (aAbs - rem);
52 }
53 }
54 }
55
75 template<std::int64_t D = 0, class T, If<IsInteger<T>> = 0>
76 static inline constexpr auto round(T const &a)
77 {
78 if constexpr(D >= 0)
79 {
80 return a;
81 }
82 else
83 {
84 constexpr T scale = iPow(Size(10), Size(-D));
85 auto aSign = sign(a);
86 auto aAbs = abs(a);
87 auto rem = aAbs % scale;
88 if(pRC::unit<T>(2) * rem >= scale)
89 {
90 return aSign * (aAbs - rem + scale);
91 }
92 else
93 {
94 return aSign * (aAbs - rem);
95 }
96 }
97 }
98}
99#endif // pRC_CORE_VALUE_FUNCTIONS_ROUND_H
Top-level class storing a floating point number.
Definition float.hpp:35
Definition cholesky.hpp:18
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 Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13
static constexpr auto round(Complex< T > const &a)
Definition round.hpp:12
static constexpr auto fmod(XA &&a, XB &&b)
Calculates the floating-point remainder of an element-wise integer division of two Tensors.
Definition fmod.hpp:25