WLED/wled00/wled_math.h

71 lines
1.6 KiB
C
Raw Normal View History

#ifndef WLED_MATH_H
#define WLED_MATH_H
/*
* 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
#define modd(x, y) ((x) - (int)((x) / (y)) * (y))
double cos_t(double x)
{
x = modd(x, TWO_PI);
char sign = 1;
if (x > PI)
{
x -= PI;
sign = -1;
}
double xx = x * x;
return 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)));
}
double sin_t(double x) {
return cos_t(HALF_PI - x);
}
double tan_t(double x) {
double c = cos_t(x);
if (c==0.0) return 0;
return sin_t(x) / c;
}
//https://stackoverflow.com/questions/3380628
// Absolute error <= 6.7e-5
float acos_t(float x) {
float negate = float(x < 0);
x = std::abs(x);
float ret = -0.0187293;
ret = ret * x;
ret = ret + 0.0742610;
ret = ret * x;
ret = ret - 0.2121144;
ret = ret * x;
ret = ret + HALF_PI;
ret = ret * sqrt(1.0-x);
ret = ret - 2 * negate * ret;
return negate * PI + ret;
}
float asin_t(float x) {
return HALF_PI - acos_t(x);
}
//https://stackoverflow.com/a/42542593
#define A 0.0776509570923569
#define B -0.287434475393028
#define C ((HALF_PI/2) - A - B)
double atan_t(double x) {
double xx = x * x;
return ((A*xx + B)*xx + C)*x;
}
#endif