예제 #1
0
            public static long Sin(long theta)
            {
                //Taylor series cuz easy
                //TODO: Profiling
                //Note: Max 4 multiplications before overflow

                theta = theta - FixedMath.TwoPi * FixedMath.Floor((theta + FixedMath.Pi) / FixedMath.TwoPi);
                long thetaSquared = theta.Mul(theta);

                long      result = theta;
                const int shift  = FixedMath.SHIFT_AMOUNT;
                //2 shifts for 2 multiplications but there's a division so only 1 shift
                long       n          = (theta * theta * theta) >> (shift * 1);
                const long Factorial3 = 3 * 2 * FixedMath.One;

                result -= n / Factorial3;

                n  *= thetaSquared;
                n >>= shift;
                const long Factorial5 = Factorial3 * 4 * 5;

                result += (n / Factorial5);

                n  *= thetaSquared;
                n >>= shift;
                const long Factorial7 = Factorial5 * 6 * 7;

                result -= n / Factorial7;

                                #if true || HIGH_ACCURACY
                //Required or there'll be .07 inaccuracy
                n  *= thetaSquared;
                n >>= shift;
                const long Factorial9 = Factorial7 * 8 * 9;
                result += n / Factorial9;
                                #endif
                return(result);
            }