-
-
Save mooman219/9698a531c932c08ccefd86ba7cd4463a to your computer and use it in GitHub Desktop.
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
Average Error 0.00060 / Largest Error 0.00224 - Performance 7.306 ns/op | |
AMD FX-8350 |
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
private static final int SIN_BITS, SIN_MASK, SIN_COUNT; | |
private static final float radFull, radToIndex; | |
private static final float degFull, degToIndex; | |
private static final float[] sin, cos; | |
static { | |
SIN_BITS = 12; | |
SIN_MASK = ~(-1 << SIN_BITS); | |
SIN_COUNT = SIN_MASK + 1; | |
radFull = (float) (Math.PI * 2.0); | |
degFull = (float) (360.0); | |
radToIndex = SIN_COUNT / radFull; | |
degToIndex = SIN_COUNT / degFull; | |
sin = new float[SIN_COUNT]; | |
cos = new float[SIN_COUNT]; | |
for (int i = 0; i < SIN_COUNT; i++) { | |
sin[i] = (float) Math.sin((i + 0.5f) / SIN_COUNT * radFull); | |
cos[i] = (float) Math.cos((i + 0.5f) / SIN_COUNT * radFull); | |
} | |
// Four cardinal directions (credits: Nate) | |
for (int i = 0; i < 360; i += 90) { | |
sin[(int) (i * degToIndex) & SIN_MASK] = (float) Math.sin(i * Math.PI / 180.0); | |
cos[(int) (i * degToIndex) & SIN_MASK] = (float) Math.cos(i * Math.PI / 180.0); | |
} | |
} | |
public static final float sin(float rad) { | |
return sin[(int) (rad * radToIndex) & SIN_MASK]; | |
} | |
public static final float cos(float rad) { | |
return cos[(int) (rad * radToIndex) & SIN_MASK]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment