Add trunc() and fract() for util::fixed_point
This commit is contained in:
parent
d42639edba
commit
abec5474f0
1 changed files with 19 additions and 0 deletions
|
|
@ -363,6 +363,25 @@ namespace psemek::util
|
|||
return FP::from_rep(((x.rep() - static_cast<rep_type>(1)) & ~fractional_mask) + fixed_point_one);
|
||||
}
|
||||
|
||||
template <unsigned int I, unsigned int F, bool S>
|
||||
constexpr fixed_point<I, F, S> trunc(fixed_point<I, F, S> x)
|
||||
{
|
||||
if (S && x < fixed_point<I, F, S>{0})
|
||||
{
|
||||
return ceil(x);
|
||||
}
|
||||
else
|
||||
{
|
||||
return floor(x);
|
||||
}
|
||||
}
|
||||
|
||||
template <unsigned int I, unsigned int F, bool S>
|
||||
constexpr fixed_point<I, F, S> fract(fixed_point<I, F, S> x)
|
||||
{
|
||||
return x - trunc(x);
|
||||
}
|
||||
|
||||
template <unsigned int I, unsigned int F, bool S>
|
||||
constexpr fixed_point<I, F, S> round(fixed_point<I, F, S> x)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue