math工具类

public abstract class MathFP
{
  private static int _fbits = 12;
  private static int _digits = 4;
  private static int _one = 4096;
  private static int _fmask = 4095;
  private static int _dmul = 10000;
  private static int _flt = 0;
  private static int _pi = 12868;
  private static int[] e = { _one, 11134, 30266, 82270, 223636 };
  public static int PI = _pi;
  public static int E = e[1];
  public static final int MAX_VALUE = 2147483647;
  public static final int MIN_VALUE = -2147483647;

  public static int setPrecision(int paramInt)
  {
    if ((paramInt > 12) || (paramInt < 0))
      return _digits;
    _fbits = paramInt;
    _one = 1 << paramInt;
    _flt = 12 - paramInt;
    _digits = 0;
    _dmul = 1;
    _fmask = _one - 1;
    PI = _pi >> _flt;
    E = e[1] >> _flt;
    int i = _one;
    while (i != 0)
    {
      i /= 10;
      _digits += 1;
      _dmul *= 10;
    }
    return _digits;
  }

  public static int getPrecision()
  {
    return _fbits;
  }

  public static int toInt(int paramInt)
  {
    paramInt = round(paramInt, 0);
    return (paramInt >> _fbits);
  }

  public static int toFP(int paramInt)
  {
    return (paramInt << _fbits);
  }

  public static int convert(int paramInt1, int paramInt2)
  {
    int i = (paramInt1 < 0) ? -1 : 1;
    if (abs(paramInt2) < 13)
      if (_fbits < paramInt2)
        paramInt1 = paramInt1 + i * (1 << paramInt2 - _fbits >> 1) >> paramInt2 - _fbits;
      else
        paramInt1 <<= _fbits - paramInt2;
    return paramInt1;
  }

  public static int toFP(String paramString)
  {
    int i = 0;
    if (paramString.charAt(0) == '-')
      i = 1;
    String str = "-1";
    int j = paramString.indexOf(46);
    if (j >= 0)
    {
      for (str = paramString.substring(j + 1, paramString.length()); str.length() < _digits; str = str + "0");
      if (str.length() > _digits)
        str = str.substring(0, _digits);
    }
    else
    {
      j = paramString.length();
    }
    int k = 0;
    if (i != j)
      k = Integer.parseInt(paramString.substring(i, j));
    int l = Integer.parseInt(str) + 1;
    int i1 = (k << _fbits) + (l << _fbits) / _dmul;
    if (i == 1)
      i1 = -i1;
    return i1;
  }

  public static String toString(int paramInt)
  {
    int i = 0;
    if (paramInt < 0)
    {
      i = 1;
      paramInt = -paramInt;
    }
    int j = paramInt >> _fbits;
    int k = _dmul * (paramInt & _fmask) >> _fbits;
    for (String str = Integer.toString(k); str.length() < _digits; str = "0" + str);
    return ((i != 0) ? "-" : "") + Integer.toString(j) + "." + str;
  }

  public static String toString(int paramInt1, int paramInt2)
  {
    if (paramInt2 > _digits)
      paramInt2 = _digits;
    String str = toString(round(paramInt1, paramInt2));
    return str.substring(0, str.length() - _digits + paramInt2);
  }

  public static int max(int paramInt1, int paramInt2)
  {
    return ((paramInt1 < paramInt2) ? paramInt2 : paramInt1);
  }

  public static int min(int paramInt1, int paramInt2)
  {
    return ((paramInt2 < paramInt1) ? paramInt2 : paramInt1);
  }

  public static int round(int paramInt1, int paramInt2)
  {
    int i = 10;
    for (int j = 0; j < paramInt2; ++j)
      i *= 10;
    i = div(toFP(5), toFP(i));
    if (paramInt1 < 0)
      i = -i;
    return (paramInt1 + i);
  }

  public static int mul(int paramInt1, int paramInt2)
  {
    int i = 0;
    int j = _fbits;
    int k = _fmask;
    if ((paramInt1 & k) == 0)
      return ((paramInt1 >> j) * paramInt2);
    if ((paramInt2 & k) == 0)
      return (paramInt1 * (paramInt2 >> j));
    if (((paramInt1 < 0) && (paramInt2 > 0)) || ((paramInt1 > 0) && (paramInt2 < 0)))
      i = 1;
    if (paramInt1 < 0)
      paramInt1 = -paramInt1;
    if (paramInt2 < 0)
      paramInt2 = -paramInt2;
    while (max(paramInt1, paramInt2) >= 1 << 31 - --j)
    {
      paramInt1 >>= 1;
      paramInt2 >>= 1;
      k >>= 1;
    }
    int l = (paramInt1 >> j) * (paramInt2 >> j) << j;
    int i1 = (paramInt1 & k) * (paramInt2 & k) >> j;
    i1 += ((paramInt1 & (k ^ 0xFFFFFFFF)) * (paramInt2 & k) >> j);
    l = l + i1 + ((paramInt1 & k) * (paramInt2 & (k ^ 0xFFFFFFFF)) >> j) << _fbits - j;
    if (l < 0)
      throw new ArithmeticException("Overflow");
    return ((i != 0) ? -l : l);
  }

  public static int div(int paramInt1, int paramInt2)
  {
    int i = 0;
    int j = _fbits;
    if (paramInt2 == _one)
      return paramInt1;
    if ((paramInt2 & _fmask) == 0)
      return (paramInt1 / (paramInt2 >> j));
    if (((paramInt1 < 0) && (paramInt2 > 0)) || ((paramInt1 > 0) && (paramInt2 < 0)))
      i = 1;
    if (paramInt1 < 0)
      paramInt1 = -paramInt1;
    if (paramInt2 < 0)
      paramInt2 = -paramInt2;
    while (max(paramInt1, paramInt2) >= 1 << 31 - --j)
    {
      paramInt1 >>= 1;
      paramInt2 >>= 1;
    }
    int k = (paramInt1 << j) / paramInt2 << _fbits - j;
    return ((i != 0) ? -k : k);
  }

  public static int add(int paramInt1, int paramInt2)
  {
    return (paramInt1 + paramInt2);
  }

  public static int sub(int paramInt1, int paramInt2)
  {
    return (paramInt1 - paramInt2);
  }

  public static int abs(int paramInt)
  {
    if (paramInt < 0)
      return (-paramInt);
    return paramInt;
  }

  public static int sqrt(int paramInt1, int paramInt2)
  {
    if (paramInt1 < 0)
      throw new ArithmeticException("Bad Input");
    if (paramInt1 == 0)
      return 0;
    int i = paramInt1 + _one >> 1;
    for (int j = 0; j < paramInt2; ++j)
      i = i + div(paramInt1, i) >> 1;
    if (i < 0)
      throw new ArithmeticException("Overflow");
    return i;
  }

  public static int sqrt(int paramInt)
  {
    return sqrt(paramInt, 16);
  }

  public static int sin(int paramInt)
  {
    int i = mul(paramInt, div(toFP(180), PI));
    i %= toFP(360);
    if (i < 0)
      i = toFP(360) + i;
    int j = i;
    if ((i >= toFP(90)) && (i < toFP(270)))
      j = toFP(180) - i;
    else if ((i >= toFP(270)) && (i < toFP(360)))
      j = -(toFP(360) - i);
    int k = j / 90;
    int l = mul(k, k);
    int i1 = mul(mul(mul(mul(-18 >> _flt, l) + (326 >> _flt), l) - (2646 >> _flt), l) + (6434 >> _flt), k);
    return i1;
  }

  public static int asin(int paramInt)
  {
    if (abs(paramInt) > _one)
      throw new ArithmeticException("Bad Input");
    int i = (paramInt < 0) ? 1 : 0;
    if (paramInt < 0)
      paramInt = -paramInt;
    int j = mul(mul(mul(mul(35 >> _flt, paramInt) - (146 >> _flt), paramInt) + (347 >> _flt), paramInt) - (877 >> _flt), paramInt) + (6434 >> _flt);
    int k = PI / 2 - mul(sqrt(_one - paramInt), j);
    return ((i != 0) ? -k : k);
  }

  public static int cos(int paramInt)
  {
    return sin(PI / 2 - paramInt);
  }

  public static int acos(int paramInt)
  {
    return (PI / 2 - asin(paramInt));
  }

  public static int tan(int paramInt)
  {
    return div(sin(paramInt), cos(paramInt));
  }

  public static int cot(int paramInt)
  {
    return div(cos(paramInt), sin(paramInt));
  }

  public static int atan(int paramInt)
  {
    return asin(div(paramInt, sqrt(_one + mul(paramInt, paramInt))));
  }

  public static int exp(int paramInt)
  {
    if (paramInt == 0)
      return _one;
    int i = (paramInt < 0) ? 1 : 0;
    paramInt = abs(paramInt);
    int j = paramInt >> _fbits;
    int k = _one;
    for (int l = 0; l < j / 4; ++l)
      k = mul(k, e[4] >> _flt);
    if (j % 4 > 0)
      k = mul(k, e[(j % 4)] >> _flt);
    paramInt &= _fmask;
    if (paramInt > 0)
    {
      int i1 = _one;
      int i2 = 0;
      int i3 = 1;
      for (int i4 = 0; i4 < 16; ++i4)
      {
        i2 += i1 / i3;
        i1 = mul(i1, paramInt);
        i3 *= (i4 + 1);
        if ((i3 > i1) || (i1 <= 0))
          break;
        if (i3 <= 0)
          break;
      }
      k = mul(k, i2);
    }
    if (i != 0)
      k = div(_one, k);
    return k;
  }

  public static int log(int paramInt)
  {
    if (paramInt <= 0)
      throw new ArithmeticException("Bad Input");
    int i = 0;
    int j = 0;
    for (int k = 0; paramInt >= _one << 1; ++k)
      paramInt >>= 1;
    int l = k * (2839 >> _flt);
    int i1 = 0;
    if (paramInt < _one)
      return (-log(div(_one, paramInt)));
    paramInt -= _one;
    for (int i2 = 1; i2 < 20; ++i2)
    {
      if (i == 0)
        j = paramInt;
      else
        j = mul(i, paramInt);
      if (j == 0)
        break;
      i1 += ((i2 % 2 == 0) ? -1 : 1) * j / i2;
      i = j;
    }
    return (l + i1);
  }

  public static int pow(int paramInt1, int paramInt2)
  {
    int i = (paramInt2 < 0) ? 1 : 0;
    int j = _one;
    paramInt2 = abs(paramInt2);
    int k = paramInt2 >> _fbits;
    while (k-- > 0)
      j = mul(j, paramInt1);
    if (j < 0)
      throw new ArithmeticException("Overflow");
    if (paramInt1 != 0)
      j = mul(j, exp(mul(log(paramInt1), paramInt2 & _fmask)));
    else
      j = 0;
    if (i != 0)
      return div(_one, j);
    return j;
  }

  public static int atan2(int paramInt1, int paramInt2)
  {
    int i = 0;
    if (paramInt2 > 0)
    {
      i = atan(div(paramInt1, paramInt2));
    }
    else if (paramInt2 < 0)
    {
      i = ((paramInt2 < 0) ? -PI : PI) - atan(abs(div(paramInt1, paramInt2)));
    }
    else
    {
      if ((paramInt2 == 0) && (paramInt1 == 0))
        throw new ArithmeticException("Bad Input");
      i = ((paramInt2 < 0) ? -PI : PI) / 2;
    }
    return i;
  }
}

你可能感兴趣的:(J#,360)