Line | Branch | Decision | Exec | Source |
---|---|---|---|---|
1 | // Various math utility functions, implemented in microcontroller friendly ways. | |||
2 | ||||
3 | #pragma once | |||
4 | ||||
5 | #include <rusefi/expected.h> | |||
6 | ||||
7 | #include <cstddef> | |||
8 | ||||
9 | // absolute value | |||
10 | 60043 | constexpr int absI(int value) { | ||
11 | 60043 | return value > 0 ? value : -value; | ||
12 | } | |||
13 | 1489881 | constexpr float absF(float value) { | ||
14 |
2/2✓ Branch 0 taken 1007243 times.
✓ Branch 1 taken 482638 times.
|
1489881 | return value > 0 ? value : -value; | |
15 | } | |||
16 | ||||
17 | // Min/max | |||
18 | int maxI(int i1, int i2); | |||
19 | int minI(int i1, int i2); | |||
20 | float maxF(float i1, float i2); | |||
21 | float minF(float i1, float i2); | |||
22 | ||||
23 | // Clamping | |||
24 | float clampF(float min, float clamp, float max); | |||
25 | ||||
26 | // Returns if two floats are within 0.0001 | |||
27 | bool isSameF(float a, float b); | |||
28 | ||||
29 | // @brief Compute e^x using a 4th order taylor expansion centered at x=-1. Provides | |||
30 | // bogus results outside the range -2 < x < 0. | |||
31 | float expf_taylor(float x); | |||
32 | ||||
33 | // @brief Compute tan(theta) using a ratio of the Taylor series for sin and cos | |||
34 | // Valid for the range [0, pi/2 - 0.01] | |||
35 | float tanf_taylor(float theta); | |||
36 | ||||
37 | struct NewtonsMethodSolver | |||
38 | { | |||
39 | // Solve for a value of x such that fx(x)=0 | |||
40 | // x0 is the initial guess | |||
41 | // deltaX controls when to stop - when abs((estimate N) - (estimate N-1)) < deltaX, stop calculation. | |||
42 | // Returns unexpected if failed to converge | |||
43 | expected<float> solve(float x0, float deltaX, size_t maxIteration = 20); | |||
44 | ||||
45 | // Function for which we want to find a root 0 = fx(x) | |||
46 | virtual float fx(float x) = 0; | |||
47 | ||||
48 | // First derivative of fx(x) | |||
49 | virtual float dfx(float x) = 0; | |||
50 | }; | |||
51 |