Quantcast
Channel: User Lundin - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 42

Answer by Lundin for Implementation of the trigonometric functions for real time application

$
0
0

One remark is that you should get rid of all the needless branching and code repetition. It's bad for performance and code maintenance both.

Given an angle you should be able to:

  • Take it's absolute value.
  • Divide by PI/2.
  • Convert to unsigned integer, truncating decimals.

Then you'll either have an index from 0 to 3 or you started with an angle larger than 2*PI (and might have to deal with that first, if that's a valid use case?).

Pseudo code:

double angle = ...;angle /= PI/2.0;unsigned long q = (unsigned long)angle;// optionally some error handling/sanity checking herequadrant(q, angle);

Where the quadrant function can use the variable q as an index to various other tables containing mulitpliers etc necessary for your calculation. It should boil down to the 5-6 lines that you currently have inside each if-else if.


Viewing all articles
Browse latest Browse all 42

Trending Articles