21 #ifndef PLAYRHO_COMMON_FIXEDMATH_HPP
22 #define PLAYRHO_COMMON_FIXEDMATH_HPP
47 template <
typename BT,
unsigned int FB,
int N = 5>
50 return arg >= 0? arg: -arg;
58 template <
typename BT,
unsigned int FB>
94 const auto doReciprocal = (n < 0);
106 return (doReciprocal)? 1 / res: res;
112 template <
typename BT,
unsigned int FB>
120 for (--n; n > 1; --n)
133 template <
typename BT,
unsigned int FB,
int N = 6>
136 const auto doReciprocal = (arg < 0);
151 for (
auto i = 2; i < N; ++i)
158 return doReciprocal? 1 / res: res;
165 template <
typename BT,
unsigned int FB,
int N = 6>
168 if (arg.
isnan() || (arg < 0))
192 for (
auto i = 2; i < N; ++i)
195 res += sign * pt / i;
204 arg = (arg - 1) / arg;
207 for (
auto i = 2; i < N; ++i)
217 template <
typename BT,
unsigned int FB,
int N = 5>
230 constexpr
const auto last = 2 * N + 1;
233 for (
auto i = 3; i <= last; i += 2)
237 const auto term = pt / ft;
246 template <
typename BT,
unsigned int FB,
int N = 5>
257 constexpr
const auto last = 2 * N;
260 for (
auto i = 2; i <= last; i += 2)
264 const auto term = pt / ft;
274 template <
typename BT,
unsigned int FB,
int N = 5>
279 const auto doReciprocal = (
abs(arg) > 1);
290 const auto last = 2 * N + 1;
292 for (
auto i = 3; i <= last; i += 2)
295 const auto term = pt / i;
302 return (arg > 0)? FixedPi<BT, FB> / 2 - res: -FixedPi<BT, FB> / 2 - res;
309 template <
typename BT,
unsigned int FB>
313 auto tempSquared =
Square(temp);
314 const auto greaterThanOne = arg > 1;
317 while (arg != tempSquared)
319 const auto mid = (lower + upper) / 2;
325 tempSquared =
Square(temp);
326 if (tempSquared > arg)
330 else if (tempSquared < arg)
342 template <
typename BT,
unsigned int FB>
345 return static_cast<Fixed<BT, FB>>(
static_cast<long long>(arg));
350 template <
typename BT,
unsigned int FB>
366 template <
typename BT,
unsigned int FB>
369 const auto quotient = dividend / divisor;
370 const auto integer =
trunc(quotient);
371 const auto remainder = quotient - integer;
372 return remainder * divisor;
383 template <
typename BT,
unsigned int FB>
400 template <
typename BT,
unsigned int FB>
409 template <
typename BT,
unsigned int FB>
412 constexpr
const auto oneRotationInRadians = 2 * FixedPi<BT, FB>;
414 angleInRadians =
fmod(angleInRadians, oneRotationInRadians);
415 if (angleInRadians > FixedPi<BT, FB>)
418 angleInRadians -= oneRotationInRadians;
420 else if (angleInRadians < -FixedPi<BT, FB>)
423 angleInRadians += oneRotationInRadians;
425 return angleInRadians;
432 template <
typename BT,
unsigned int FB>
436 return detail::sin<BT, FB, 5>(arg);
441 template <
typename BT,
unsigned int FB>
445 return detail::cos<BT, FB, 5>(arg);
451 template <
typename BT,
unsigned int FB>
454 if (arg.
isnan() || (arg == 0))
460 return detail::FixedPi<BT, FB> / 2;
464 return -detail::FixedPi<BT, FB> / 2;
466 return detail::atan<BT, FB, 5>(arg);
472 template <
typename BT,
unsigned int FB>
483 return atan(y / x) + ((y >= 0)? +1: -1) * detail::FixedPi<BT, FB>;
487 return +detail::FixedPi<BT, FB> / 2;
491 return -detail::FixedPi<BT, FB> / 2;
498 template <
typename BT,
unsigned int FB>
501 return (arg < 8)? detail::log<BT, FB, 36>(arg): detail::log<BT, FB, 96>(arg);
506 template <
typename BT,
unsigned int FB>
509 return (arg <= 2)? detail::exp<BT, FB, 6>(arg): detail::exp<BT, FB, 24>(arg);
514 template <
typename BT,
unsigned int FB>
519 const auto intExp =
static_cast<int>(exponent);
520 if (intExp == exponent)
523 return pow(base, intExp);
532 const auto lnResult =
log(base);
533 const auto expResult =
exp(lnResult * exponent);
539 template <
typename BT,
unsigned int FB>
542 return sqrt(x * x + y * y);
547 template <
typename BT,
unsigned int FB>
557 template <
typename BT,
unsigned int FB>
560 return value.getsign() < 0;
565 template <
typename BT,
unsigned int FB>
573 template <
typename BT,
unsigned int FB>
577 && (value < Fixed<BT, FB>::GetInfinity());
584 #endif // PLAYRHO_COMMON_FIXEDMATH_HPP