Fix transcendetal functions on util::fixed_point
This commit is contained in:
parent
ce829f7356
commit
68ba3698f8
1 changed files with 11 additions and 8 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue