Skip to content

Instantly share code, notes, and snippets.

@linnykoleh
Last active June 16, 2023 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linnykoleh/dd7106df881a97f72d0ae2356498bbf2 to your computer and use it in GitHub Desktop.
Save linnykoleh/dd7106df881a97f72d0ae2356498bbf2 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class SpeedUpTheCodeSin {
public static void main(String[] args) {
int N = 5;
int terms = 5;
float[] x = {0.5f, 1.0f, 1.5f, 2.0f, 2.5f};
float[] result = new float[N];
var denomCache = new int[terms + 1];
denomCache[0] = 6;
for (int j = 1; j <= terms; j++) {
denomCache[j] = denomCache[j - 1] * (2 * j + 2) * (2 * j + 3);
}
var denoms = new float[terms + 1];
for (int j = 0; j <= terms; j++) {
denoms[j] = (float) 1 / denomCache[j];
}
sinxFast(N, terms, denoms, x, result);
System.out.println(Arrays.toString(result));
}
static void sinxFast2(int N, int terms, float[] denoms, float[] x, float[] result) {
float v, n1, n2;
for (int i = 0; i < N; i++) {
v = x[i];
n1 = v * v;
n2 = v * n1;
for (int j = 1; j <= terms; j++) {
n2 = -n2;
v += n2 * denoms[j - 1];
n2 *= n1;
}
result[i] = v;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment