Riven, thanks for the lookup code (I was using modulus instead of masking…).
Your atan2 lookup, run on my desktop, takes about 1.6 times longer than the atan2 I posted above. However, yours is quite a bit more accurate. It is good to have options! Both are waaay faster (5+ times) than Math.atan2. That was the desktop, on the G1 I see (10k iterations with 20k precalculated random numbers):
Math.atan2: 159.66797
Riven’s atan2: 58.86841
Nate’s atan2: 48.49243
So, a smaller difference all around.
I played with Riven’s lookup table and DzzD’s low precision forumula. Benchmark code:
for (int i = 0; i < 10; i++)
test(false);
test(true);
test(true);
test(true);
public void test (boolean log) {
int count = 50000;
long s, e;
Random random = new Random();
float[] numbers = new float[count];
for (int i = 0; i < count; i++)
numbers[i] = random.nextFloat();
s = System.nanoTime();
for (int i = 0; i < count; i++)
Math.cos(numbers[i]);
e = System.nanoTime();
if (log) System.out.println("Java: " + (e - s) / 1000000f);
s = System.nanoTime();
for (int i = 0; i < count; i++)
FastMath2.cos(numbers[i]); // Riven
e = System.nanoTime();
if (log) System.out.println("Riven: " + (e - s) / 1000000f);
s = System.nanoTime();
for (int i = 0; i < count; i++)
FastMath.cos(numbers[i]); // DzzD
e = System.nanoTime();
if (log) System.out.println("DzzD: " + (e - s) / 1000000f);
if (log) {
System.out.println("Input: " + numbers[3]);
System.out.println("DzzD: " + FastMath.cos(numbers[3]));
System.out.println("Riven: " + FastMath2.cos(numbers[3]));
System.out.println("Java: " + Math.cos(numbers[3]));
System.out.println();
}
}
Results on my G1:
[quote]Speed:
Java: 327.54517
Riven: 80.47485
DzzD: 149.93286
Java: 330.07813
Riven: 79.10156
DzzD: 150.08545
Java: 331.29883
Riven: 80.62744
DzzD: 150.23804
Accuracy:
Input: 0.16849637
DzzD: 0.9169282
Riven: 0.98592603
Java: 0.9858380402606572
Input: 0.068103254
DzzD: 0.96614116
Riven: 0.99767107
Java: 0.9976818695836023
Input: 0.99370223
DzzD: 0.5429537
Riven: 0.5459677
Java: 0.5455909445089435
[/quote]
Accuracy probably varies more when the input is not between 0 and 1.
Edit: Anyone have a good solution for sqrt?