2021-04-14 16:49:47 +02:00
|
|
|
/*
|
|
|
|
* Contains some trigonometric functions.
|
|
|
|
* The ANSI C equivalents are likely faster, but using any sin/cos/tan function incurs a memory penalty of 460 bytes on ESP8266, likely for lookup tables.
|
|
|
|
* This implementation has no extra static memory usage.
|
|
|
|
*
|
|
|
|
* Source of the cos_t() function: https://web.eecs.utk.edu/~azh/blog/cosine.html (cos_taylor_literal_6terms)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Arduino.h> //PI constant
|
|
|
|
|
2021-05-23 18:49:23 +02:00
|
|
|
//#define WLED_DEBUG_MATH
|
|
|
|
|
2021-04-14 16:49:47 +02:00
|
|
|
#define modd(x, y) ((x) - (int)((x) / (y)) * (y))
|
|
|
|
|
2021-09-23 20:45:53 +02:00
|
|
|
float cos_t(float phi)
|
2021-04-14 16:49:47 +02:00
|
|
|
{
|
2021-09-23 20:45:53 +02:00
|
|
|
float x = modd(phi, TWO_PI);
|
2022-03-19 19:27:32 +01:00
|
|
|
if (x < 0) x = -1 * x;
|
2021-09-23 20:45:53 +02:00
|
|
|
int8_t sign = 1;
|
2021-04-14 16:49:47 +02:00
|
|
|
if (x > PI)
|
|
|
|
{
|
|
|
|
x -= PI;
|
|
|
|
sign = -1;
|
|
|
|
}
|
2021-04-16 20:07:54 +02:00
|
|
|
float xx = x * x;
|
2021-04-14 16:49:47 +02:00
|
|
|
|
2021-05-23 18:49:23 +02:00
|
|
|
float res = sign * (1 - ((xx) / (2)) + ((xx * xx) / (24)) - ((xx * xx * xx) / (720)) + ((xx * xx * xx * xx) / (40320)) - ((xx * xx * xx * xx * xx) / (3628800)) + ((xx * xx * xx * xx * xx * xx) / (479001600)));
|
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("cos: %f,%f,%f,(%f)\n",phi,res,cos(x),res-cos(x));
|
2021-05-23 18:49:23 +02:00
|
|
|
#endif
|
|
|
|
return res;
|
2021-04-14 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 20:07:54 +02:00
|
|
|
float sin_t(float x) {
|
2021-05-23 18:49:23 +02:00
|
|
|
float res = cos_t(HALF_PI - x);
|
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("sin: %f,%f,%f,(%f)\n",x,res,sin(x),res-sin(x));
|
2021-05-23 18:49:23 +02:00
|
|
|
#endif
|
|
|
|
return res;
|
2021-04-14 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 20:07:54 +02:00
|
|
|
float tan_t(float x) {
|
|
|
|
float c = cos_t(x);
|
2021-04-14 16:49:47 +02:00
|
|
|
if (c==0.0) return 0;
|
2021-05-23 18:49:23 +02:00
|
|
|
float res = sin_t(x) / c;
|
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("tan: %f,%f,%f,(%f)\n",x,res,tan(x),res-tan(x));
|
2021-05-23 18:49:23 +02:00
|
|
|
#endif
|
|
|
|
return res;
|
2021-04-14 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//https://stackoverflow.com/questions/3380628
|
|
|
|
// Absolute error <= 6.7e-5
|
|
|
|
float acos_t(float x) {
|
|
|
|
float negate = float(x < 0);
|
2021-05-23 18:49:23 +02:00
|
|
|
float xabs = std::abs(x);
|
2021-04-14 16:49:47 +02:00
|
|
|
float ret = -0.0187293;
|
2021-05-23 18:49:23 +02:00
|
|
|
ret = ret * xabs;
|
2021-04-14 16:49:47 +02:00
|
|
|
ret = ret + 0.0742610;
|
2021-05-23 18:49:23 +02:00
|
|
|
ret = ret * xabs;
|
2021-04-14 16:49:47 +02:00
|
|
|
ret = ret - 0.2121144;
|
2021-05-23 18:49:23 +02:00
|
|
|
ret = ret * xabs;
|
2021-04-14 16:49:47 +02:00
|
|
|
ret = ret + HALF_PI;
|
2021-05-23 18:49:23 +02:00
|
|
|
ret = ret * sqrt(1.0-xabs);
|
2021-04-14 16:49:47 +02:00
|
|
|
ret = ret - 2 * negate * ret;
|
2021-05-23 18:49:23 +02:00
|
|
|
float res = negate * PI + ret;
|
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("acos: %f,%f,%f,(%f)\n",x,res,acos(x),res-acos(x));
|
2021-05-23 18:49:23 +02:00
|
|
|
#endif
|
|
|
|
return res;
|
2021-04-14 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
float asin_t(float x) {
|
2021-05-23 18:49:23 +02:00
|
|
|
float res = HALF_PI - acos_t(x);
|
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("asin: %f,%f,%f,(%f)\n",x,res,asin(x),res-asin(x));
|
2021-05-23 18:49:23 +02:00
|
|
|
#endif
|
|
|
|
return res;
|
2021-04-14 16:49:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 09:46:54 +01:00
|
|
|
// declare a template with no implementation, and only one specialization
|
|
|
|
// this allows hiding the constants, while ensuring ODR causes optimizations
|
|
|
|
// to still apply. (Fixes issues with conflicting 3rd party #define's)
|
|
|
|
template <typename T> T atan_t(T x);
|
|
|
|
template<>
|
2021-05-23 18:22:26 +02:00
|
|
|
float atan_t(float x) {
|
2022-02-09 09:46:54 +01:00
|
|
|
//For A/B/C, see https://stackoverflow.com/a/42542593
|
|
|
|
static const double A { 0.0776509570923569 };
|
|
|
|
static const double B { -0.287434475393028 };
|
|
|
|
static const double C { ((HALF_PI/2) - A - B) };
|
|
|
|
// polynominal factors for approximation between 1 and 5
|
|
|
|
static const float C0 { 0.089494f };
|
|
|
|
static const float C1 { 0.974207f };
|
|
|
|
static const float C2 { -0.326175f };
|
|
|
|
static const float C3 { 0.05375f };
|
|
|
|
static const float C4 { -0.003445f };
|
|
|
|
|
2021-05-23 18:22:26 +02:00
|
|
|
#ifdef WLED_DEBUG_MATH
|
|
|
|
float xinput = x;
|
|
|
|
#endif
|
2022-02-09 09:46:54 +01:00
|
|
|
bool neg = (x < 0);
|
2021-05-23 18:22:26 +02:00
|
|
|
x = std::abs(x);
|
|
|
|
float res;
|
2022-02-09 09:46:54 +01:00
|
|
|
if (x > 5.0f) { // atan(x) converges to pi/2 - (1/x) for large values
|
2021-05-23 18:22:26 +02:00
|
|
|
res = HALF_PI - (1.0f/x);
|
2022-02-09 09:46:54 +01:00
|
|
|
} else if (x > 1.0f) { //1 < x < 5
|
2021-05-23 18:22:26 +02:00
|
|
|
float xx = x * x;
|
|
|
|
res = (C4*xx*xx)+(C3*xx*x)+(C2*xx)+(C1*x)+C0;
|
2022-02-09 09:46:54 +01:00
|
|
|
} else { // this approximation is only for x <= 1
|
2021-05-23 18:22:26 +02:00
|
|
|
float xx = x * x;
|
|
|
|
res = ((A*xx + B)*xx + C)*x;
|
|
|
|
}
|
2022-02-09 09:46:54 +01:00
|
|
|
if (neg) {
|
|
|
|
res = -res;
|
|
|
|
}
|
2021-05-23 18:22:26 +02:00
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("atan: %f,%f,%f,(%f)\n",xinput,res,atan(xinput),res-atan(xinput));
|
2021-05-23 18:22:26 +02:00
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
2021-04-14 16:49:47 +02:00
|
|
|
|
2021-04-16 20:07:54 +02:00
|
|
|
float floor_t(float x) {
|
|
|
|
bool neg = x < 0;
|
|
|
|
int val = x;
|
|
|
|
if (neg) val--;
|
2021-05-23 18:49:23 +02:00
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("floor: %f,%f,%f\n",x,(float)val,floor(x));
|
2021-05-23 18:49:23 +02:00
|
|
|
#endif
|
2021-04-16 20:07:54 +02:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
float fmod_t(float num, float denom) {
|
|
|
|
int tquot = num / denom;
|
2021-05-23 18:49:23 +02:00
|
|
|
float res = num - tquot * denom;
|
|
|
|
#ifdef WLED_DEBUG_MATH
|
2021-09-23 20:45:53 +02:00
|
|
|
Serial.printf("fmod: %f,%f,(%f)\n",res,fmod(num,denom),res-fmod(num,denom));
|
2021-05-23 18:49:23 +02:00
|
|
|
#endif
|
|
|
|
return res;
|
2021-04-16 20:07:54 +02:00
|
|
|
}
|