diff --git a/libs/util/include/psemek/util/fixed_point.hpp b/libs/util/include/psemek/util/fixed_point.hpp index 115590b4..20295b43 100644 --- a/libs/util/include/psemek/util/fixed_point.hpp +++ b/libs/util/include/psemek/util/fixed_point.hpp @@ -113,6 +113,13 @@ namespace psemek::util return static_cast(x); } + template + auto apply_via_floating_point(Function && function, FixedPoint1 const & x1, FixedPoint const & ... xs) + { + static_assert((std::is_same_v && ...)); + return static_cast(std::forward(function)(static_cast(x1), static_cast(xs) ...)); + } + } template @@ -353,29 +360,25 @@ namespace psemek::util template fixed_point sin(fixed_point x) { - using FP = fixed_point; - return static_cast(std::round(std::sin(static_cast(x)))); + return detail::apply_via_floating_point(&std::sin, x); } template fixed_point cos(fixed_point x) { - using FP = fixed_point; - return static_cast(std::round(std::cos(static_cast(x)))); + return detail::apply_via_floating_point(&std::cos, x); } template fixed_point exp(fixed_point x) { - using FP = fixed_point; - return static_cast(std::round(std::exp(static_cast(x)))); + return detail::apply_via_floating_point(&std::exp, x); } template fixed_point log(fixed_point x) { - using FP = fixed_point; - return static_cast(std::round(std::log(static_cast(x)))); + return detail::apply_via_floating_point(&std::log, x); } }