Skip to content

Instantly share code, notes, and snippets.

@MrAnnix
Last active July 6, 2020 11:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrAnnix/ecb55cec0b9da5c34988ae79d008f4a2 to your computer and use it in GitHub Desktop.
Save MrAnnix/ecb55cec0b9da5c34988ae79d008f4a2 to your computer and use it in GitHub Desktop.
Very accurate and fast sine computation based on Bhaskara I's sine approximation formula. https://en.wikipedia.org/wiki/Bhaskara_I%27s_sine_approximation_formula
#define E_PI 3.141592653589793238462643383279502884197169399375105820974
#define E_TAU 6.283185307179586476925286766559005768394338798750211641950
#define E_PI_SQ 9.869604401089358618834490999876151135313699407240790626413
double fast_sin(double var){
int loops = var/(2*E_PI);
var = var - loops*2*E_PI;
if(var>E_PI){
var = E_PI - var;
}else if((-var)>E_PI){
var = -E_PI - var;
}// Make the angle to be between -pi and pi
if(var<0){
var = -var;
return -(16.0*var*(E_PI - var))/(5.0*E_PI_SQ - 4.0*var*(E_PI - var)); // Sine is an odd function -> sin(x) = -sin(x)
}else{
return (16.0*var*(E_PI - var))/(5.0*E_PI_SQ - 4.0*var*(E_PI - var));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment