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