Fast trig

I am wondering what is reason for the fast trig class has three lists. I could be dun with one taking up one third of the memory with not much time lost(just one ot two math calculations) .

FastTrig was added in by eric mostly as a quick fix for pq torus and is the way it is b/c it looks like code that was pulled from the jgo forums. If you code a better way and test the heck out of it, we’ll likely put it in. :slight_smile:

Err. It wasn’t me that added FastTrig! In fact, I never touched it except to point out that in it’s original incarnation the sin and cos approximations were utter garbage. Renanse, you’re the one that initially committed it and made the vast majority of the changes. One of the CVS commit entries even reads “renanse - 3/21/04: added JCD’s fast trig class as a subclass”! Heh. :slight_smile:

As for FastTrig containing three lists, it has sine, cosine and tangent tables. I don’t see how we could eliminate any without sacrificing the speed for which FastTrig was designed.

This could seamed worked when I tested It with the PQ Tories. There was no noticeable drop in speed. It needs slightly more testing.

  static public class FastTrig {

        public static int PRECISION = 0x100000;

        private static float RAD_SLICE = TWO_PI / PRECISION, sinTable[] = null;

        static {

            sinTable = new float[PRECISION];

            float rad = 0;

            for (int i = 0; i < PRECISION; i++) {
                rad = (float) i * RAD_SLICE;
                sinTable[i] = (float) java.lang.Math.sin(rad);

        private static final int radToIndex(float radians) {
            return (int) ((radians / TWO_PI) * (float) PRECISION)
                    & (PRECISION - 1);

        public static float sin(float radians) {
            return sinTable[radToIndex(radians)];

        public static float cos(float radians) {
            return sin(radians+HALF_PI);

        public static float tan(float radians) {
            return sin(radians)/cos(radians);

Heh, sorry eric, wasn’t pointing fingers, just faulty memory… only could recall that pqtorus was the only thing using it. :slight_smile:

It might be worth the drop of cos table, but the additional division makes me pause on dropping tan table… divisions can be expensive

I have tested my could sum more. The new cosine function works but the tan does not work if the cos(x)<.01

Update: I’ve updated FastTrig to only use a sin and tan table. cos(rad) is implemented as sin(pi/2 - rad).

Also, There is now a static boolean flag in FastMath itself called USE_FAST_TRIG. By default it is set to false. If you set it to true, all calls to FastMath.sin, cos and tan will be forwarded to FastTrig. You can access and set it to true in your own programs if you are using jme in such a way as to make it use a lot of trig.

A thought… We might want to include atan and asin tables in FastTrig as well since those are also heavily used.