ALFI
Advanced Library for Function Interpolation
Loading...
Searching...
No Matches
numeric.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4
5#include "../config.h"
6
8 template <typename Number = DefaultNumber>
9 bool are_equal(const Number& a, const Number& b, const Number& epsilon = std::numeric_limits<Number>::epsilon()) {
10 return std::abs(a - b) <= epsilon || std::abs(a - b) <= std::max(std::abs(a), std::abs(b)) * epsilon;
11 }
12
25 template <typename Number, typename ExponentType>
26 Number binpow(Number x, ExponentType n) {
27 if constexpr (std::is_signed_v<ExponentType>) {
28 if (n < 0) {
29 return 1 / binpow(x, -n);
30 }
31 }
32 Number result = 1;
33 while (n > 0) {
34 if ((n & 1) == 1) {
35 result *= x;
36 }
37 x *= x;
38 n >>= 1;
39 }
40 return result;
41 }
42}
Definition numeric.h:7
bool are_equal(const Number &a, const Number &b, const Number &epsilon=std::numeric_limits< Number >::epsilon())
Definition numeric.h:9
Number binpow(Number x, ExponentType n)
Computes the power of a number using binary exponentiation.
Definition numeric.h:26