mathmodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 728 行 · 第 1/2 页

JAVA
728
字号
      else        sb.append((char) (d + 'a' - 10));    } while (value != 0);    sb.reverse();    return env.createString(sb.toString());  }  /**   * Convert a number between arbitrary bases   *   * @param number A string represeantion of an binary number.   * @param fromBase The base of the number parameter.   * @param toBase The base of convert to.   * @return the number as a Value, either a LongValue or a DoubleValue.   */  public static Value base_convert(Env env, StringValue number, int fromBase, int toBase)  {    if (fromBase < 2 || fromBase > 36) {      env.warning(L.l("invalid `{0}' ({1})", "from base", fromBase));      return BooleanValue.FALSE;    }    if (toBase < 2 || toBase > 36) {      env.warning(L.l("invalid `{0}' ({1})", "to base", toBase));      return BooleanValue.FALSE;    }    Value val = baseToValue(env, number.toString(), fromBase);    return valueToBase(env, val, toBase);  }  /**   * Returns the decimal equivalent of the binary number represented by the   * binary string argument.   *   * @param bin A string represeantion of an binary number.   * @return the decimal equivalent of the binary number   */  public static Value bindec(Env env, StringValue bin)  {     return baseToValue(env, bin.toString(), 2);  }  public static double ceil(double value)  {    return Math.ceil(value);  }  public static double cos(double value)  {    return Math.cos(value);  }  public static double cosh(double value)  {    return Math.cosh(value);  }  /**   * Returns a binary representation of a number.   * @param value the number   */  public static StringValue decbin(Env env, long value)  {    return valueToBase2(env, value);  }  /**   * Returns a hexadecimal representation of a number.   * @param value the number   */  public static StringValue dechex(Env env, long value)  {    return valueToBase16(env, value);  }  /**   * Returns an octal representation of a number.   * @param value the number   */  public static StringValue decoct(Env env, long value)  {    return valueToBase8(env, value);  }  public static double deg2rad(double value)  {    return value * Math.PI / 180;  }  public static Value exp(Value value)  {    return new DoubleValue(Math.exp(value.toDouble()));  }  public static Value expm1(Value value)  {    return new DoubleValue(Math.expm1(value.toDouble()));  }  public static Value floor(Value value)  {    return new DoubleValue(Math.floor(value.toDouble()));  }  public static double fmod(double xV, double yV)  {    return Math.IEEEremainder(xV, yV);  }  public static Value hexdec(Env env, StringValue s)  {    return baseToValue(env, s.toString(), 16);  }  public static double hypot(double a, double b)  {    return Math.hypot(a, b);  }  public static boolean is_finite(Value value)  {    if (value instanceof LongValue)      return true;    else if (value instanceof DoubleValue) {      double v = value.toDouble();      return ! Double.isInfinite(v);    }    else      return false;  }  public static Value is_infinite(Value value)  {    if (value instanceof LongValue)      return BooleanValue.FALSE;    else if (value instanceof DoubleValue) {      double v = value.toDouble();      return Double.isInfinite(v) ? BooleanValue.TRUE : BooleanValue.FALSE;    }    else      return BooleanValue.FALSE;  }  public static Value is_nan(Value value)  {    if (value instanceof LongValue)      return BooleanValue.FALSE;    else if (value instanceof DoubleValue) {      double v = value.toDouble();      return Double.isNaN(v) ? BooleanValue.TRUE : BooleanValue.FALSE;    }    else      return BooleanValue.FALSE;  }  public static double log(double value)  {    return Math.log(value);  }  public static double log10(double value)  {    return Math.log10(value);  }  public static double log1p(double value)  {    return Math.log1p(value);  }  public static Value getrandmax()  {    return mt_getrandmax();  }  public static Value max(Env env, Value []args)  {    if (args.length == 1 && args[0] instanceof ArrayValue) {      Value array = args[0];      Value max = NullValue.NULL;      double maxValue = Double.MIN_VALUE;      Iterator<Value> iter = array.getValueIterator(env);      while (iter.hasNext()) {        Value value = iter.next();        double dValue = value.toDouble();        if (maxValue < dValue) {          maxValue = dValue;          max = value;        }      }      return max;    }    else {      double maxValue = - Double.MAX_VALUE;      Value max = NullValue.NULL;      for (int i = 0; i < args.length; i++) {        double value = args[i].toDouble();        if (maxValue < value) {          maxValue = value;          max = args[i];        }      }      return max;    }  }  public static Value min(Env env, Value []args)  {    if (args.length == 1 && args[0] instanceof ArrayValue) {      Value array = args[0];      Value min = NullValue.NULL;      double minValue = Double.MAX_VALUE;      Iterator<Value> iter = array.getValueIterator(env);      while (iter.hasNext()) {        Value value = iter.next();        double dValue = value.toDouble();        if (dValue < minValue) {          minValue = dValue;          min = value;        }      }      return min;    }    else {      double minValue = Double.MAX_VALUE;      Value min = NullValue.NULL;      for (int i = 0; i < args.length; i++) {        double value = args[i].toDouble();        if (value < minValue) {          minValue = value;          min = args[i];        }      }      return min;    }  }  public static Value mt_getrandmax()  {    return new LongValue(RAND_MAX);  }  public static long mt_rand(@Optional("0") long min,                             @Optional("RAND_MAX") long max)  {    long range = max - min + 1;    if (range <= 0)      return min;    long value = RandomUtil.getRandomLong();    if (value < 0)      value = - value;    return min + value % range;  }  public static Value mt_srand(@Optional long seed)  {    return NullValue.NULL;  }  /**   *  Returns the decimal equivalent of the octal number represented by the   *  octal_string argument.   *   * @param oct A string represeantion of an octal number.   * @return the decimal equivalent of the octal number   */  public static Value octdec(Env env, StringValue oct)  {    return baseToValue(env, oct.toString(), 8);  }  public static double pi()  {    return M_PI;  }  /*   * XXX: may return an integer if it can be represented   *      by a system-dependent integer   */  public static double pow(double base, double exp)  {    return Math.pow(base, exp);  }  public static double rad2deg(double value)  {    return 180 * value / Math.PI;  }  public static long rand(@Optional int min,			  @Optional("RAND_MAX") int max)  {    return mt_rand(min, max);  }  public static double round(double value, @Optional int precision)  {    if (precision > 0) {      double exp = Math.pow(10, precision);      return Math.round(value * exp) / exp;    }    else      return Math.round(value);  }  public static double sin(double value)  {    return Math.sin(value);  }  public static Value sinh(Value value)  {    return new DoubleValue(Math.sinh(value.toDouble()));  }  public static double sqrt(double value)  {    return Math.sqrt(value);  }  public static Value srand(@Optional long seed)  {    return NullValue.NULL;  }  public static double tan(double value)  {    return Math.tan(value);  }  public static double tanh(double value)  {    return Math.tanh(value);  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?