Add trunc() and fract() for util::fixed_point

This commit is contained in:
Nikita Lisitsa 2026-08-14 16:34:58 +03:00
parent d42639edba
commit abec5474f0

View file

@ -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)
{