| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 1 | #include <rusefi/rusefi_math.h> | |||
| 2 | ||||
| 3 | #include <cstdint> | |||
| 4 | ||||
| 5 | 1219380 | int maxI(int i1, int i2) { | ||
| 6 |
2/2✓ Branch 0 taken 1115501 times.
✓ Branch 1 taken 103879 times.
|
1219380 | return i1 > i2 ? i1 : i2; | |
| 7 | } | |||
| 8 | ||||
| 9 | 1541 | int minI(int i1, int i2) { | ||
| 10 |
2/2✓ Branch 0 taken 570 times.
✓ Branch 1 taken 971 times.
|
1541 | return i1 < i2 ? i1 : i2; | |
| 11 | } | |||
| 12 | ||||
| 13 | 413582 | float maxF(float i1, float i2) { | ||
| 14 |
2/2✓ Branch 0 taken 155765 times.
✓ Branch 1 taken 257817 times.
|
413582 | return i1 > i2 ? i1 : i2; | |
| 15 | } | |||
| 16 | ||||
| 17 | 944652 | float minF(float i1, float i2) { | ||
| 18 |
2/2✓ Branch 0 taken 372431 times.
✓ Branch 1 taken 572221 times.
|
944652 | return i1 < i2 ? i1 : i2; | |
| 19 | } | |||
| 20 | ||||
| 21 | 293574 | float clampF(float min, float clamp, float max) { | ||
| 22 | 293574 | return maxF(min, minF(clamp, max)); | ||
| 23 | } | |||
| 24 | ||||
| 25 | 474366 | bool isSameF(float a, float b) { | ||
| 26 | 474366 | return absF(a - b) < 0.0001; | ||
| 27 | } | |||
| 28 | ||||
| 29 | 41 | constexpr float expf_taylor_impl(float x, uint8_t n) | ||
| 30 | { | |||
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 41 times.
|
41 | if (x < -2) |
| 32 | { | |||
| 33 | ✗ | return 0.818f; | ||
| 34 | } | |||
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 41 times.
|
41 | else if (x > 0) |
| 36 | { | |||
| 37 | ✗ | return 1; | ||
| 38 | } | |||
| 39 | ||||
| 40 | 41 | x = x + 1; | ||
| 41 | ||||
| 42 | 41 | float x_power = x; | ||
| 43 | 41 | int fac = 1; | ||
| 44 | 41 | float sum = 1; | ||
| 45 | ||||
| 46 |
2/2✓ Branch 0 taken 164 times.
✓ Branch 1 taken 41 times.
|
2/2✓ Decision 'true' taken 164 times.
✓ Decision 'false' taken 41 times.
|
205 | for (int i = 1; i <= n; i++) |
| 47 | { | |||
| 48 | 164 | fac *= i; | ||
| 49 | 164 | sum += x_power / fac; | ||
| 50 | ||||
| 51 | 164 | x_power *= x; | ||
| 52 | } | |||
| 53 | ||||
| 54 | 41 | constexpr const float constant_e = 2.71828f; | ||
| 55 | 41 | return sum / constant_e; | ||
| 56 | } | |||
| 57 | ||||
| 58 | 41 | float expf_taylor(float x) | ||
| 59 | { | |||
| 60 | 41 | return expf_taylor_impl(x, 4); | ||
| 61 | } | |||
| 62 | ||||
| 63 | 617 | float tanf_taylor(float x) { | ||
| 64 | // This exists because the "normal" implementation, tanf, pulls in like 6kb of | |||
| 65 | // code and loookup tables | |||
| 66 | ||||
| 67 | // This is only specified from [0, pi/2 - 0.01) | |||
| 68 | // Inside that range it has an error of less than 0.1%, and it gets worse as theta -> pi/2 | |||
| 69 | ||||
| 70 | // Precompute some exponents of x | |||
| 71 | 617 | float x2 = x * x; | ||
| 72 | 617 | float x3 = x2 * x; | ||
| 73 | 617 | float x4 = x3 * x; | ||
| 74 | 617 | float x5 = x4 * x; | ||
| 75 | 617 | float x6 = x5 * x; | ||
| 76 | // x7 not used | |||
| 77 | 617 | float x8 = x6 * x2; | ||
| 78 | ||||
| 79 | // 3-term Taylor Series for sin(theta) | |||
| 80 | 617 | float sin_val = x - (x3 / 6) + (x5 / 120); | ||
| 81 | ||||
| 82 | // 5-term Taylor Series for cos(theta) | |||
| 83 | 617 | float cos_val = 1 - (x2 / 2) + (x4 / 24) - (x6 / 720) + (x8 / 40320); | ||
| 84 | ||||
| 85 | // tan = sin / cos | |||
| 86 | 617 | return sin_val / cos_val; | ||
| 87 | } | |||
| 88 | ||||
| 89 | 837 | expected<float> NewtonsMethodSolver::solve(float x0, float deltaX, size_t maxIteration) { | ||
| 90 | 837 | float Xcur, Xnext; | ||
| 91 | 837 | Xnext = x0; | ||
| 92 | ||||
| 93 | do { | |||
| 94 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 908 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 908 times.
|
908 | if (maxIteration-- == 0) { |
| 95 | ✗ | return unexpected; | ||
| 96 | } | |||
| 97 | ||||
| 98 | 908 | Xcur = Xnext; | ||
| 99 |
2/2✓ Branch 1 taken 908 times.
✓ Branch 4 taken 908 times.
|
908 | Xnext = Xcur - fx(Xcur) / dfx(Xcur); | |
| 100 |
2/2✓ Branch 1 taken 71 times.
✓ Branch 2 taken 837 times.
|
2/2✓ Decision 'true' taken 71 times.
✓ Decision 'false' taken 837 times.
|
908 | } while (absF(Xnext - Xcur) > deltaX); |
| 101 | ||||
| 102 | 837 | return Xnext; | ||
| 103 | } | |||
| 104 |