GY-63_MS5611/libraries/FastTrig/FastTrig.cpp

453 lines
9.0 KiB
C++
Raw Normal View History

2022-04-15 10:43:05 -04:00
//
// FILE: FastTrig.cpp
// AUTHOR: Rob Tillaart
2023-11-02 10:13:39 -04:00
// VERSION: 0.3.3
2022-04-15 10:43:05 -04:00
// PURPOSE: Arduino library for a faster approximation of sin() and cos()
// DATE: 2011-08-18
// URL: https://github.com/RobTillaart/FastTrig
// https://forum.arduino.cc/index.php?topic=69723.0
#include "FastTrig.h"
2022-12-03 06:59:56 -05:00
// 91 x 2 bytes ==> 182 bytes
// use 65535.0 as divider
uint16_t sinTable16[] = {
2022-04-15 10:43:05 -04:00
0,
1145, 2289, 3435, 4572, 5716, 6853, 7989, 9125, 10255, 11385,
12508, 13631, 14745, 15859, 16963, 18067, 19165, 20253, 21342, 22417,
23489, 24553, 25610, 26659, 27703, 28731, 29755, 30773, 31777, 32772,
33756, 34734, 35697, 36649, 37594, 38523, 39445, 40350, 41247, 42131,
42998, 43856, 44701, 45528, 46344, 47147, 47931, 48708, 49461, 50205,
50933, 51646, 52342, 53022, 53686, 54334, 54969, 55579, 56180, 56760,
57322, 57866, 58394, 58908, 59399, 59871, 60327, 60768, 61184, 61584,
61969, 62330, 62677, 63000, 63304, 63593, 63858, 64108, 64334, 64545,
64731, 64903, 65049, 65177, 65289, 65377, 65449, 65501, 65527, 65535,
65535
};
2022-12-03 06:59:56 -05:00
/* 0.1.4 table
uint16_t sinTable16[] = {
2022-04-15 10:43:05 -04:00
0,
1145, 2289, 3435, 4571, 5715, 6852, 7988, 9125, 10254, 11385,
12508, 13630, 14745, 15859, 16963, 18067, 19165, 20253, 21342, 22416,
23488, 24553, 25610, 26659, 27699, 28730, 29754, 30773, 31777, 32771,
33755, 34734, 35697, 36649, 37594, 38523, 39445, 40350, 41247, 42127,
42998, 43856, 44697, 45527, 46344, 47146, 47931, 48708, 49461, 50205,
50933, 51645, 52341, 53022, 53686, 54333, 54969, 55578, 56180, 56759,
57322, 57866, 58394, 58908, 59399, 59871, 60327, 60767, 61184, 61584,
61969, 62330, 62677, 63000, 63304, 63592, 63857, 64108, 64333, 64544,
64731, 64902, 65049, 65177, 65289, 65376, 65449, 65501, 65527, 65535,
65535
};
*/
// use 255.0 as divider
2023-11-02 10:13:39 -04:00
uint8_t sinTable8[] = {
2022-04-15 10:43:05 -04:00
0, 4, 9, 13, 18, 22, 27, 31, 35, 40, 44,
49, 53, 57, 62, 66, 70, 75, 79, 83, 87,
91, 96, 100, 104, 108, 112, 116, 120, 124, 128,
131, 135, 139, 143, 146, 150, 153, 157, 160, 164,
167, 171, 174, 177, 180, 183, 186, 190, 192, 195,
198, 201, 204, 206, 209, 211, 214, 216, 219, 221,
223, 225, 227, 229, 231, 233, 235, 236, 238, 240,
241, 243, 244, 245, 246, 247, 248, 249, 250, 251,
252, 253, 253, 254, 254, 254, 255, 255, 255, 255,
255
};
2022-12-17 09:04:42 -05:00
///////////////////////////////////////////////////////
//
// GONIO INT EXPERIMENTAL
// works with only whole degrees.
//
int isin256(uint32_t v)
{
bool negative = false;
long whole = v;
if (whole >= 360) whole %= 360;
int y = whole; // 16 bit math is faster than 32 bit
if (y >= 180)
{
y -= 180;
negative = true;
}
if (y >= 90)
{
y = 180 - y;
}
int g = sinTable16[y] >> 8;
if (negative) return -g;
return g;
}
int icos256(uint32_t v)
{
return isin256(v + 90);
}
2022-12-22 07:44:04 -05:00
void isincos256(uint32_t v, int *si, int *co)
2022-12-17 09:04:42 -05:00
{
bool sneg = false;
bool cneg = false;
2023-11-02 10:13:39 -04:00
long whole = v;
2022-12-17 09:04:42 -05:00
2023-11-02 10:13:39 -04:00
if (whole >= 360)
2022-12-17 09:04:42 -05:00
{
whole %= 360;
}
int y = whole; // 16 bit math is faster than 32 bit
if (y >= 180)
{
y -= 180;
sneg = !sneg;
cneg = !cneg;
}
if (y >= 90)
{
y = 180 - y;
cneg = !cneg;
}
2022-12-22 07:44:04 -05:00
*si = sinTable16[y] >> 8;
*co = sinTable16[90-y] >> 8;
if (sneg) *si = - *si;
if (cneg) *co = - *co;
2022-12-17 09:04:42 -05:00
}
2022-04-15 10:43:05 -04:00
///////////////////////////////////////////////////////
//
2022-12-03 06:59:56 -05:00
// GONIO LOOKUP
2022-04-15 10:43:05 -04:00
//
float isin(float f)
{
2022-12-03 06:59:56 -05:00
bool negative = (f < 0);
if (negative)
2022-04-15 10:43:05 -04:00
{
f = -f;
2022-12-03 06:59:56 -05:00
negative = true;
2022-04-15 10:43:05 -04:00
}
2022-12-03 06:59:56 -05:00
long whole = f;
uint8_t remain = (f - whole) * 256;
2022-04-15 10:43:05 -04:00
2023-11-02 10:13:39 -04:00
if (whole >= 360)
2022-12-17 09:04:42 -05:00
{
whole %= 360;
// possible faster for 360-720
// if (whole >= 720) whole %= 360;
// else whole -= 360;
}
2022-04-15 10:43:05 -04:00
2022-12-03 06:59:56 -05:00
int y = whole; // 16 bit math is faster than 32 bit
2022-04-15 10:43:05 -04:00
if (y >= 180)
{
y -= 180;
2022-12-03 06:59:56 -05:00
negative = !negative;
2022-04-15 10:43:05 -04:00
}
if (y >= 90)
{
y = 180 - y;
2022-12-03 06:59:56 -05:00
if (remain != 0)
2022-04-15 10:43:05 -04:00
{
2022-12-17 09:04:42 -05:00
remain = 256 - remain;
2022-04-15 10:43:05 -04:00
y--;
}
}
2022-12-03 06:59:56 -05:00
// float value improves ~4% on avg error for ~60 bytes.
uint16_t value = sinTable16[y];
2023-11-02 10:13:39 -04:00
2022-12-03 06:59:56 -05:00
// interpolate if needed
2023-11-02 10:13:39 -04:00
if (remain > 0)
2022-04-15 10:43:05 -04:00
{
2022-12-03 06:59:56 -05:00
value = value + ((sinTable16[y + 1] - value) / 8 * remain) / 32; // == * remain / 256
2022-04-15 10:43:05 -04:00
}
2022-12-03 06:59:56 -05:00
float g = value * 0.0000152590219; // = / 65535.0
if (negative) return -g;
return g;
2022-04-15 10:43:05 -04:00
}
float icos(float x)
{
2022-12-03 06:59:56 -05:00
// prevent modulo math if x in 0..360
return isin(x - 270.0); // better than x + 90;
2022-04-15 10:43:05 -04:00
}
2022-12-22 07:44:04 -05:00
void isincos(float f, float *si, float *co)
2022-12-17 09:04:42 -05:00
{
bool sneg = (f < 0);
bool cneg = false;
if (sneg)
{
f = -f;
}
long whole = f;
uint8_t remain = (f - whole) * 256;
2023-11-02 10:13:39 -04:00
if (whole >= 360)
2022-12-17 09:04:42 -05:00
{
whole %= 360;
// possible faster for 360-720
// if (whole >= 720) whole %= 360;
// else whole -= 360;
}
int y = whole; // 16 bit math is faster than 32 bit
if (y >= 180)
{
y -= 180;
sneg = !sneg;
cneg = !cneg;
}
if (y >= 90)
{
y = 180 - y;
if (remain != 0)
{
remain = - remain;
y--;
}
cneg = !cneg;
}
// float value improves ~4% on avg error for ~60 bytes.
// SIN
uint16_t value = sinTable16[y];
// interpolate if needed
2023-11-02 10:13:39 -04:00
if (remain > 0)
2022-12-17 09:04:42 -05:00
{
value = value + ((sinTable16[y + 1] - value) / 8 * remain) / 32; // == * remain / 256
}
2022-12-22 07:44:04 -05:00
*si = value * 0.0000152590219; // = / 65535.0
if (sneg) *si = - *si;
2022-12-17 09:04:42 -05:00
// COS
value = sinTable16[90-y];
if (remain > 0)
{
value = sinTable16[89-y];
remain = 256 - remain;
value = value + ((sinTable16[90-y] - value) / 8 * remain) / 32; // == * remain / 256
}
2022-12-22 07:44:04 -05:00
*co = value * 0.0000152590219; // = / 65535.0
if (cneg) *co = - *co;
2022-12-17 09:04:42 -05:00
}
///////////////////////////////////////////////
//
// TAN
//
2023-11-02 10:13:39 -04:00
// tan() should be done with isincos()
2022-12-17 09:04:42 -05:00
// as icos() is less accurate => tan() less accurate.
/*
float itan(float f)
{
float x, y;
isincos(f,x,y);
if (y != 0) return x/y;
return NAN;
}
*/
2022-04-15 10:43:05 -04:00
float itan(float f)
{
2022-12-03 06:59:56 -05:00
// reference
// return isin(f)/icos(f);
2023-11-02 10:13:39 -04:00
// idea is to divide two (interpolated) values from the table
2022-12-03 06:59:56 -05:00
// so no divide by 65535
2023-11-02 10:13:39 -04:00
2022-12-03 06:59:56 -05:00
// FOLDING
bool mirror = false;
bool negative = (f < 0);
if (negative) f = -f;
long whole = f;
float remain = f - whole;
if (whole >= 180) whole %= 180;
float value = remain + whole; // normalised value 0..179.9999
if (value > 90)
2022-04-15 10:43:05 -04:00
{
2022-12-03 06:59:56 -05:00
value = 180 - value;
negative = !negative;
mirror = true;
2022-04-15 10:43:05 -04:00
}
2022-12-03 06:59:56 -05:00
uint8_t d = value;
if (d == 90) return NAN;
2022-04-15 10:43:05 -04:00
2022-12-03 06:59:56 -05:00
// COS FIRST
2022-04-15 10:43:05 -04:00
uint8_t p = 90 - d;
2022-12-03 06:59:56 -05:00
float co = sinTable16[p];
if (remain != 0)
2022-04-15 10:43:05 -04:00
{
2022-12-03 06:59:56 -05:00
float delta = (sinTable16[p] - sinTable16[p - 1]);
if (mirror) co = sinTable16[p - 1] + remain * delta;
else co = sinTable16[p] - remain * delta;
2022-04-15 10:43:05 -04:00
}
else if (co == 0) return 0;
2022-12-03 06:59:56 -05:00
float si = sinTable16[d];
if (remain != 0) si += remain * (sinTable16[d + 1] - sinTable16[d]);
2022-04-15 10:43:05 -04:00
float ta = si/co;
2022-12-03 06:59:56 -05:00
if (negative) return -ta;
2022-04-15 10:43:05 -04:00
return ta;
}
2022-12-03 06:59:56 -05:00
// some problem at 0 but at least we have a icot(x) cotangent.
2022-04-15 10:43:05 -04:00
float icot(float f)
{
2022-12-03 06:59:56 -05:00
float ta = itan(f);
if (ta == 0) return NAN;
return 1.0 / ta;
2022-04-15 10:43:05 -04:00
}
///////////////////////////////////////////////////////
//
2022-12-03 06:59:56 -05:00
// INVERSE GONIO LOOKUP
2022-04-15 10:43:05 -04:00
//
float iasin(float f)
{
2022-12-03 06:59:56 -05:00
bool negative = (f < 0);
if (negative)
2022-04-15 10:43:05 -04:00
{
f = -f;
2022-12-03 06:59:56 -05:00
negative = true;
2022-04-15 10:43:05 -04:00
}
2022-12-03 06:59:56 -05:00
uint16_t value = round(f * 65535);
2022-04-15 10:43:05 -04:00
uint8_t lo = 0;
uint8_t hi = 90;
while (hi - lo > 1)
{
uint8_t mi = (lo + hi) / 2;
2022-12-03 06:59:56 -05:00
if (sinTable16[mi] == value)
2022-04-15 10:43:05 -04:00
{
2022-12-03 06:59:56 -05:00
if (negative) return -mi;
2022-04-15 10:43:05 -04:00
return mi;
}
2022-12-03 06:59:56 -05:00
if (sinTable16[mi] < value) lo = mi;
2022-04-15 10:43:05 -04:00
else hi = mi;
2023-11-02 10:13:39 -04:00
}
2022-12-03 06:59:56 -05:00
float delta = value - sinTable16[lo];
uint16_t range = sinTable16[hi] - sinTable16[lo];
2022-04-15 10:43:05 -04:00
delta /= range;
2023-11-02 10:13:39 -04:00
if (negative) return -(lo + delta);
return (lo + delta);
2022-04-15 10:43:05 -04:00
}
float iacos(float f)
{
return 90 - iasin(f);
}
2022-12-03 06:59:56 -05:00
// PLACEHOLDER
2022-04-15 10:43:05 -04:00
float iatan(float f)
{
return 0 * f;
}
2022-12-08 11:32:53 -05:00
float atanFast(float x)
{
// remove two test will limit the input range but makes it even faster.
2022-12-22 07:44:04 -05:00
if ( x > 1) return ( M_PI / 2) - atanHelper(1.0 / x);
if ( x < -1) return (-M_PI / 2) - atanHelper(1.0 / x);
2022-12-08 11:32:53 -05:00
return atanHelper(x);
}
inline float atanHelper(float x)
{
float x2 = x * x;
return (((0.079331 * x2) - 0.288679) * x2 + 0.995354) * x;
// an even more accurate alternative, less fast
// return ((((-0.0389929 * x2) + 0.1462766) * x2 - 0.3211819) * x2 + 0.9992150) * x;
}
float atan2Fast(float y, float x)
{
// catch singularity.
2022-12-22 07:44:04 -05:00
if ((x == 0) && (y == 0)) return NAN;
2022-12-08 11:32:53 -05:00
if (x >= 0)
{
if (y >= 0)
{
if (fabs(y) >= fabs(x)) return M_PI / 2 - atanFast(x / y);
return atanFast(y / x);
}
if (fabs(y) >= fabs(x)) return -M_PI / 2 - atanFast(x / y);
return atanFast(y / x);
}
else
{
if (y >= 0)
{
if (fabs(y) >= fabs(x)) return M_PI / 2 - atanFast(x / y);
return M_PI + atanFast(y / x);
}
if (fabs(y) >= fabs(x)) return -M_PI / 2 - atanFast(x / y);
return -M_PI + atanFast(y / x);
}
}
2022-12-18 10:36:10 -05:00
///////////////////////////////////////////////////////
//
// HYPOT
// related but not strict gonio.
//
// hypotFast() formula for faster hypot() at the price of accuracy
// experimental!
float hypotFast(float x, float y)
{
float a = fabs(x);
float b = fabs(y);
if (a > b)
{
a = fabs(y);
b = fabs(x);
}
float z = 0.917981 * (b + a / 2);
if (z > b) return z;
return b;
}
2022-12-03 06:59:56 -05:00
// -- END OF FILE --
2022-12-17 09:04:42 -05:00