LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
7#ifndef _MATH_UTILS_HPP_
8#define _MATH_UTILS_HPP_
9
10#include <cmath>
11#include <iostream>
12
13namespace squiggles {
20template <class T> inline int sgn(T v) {
21 return (v > T(0)) - (v < T(0));
22}
23
24inline bool
25nearly_equal(const double& a, const double& b, double epsilon = 1e-5) {
26 return std::fabs(a - b) < epsilon;
27}
28} // namespace squiggles
29
30namespace std {
31// Copied from https://github.com/emsr/cxx_linear
32template <typename _Float>
33constexpr std::enable_if_t<
34 std::is_floating_point_v<_Float> &&
35 __cplusplus <= 201703L, // Only defines this function if C++ standard < 20
36 _Float>
37lerp(_Float __a, _Float __b, _Float __t) {
38 if (std::isnan(__a) || std::isnan(__b) || std::isnan(__t))
39 return std::numeric_limits<_Float>::quiet_NaN();
40 else if ((__a <= _Float{0} && __b >= _Float{0}) ||
41 (__a >= _Float{0} && __b <= _Float{0}))
42 // ab <= 0 but product could overflow.
43#ifndef FMA
44 return __t * __b + (_Float{1} - __t) * __a;
45#else
46 return std::fma(__t, __b, (_Float{1} - __t) * __a);
47#endif
48 else if (__t == _Float{1})
49 return __b;
50 else { // monotonic near t == 1.
51#ifndef FMA
52 const auto __x = __a + __t * (__b - __a);
53#else
54 const auto __x = std::fma(__t, __b - __a, __a);
55#endif
56 return (__t > _Float{1}) == (__b > __a) ? std::max(__b, __x)
57 : std::min(__b, __x);
58 }
59}
60} // namespace std
61#endif
Copyright 2020 Jonathan Bayless.
int sgn(T v)
Returns the sign value of the given value.
Definition utils.hpp:20
bool nearly_equal(const double &a, const double &b, double epsilon=1e-5)
Definition utils.hpp:25
STL namespace.