public static fint Atan2(fint F1, fint F2)
        {
            if (F2.raw == 0 && F1.raw == 0)
            {
                return(fint.zero);
            }

            fint result = fint.zero;

            if (F2 > fint.zero)
            {
                result = Atan(F1 / F2);
            }
            else if (F2 < fint.zero)
            {
                if (F1 >= fint.zero)
                {
                    result = (PI - Atan(Abs(F1 / F2)));
                }
                else
                {
                    result = -(PI - Atan(Abs(F1 / F2)));
                }
            }
            else
            {
                result = (F1 >= fint.zero ? PI : -PI) / fint.CreateFromInt(2);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Create a fixed-int number from parts.  For example, to create 1.5 pass in 1 and 500.
        /// </summary>
        /// <param name="PreDecimal">The number above the decimal.  For 1.5, this would be 1.</param>
        /// <param name="PostDecimal">The number below the decimal, to three digits.
        /// For 1.5, this would be 500. For 1.005, this would be 5.</param>
        /// <returns>A fixed-int representation of the number parts</returns>
        public static fint FromParts(int PreDecimal, int PostDecimal)
        {
            fint f = fint.CreateFromInt(PreDecimal);

            if (PostDecimal != 0)
            {
                f += (fint.CreateFromInt(PostDecimal) / fint.CreateFromInt(1000));
            }
            return(f);
        }
 public static fint Abs(fint F)
 {
     if (F.raw < 0)
     {
         return(-F);
     }
     else
     {
         return(F);
     }
 }
 public static fint Clamp01(fint value)
 {
     if (value < fint.zero)
     {
         return(fint.zero);
     }
     if (value > fint.one)
     {
         return(fint.one);
     }
     return(value);
 }
 private static fint sin_lookup(fint i, fint j)
 {
     if (j.raw > 0 && j < fint.CreateRaw(10) && i < fint.CreateRaw(90))
     {
         return(fint.CreateRaw(SIN_TABLE[i.raw]) +
                ((fint.CreateRaw(SIN_TABLE[i.raw + 1]) - fint.CreateRaw(SIN_TABLE[i.raw])) /
                 fint.CreateRaw(10)) * j);
     }
     else
     {
         return(fint.CreateRaw(SIN_TABLE[i.raw]));
     }
 }
 public static fint Clamp(fint value, fint min, fint max)
 {
     if (value < min)
     {
         value = min;
     }
     else
     {
         if (value > max)
         {
             value = max;
         }
     }
     return(value);
 }
 public static fint Sqrt(fint f)
 {
     if (f.raw > 0x3e8000)
     {
         return(Sqrt(f, 16 * 3 / 4));
     }
     else if (f.raw > 0x64000)
     {
         return(Sqrt(f, 12 * 2 / 3));
     }
     else
     {
         return(Sqrt(f, 8 * 2 / 3));
     }
 }
Example #8
0
        /// <summary>
        /// Inserts value into array using insertion sort. If the value is greater than the largest value in the array
        /// it will not be added to the array.
        /// </summary>
        /// <param name="arr">The array to insert the value into.</param>
        /// <param name="value">The value to insert into the array.</param>
        private static void insert(fint[] arr, fint value)
        {
            fint temp;

            for (int i = arr.Length - 1; i >= 0; i--)
            {
                if (value > arr[i])
                {
                    break;
                }
                temp   = arr[i];
                arr[i] = value;
                if (i + 1 < arr.Length)
                {
                    arr[i + 1] = temp;
                }
            }
        }
        public static fint Sqrt(fint f, int NumberOfIterations)
        {
            if (f.raw < 0) //NaN in Math.Sqrt
            {
                throw new ArithmeticException("Input Error");
            }

            if (f.raw == 0)
            {
                return(fint.zero);
            }

#if USE_OPTIMIZATIONS
            long fraw      = f.raw;
            long frawshift = (fraw << fint.SHIFT_AMOUNT);
            long k         = fraw + fint.one.raw >> 1;

            for (int i = 0; i < NumberOfIterations; i++)
            {
                k = (k + (frawshift / k)) >> 1;
            }

            if (k < 0)
            {
                throw new ArithmeticException("Overflow");
            }

            return(fint.CreateRaw((int)k));
#else
            fint k = f + fint.one >> 1;
            for (int i = 0; i < NumberOfIterations; i++)
            {
                k = (k + (f / k)) >> 1;
            }

            if (k.raw < 0)
            {
                throw new ArithmeticException("Overflow");
            }

            return(k);
#endif
        }
Example #10
0
        public static fint Min(params fint[] values)
        {
            int num = values.Length;

            if (num == 0)
            {
                return(fint.zero);
            }
            fint num2 = values[0];

            for (int i = 1; i < num; i++)
            {
                if (values[i] < num2)
                {
                    num2 = values[i];
                }
            }
            return(num2);
        }
Example #11
0
        public static fint Asin(fint F)
        {
            bool isNegative = F.raw < 0;

            F = Abs(F);

            if (F > fint.one)
            {
                throw new ArithmeticException("Bad Asin Input:" + F.ToFloat());
            }

            fint f1 = mul(mul(mul(mul(
                                      fint.CreateRaw(145103 >> fint.SHIFT_AMOUNT), F) -
                                  fint.CreateRaw(599880 >> fint.SHIFT_AMOUNT), F) +
                              fint.CreateRaw(1420468 >> fint.SHIFT_AMOUNT), F) -
                          fint.CreateRaw(3592413 >> fint.SHIFT_AMOUNT), F) +
                      fint.CreateRaw(26353447 >> fint.SHIFT_AMOUNT);

            fint f2 = HalfPI - (Sqrt(fint.one - F) * f1);

            return(isNegative ? -f2 : f2);
        }
Example #12
0
        public static fint Sin(fint i)
        {
            fint j = fint.zero;

            for (; i.raw < 0; i += fint.CreateRaw(25736))
            {
                ;
            }
            if (i > fint.CreateRaw(25736))
            {
                i %= fint.CreateRaw(25736);
            }
            fint k = (i * fint.CreateRaw(10)) / fint.CreateRaw(714);

            if (i.raw != 0 && i != fint.CreateRaw(6434) && i != fint.CreateRaw(12868) &&
                i != fint.CreateRaw(19302) && i != fint.CreateRaw(25736))
            {
                j = (i * fint.CreateRaw(100)) / fint.CreateRaw(714) - k * fint.CreateRaw(10);
            }
            if (k <= fint.CreateRaw(90))
            {
                return(sin_lookup(k, j));
            }
            if (k <= fint.CreateRaw(180))
            {
                return(sin_lookup(fint.CreateRaw(180) - k, j));
            }
            if (k <= fint.CreateRaw(270))
            {
                return(-sin_lookup(k - fint.CreateRaw(180), j));
            }
            else
            {
                return(-sin_lookup(fint.CreateRaw(360) - k, j));
            }
        }
Example #13
0
 private static fint mul(fint F1, fint F2)
 {
     return(F1 * F2);
 }
Example #14
0
 public static fint Min(fint a, fint b)
 {
     return((a >= b) ? b : a);
 }
Example #15
0
 public static fint Max(fint a, fint b)
 {
     return((a <= b) ? b : a);
 }
Example #16
0
 public static fint Cos(fint i)
 {
     return(Sin(i + fint.CreateRaw(6435)));
 }
Example #17
0
 public static fint Tan(fint i)
 {
     return(Sin(i) / Cos(i));
 }
 /// <summary>
 /// Inserts value into array using insertion sort. If the value is greater than the largest value in the array
 /// it will not be added to the array.
 /// </summary>
 /// <param name="arr">The array to insert the value into.</param>
 /// <param name="value">The value to insert into the array.</param>
 private static void insert(fint[] arr, fint value)
 {
     fint temp;
     for (int i = arr.Length - 1; i >= 0; i--)
     {
         if (value > arr[i]) break;
         temp = arr[i];
         arr[i] = value;
         if (i + 1 < arr.Length) arr[i + 1] = temp;
     }
 }
Example #19
0
 public static fint Acos(fint F)
 {
     return(HalfPI - Asin(F));
 }
Example #20
0
 public static fint Atan(fint F)
 {
     return(Asin(F / Sqrt(fint.one + (F * F))));
 }