FastLED 3.9.15
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/has_include.h"
4
5// Include math headers with better ESP32C2 compatibility
6#ifndef FASTLED_HAS_EXP
7#if FL_HAS_INCLUDE(<cmath>)
8 #define FASTLED_HAS_EXP 1
9 #include <cmath> // ok include
10#elif FL_HAS_INCLUDE(<math.h>)
11 #define FASTLED_HAS_EXP 1
12 #include <math.h> // ok include
13#else
14 #define FASTLED_HAS_EXP 0
15#endif
16#endif // !FASTLED_HAS_EXP
17
18
19#include "fl/clamp.h"
20#include "fl/map_range.h"
21#include "fl/math_macros.h"
22
23namespace fl {
24
25template <typename T> inline T floor(T value) {
26 if (value >= 0) {
27 return static_cast<T>(static_cast<int>(value));
28 }
29 return static_cast<T>(::floor(static_cast<float>(value)));
30}
31
32template <typename T> inline T ceil(T value) {
33 if (value <= 0) {
34 return static_cast<T>(static_cast<int>(value));
35 }
36 return static_cast<T>(::ceil(static_cast<float>(value)));
37}
38
39// Exponential function - binds to standard library exp if available
40template <typename T> inline T exp(T value) {
41#if FASTLED_HAS_EXP
42 return static_cast<T>(::exp(static_cast<double>(value)));
43#else
44 // Fallback implementation using Taylor series approximation
45 // e^x ≈ 1 + x + x²/2! + x³/3! + x⁴/4! + x⁵/5! + ...
46 // This is a simple approximation for small values
47 double x = static_cast<double>(value);
48 if (x > 10.0)
49 return static_cast<T>(22026.465794806718); // e^10 approx
50 if (x < -10.0)
51 return static_cast<T>(0.0000453999297625); // e^-10 approx
52
53 double result = 1.0;
54 double term = 1.0;
55 for (int i = 1; i < 10; ++i) {
56 term *= x / i;
57 result += term;
58 }
59 return static_cast<T>(result);
60#endif
61}
62
63// Constexpr version for compile-time evaluation (compatible with older C++
64// standards)
65constexpr int ceil_constexpr(float value) {
66 return static_cast<int>((value > static_cast<float>(static_cast<int>(value)))
67 ? static_cast<int>(value) + 1
68 : static_cast<int>(value));
69}
70
71// Arduino will define this in the global namespace as macros, so we can't
72// define them ourselves.
73// template <typename T>
74// inline T abs(T value) {
75// return (value < 0) ? -value : value;
76// }
77
78// template <typename T>
79// inline T min(T a, T b) {
80// return (a < b) ? a : b;
81// }
82
83// template <typename T>
84// inline T max(T a, T b) {
85// return (a > b) ? a : b;
86// }
87
88} // namespace fl
int x
Definition simple.h:92
Result type for promise operations.
constexpr int ceil_constexpr(float value)
Definition math.h:65
T ceil(T value)
Definition math.h:32
T exp(T value)
Definition math.h:40
T floor(T value)
Definition math.h:25
IMPORTANT!
Definition crgb.h:20