Skip to content

Commit

Permalink
Updated with pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shobuj-Paul committed Feb 17, 2024
1 parent 0663273 commit a6f3e5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
14 changes: 7 additions & 7 deletions include/math/cordic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace CORDIC
{
struct Trig
{
int sin;
int cos;
};
Trig trig(int angle);
}
struct Trig
{
int sin;
int cos;
};
Trig trig(int angle);
} // namespace CORDIC
8 changes: 4 additions & 4 deletions src/math/cordic.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include<math/cordic.hpp>
#include <math/cordic.hpp>

CORDIC::Trig CORDIC::trig(int angle)
{
int32_t atan2[14] = {8192, 4836, 2555, 1297, 651, 325, 162, 81, 40, 20, 10, 5, 2, 1};
int32_t atan2[14] = { 8192, 4836, 2555, 1297, 651, 325, 162, 81, 40, 20, 10, 5, 2, 1 };
int32_t x = 9949, y = 0, theta = 0;
for(int32_t i = 0; i < 14; i++)
for (int32_t i = 0; i < 14; i++)
{
int32_t sigma = (theta < angle) ? 1 : -1;
theta = theta + sigma * atan2[i];
int32_t nx = x - sigma * (y >> i);
y = y + sigma * (x >> i);
x = nx;
}
return {y, x};
return { y, x };
}
17 changes: 10 additions & 7 deletions tests/cordic_algo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
#include <math/functions.hpp>
#include <math/cordic.hpp>

int main(int argc, char **argv)
int main(int argc, char** argv)
{
if(argc != 2)
if (argc != 2)
{
std::cerr << "Usage: " << argv[0] << " <angle>" << std::endl;
return 1;
}
int angle = atoi(argv[1]);
int angle_cordic = (angle << 16) / 360;
CORDIC::Trig trig = CORDIC::trig(angle_cordic);
std::cout << "sin(x) in cru = " << trig.sin << " " << "cos(x) in cru = " << trig.cos << std::endl;
float sine = (float)trig.sin/16384.0F;
float cosine = (float)trig.cos/16384.0F;
std::cout << "std::sin(x) error = " << std::sin(angle*PI/180) - sine << " " << "std::cos(x) error = " << std::cos(angle*PI/180) - cosine << std::endl;
std::cout << "math::sin(x) error = " << math::sin(angle*PI/180) - sine << " " << "math::cos(x) error = " << math::cos(angle*PI/180) - cosine << std::endl;
std::cout << "sin(x) in cru = " << trig.sin << " "
<< "cos(x) in cru = " << trig.cos << std::endl;
float sine = (float)trig.sin / 16384.0F;
float cosine = (float)trig.cos / 16384.0F;
std::cout << "std::sin(x) error = " << std::sin(angle * PI / 180) - sine << " "
<< "std::cos(x) error = " << std::cos(angle * PI / 180) - cosine << std::endl;
std::cout << "math::sin(x) error = " << math::sin(angle * PI / 180) - sine << " "
<< "math::cos(x) error = " << math::cos(angle * PI / 180) - cosine << std::endl;
return 0;
}

0 comments on commit a6f3e5f

Please sign in to comment.