Fix transcendetal functions on util::fixed_point

This commit is contained in:
Nikita Lisitsa 2025-07-29 21:05:42 +03:00
parent ce829f7356
commit 68ba3698f8

View file

@ -113,6 +113,13 @@ namespace psemek::util
return static_cast<H>(x);
}
template <typename FloatingPoint, typename Function, typename FixedPoint1, typename ... FixedPoint>
auto apply_via_floating_point(Function && function, FixedPoint1 const & x1, FixedPoint const & ... xs)
{
static_assert((std::is_same_v<FixedPoint1, FixedPoint> && ...));
return static_cast<FixedPoint1>(std::forward<Function>(function)(static_cast<FloatingPoint>(x1), static_cast<FloatingPoint>(xs) ...));
}
}
template <unsigned int IntegerBits, unsigned int FractionalBits, bool Signed>
@ -353,29 +360,25 @@ namespace psemek::util
template <unsigned int I, unsigned int F, bool S>
fixed_point<I, F, S> sin(fixed_point<I, F, S> x)
{
using FP = fixed_point<I, F, S>;
return static_cast<FP>(std::round(std::sin(static_cast<double>(x))));
return detail::apply_via_floating_point<double>(&std::sin, x);
}
template <unsigned int I, unsigned int F, bool S>
fixed_point<I, F, S> cos(fixed_point<I, F, S> x)
{
using FP = fixed_point<I, F, S>;
return static_cast<FP>(std::round(std::cos(static_cast<double>(x))));
return detail::apply_via_floating_point<double>(&std::cos, x);
}
template <unsigned int I, unsigned int F, bool S>
fixed_point<I, F, S> exp(fixed_point<I, F, S> x)
{
using FP = fixed_point<I, F, S>;
return static_cast<FP>(std::round(std::exp(static_cast<double>(x))));
return detail::apply_via_floating_point<double>(&std::exp, x);
}
template <unsigned int I, unsigned int F, bool S>
fixed_point<I, F, S> log(fixed_point<I, F, S> x)
{
using FP = fixed_point<I, F, S>;
return static_cast<FP>(std::round(std::log(static_cast<double>(x))));
return detail::apply_via_floating_point<double>(&std::log, x);
}
}