Last active
October 26, 2024 05:26
-
-
Save kaievns/5570f3dfa40d9a38b2e4a424a070803f to your computer and use it in GitHub Desktop.
Fake math.h substitute for embedded systems to calculate sin/cos values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A super simple sin/cos substitute based on taylor sequences | |
* to avoid importing the math.h | |
* | |
* sin x = x - x^3/3! + x^5/5! -x^7/7! ... | |
* cos x = 1 -x^2/2!+x^4/4!-x^6/6! ... | |
*/ | |
// #include "math.h" | |
// #include <zephyr/kernel.h> | |
#include <stdio.h> | |
#define PI (3.14159265359f) | |
#define TERMS 7 | |
float simple_sin(float rad) | |
{ | |
float sign = 1.0; | |
if (rad < 0) | |
{ | |
sign = -1.0; | |
rad = -rad; | |
} | |
float res = 0; | |
float term = rad; | |
int k = 1; | |
while (k < TERMS * 2) | |
{ | |
res += term; | |
k += 2; | |
term *= -rad * rad / k / (k - 1); | |
} | |
return sign * res; | |
} | |
float simple_cos(float rad) | |
{ | |
if (rad < 0) | |
{ | |
rad = -rad; | |
} | |
float res = 0; | |
float term = 1; | |
int k = 0; | |
while (k < TERMS * 2) | |
{ | |
res += term; | |
k += 2; | |
term *= -rad * rad / k / (k - 1); | |
} | |
return res; | |
} | |
// conver clockwise degrees into counter-clockwise radians | |
float deg_to_cc_rad(int deg) | |
{ | |
deg %= 360; // make it less than 360 | |
return -(deg * PI) / 180.0f; | |
} | |
// clockwise (accepts negatives too) | |
float sin_from_deg(int deg) | |
{ | |
return simple_sin(deg_to_cc_rad(deg)); | |
} | |
float cos_from_deg(int deg) | |
{ | |
return simple_cos(deg_to_cc_rad(deg % 360)); | |
} | |
int main() | |
{ | |
printf("Sin 0 %f\n", sin_from_deg(0)); | |
printf("Sin 30 %f\n", sin_from_deg(30)); | |
printf("Sin 45 %f\n", sin_from_deg(45)); | |
printf("Sin 60 %f\n", sin_from_deg(60)); | |
printf("Sin 90 %f\n", sin_from_deg(90)); | |
printf("Sin 110 %f\n", sin_from_deg(110)); | |
printf("Sin 150 %f\n", sin_from_deg(150)); | |
printf("Sin 180 %f\n", sin_from_deg(180)); | |
printf("------------\n"); | |
printf("Sin 0 %f\n", sin_from_deg(0)); | |
printf("Sin -30 %f\n", sin_from_deg(-30)); | |
printf("Sin -45 %f\n", sin_from_deg(-45)); | |
printf("Sin -60 %f\n", sin_from_deg(-60)); | |
printf("Sin -90 %f\n", sin_from_deg(-90)); | |
printf("Sin -110 %f\n", sin_from_deg(-110)); | |
printf("Sin -150 %f\n", sin_from_deg(-150)); | |
printf("Sin -180 %f\n", sin_from_deg(-180)); | |
printf("===============\n"); | |
printf("Cos 0 %f\n", cos_from_deg(0)); | |
printf("Cos 30 %f\n", cos_from_deg(30)); | |
printf("Cos 45 %f\n", cos_from_deg(45)); | |
printf("Cos 60 %f\n", cos_from_deg(60)); | |
printf("Cos 90 %f\n", cos_from_deg(90)); | |
printf("Cos 110 %f\n", cos_from_deg(110)); | |
printf("Cos 150 %f\n", cos_from_deg(150)); | |
printf("Cos 180 %f\n", cos_from_deg(180)); | |
printf("------------\n"); | |
printf("Cos 0 %f\n", cos_from_deg(0)); | |
printf("Cos -30 %f\n", cos_from_deg(-30)); | |
printf("Cos -45 %f\n", cos_from_deg(-45)); | |
printf("Cos -60 %f\n", cos_from_deg(-60)); | |
printf("Cos -90 %f\n", cos_from_deg(-90)); | |
printf("Cos -110 %f\n", cos_from_deg(-110)); | |
printf("Cos -150 %f\n", cos_from_deg(-150)); | |
printf("Cos -180 %f\n", cos_from_deg(-180)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment