⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 microdouble.java

📁 This is a Java library for performing floating-point calculations on small devices such as mobile p
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
   * @param   d2   the second <code>double</code> value to be compared.
   * @return  <code>true</code> if the first value is less than the second value;
   *          <code>false</code> otherwise.
   */
  public static boolean lt(long d1, long d2) {
    if (isNaN(d1) || isNaN(d2)) {
      return false;
    } else if (d2 == ZERO) {
      d2 = NEGATIVE_ZERO;
    }
    return (cmp(d1, d2) < 0);
  }
  
  /**
   * Returns <code>true</code> if the first argument is considered less than
   * or equal to the second argument according to 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#153654">section
   * 15.20.1 of the JLS</a>.  Special cases:
   * <ul>
   * <li>If either operand is NaN, then the result is false
   * <li>Positive zero and negative zero are considered equal
   * <li>Negative infinity is conisdered less than all other values except NaN
   * <li>Positive infinity is conisdered greater than all other values except NaN
   * </ul>
   * <p>
   * This method takes the place of the <code>&lt;=</code> operator.
   *
   * @param   d1   the first <code>double</code> value to be compared.
   * @param   d2   the second <code>double</code> value to be compared.
   * @return  <code>true</code> if the first value is less than or equal to 
   *          the second value; <code>false</code> otherwise.
   */
  public static boolean le(long d1, long d2) {
    if (isNaN(d1) || isNaN(d2)) {
      return false;
    } else if (d2 == NEGATIVE_ZERO) {
      d2 = ZERO;
    }
    return (cmp(d1, d2) <= 0);
  }

  /**
   * Returns <code>true</code> if the first argument is considered greater than
   * the second argument according to 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#153654">section
   * 15.20.1 of the JLS</a>.  Special cases:
   * <ul>
   * <li>If either operand is NaN, then the result is false
   * <li>Positive zero and negative zero are considered equal
   * <li>Negative infinity is conisdered less than all other values except NaN
   * <li>Positive infinity is conisdered greater than all other values except NaN
   * </ul>
   * <p>
   * This method takes the place of the <code>&gt;</code> operator.
   *
   * @param   d1   the first <code>double</code> value to be compared.
   * @param   d2   the second <code>double</code> value to be compared.
   * @return  <code>true</code> if the first value is greater than the second value;
   *          <code>false</code> otherwise.
   */
  public static boolean gt(long d1, long d2) {
    if (isNaN(d1) || isNaN(d2)) {
      return false;
    } else if (d1 == ZERO) {
      d1 = NEGATIVE_ZERO;
    }
    return (cmp(d1, d2) > 0);
  }
  
  /**
   * Returns <code>true</code> if the first argument is considered greater than
   * or equal to the second argument according to 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#153654">section
   * 15.20.1 of the JLS</a>.  Special cases:
   * <ul>
   * <li>If either operand is NaN, then the result is false
   * <li>Positive zero and negative zero are considered equal
   * <li>Negative infinity is conisdered less than all other values except NaN
   * <li>Positive infinity is conisdered greater than all other values except NaN
   * </ul>
   * <p>
   * This method takes the place of the <code>&gt;=</code> operator.
   *
   * @param   d1   the first <code>double</code> value to be compared.
   * @param   d2   the second <code>double</code> value to be compared.
   * @return  <code>true</code> if the first value is greater than or equal to 
   *          the second value; <code>false</code> otherwise.
   */
  public static boolean ge(long d1, long d2) {
    if (isNaN(d1) || isNaN(d2)) {
      return false;
    } else if (d1 == NEGATIVE_ZERO) {
      d1 = ZERO;
    }
    return (cmp(d1, d2) >= 0);
  }

  /**
   * Mimics <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html#compare(double, double)">Double.compare(double, double)</a>.
   * <p>
   * Note that when using this method (as well as <code>Double.compare</code>),
   * the following rules apply:
   * <ul><li>
   *		<code>NaN</code> is considered 
   *		to be equal to itself and greater than all other
   *		<code>double</code> values (including
   *		<code>POSITIVE_INFINITY</code>).
   * <li>
   *		<code>0.0</code> is considered to be greater
   *		than <code>-0.0</code>.
   * </ul>
   */
  public static int compare(long d1, long d2) {
    boolean n1 = isNaN(d1);
    boolean n2 = isNaN(d2);
    if (n1 || n2) {
      if (n1 && n2) {
        return 0;
      }
      return (n1 ? 1 : -1);
    }
    return cmp(d1, d2);
  }

  /**
   * Mimics <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#max(double, double)">Math.max(double, double)</a>
   */
  public static long max(long d1, long d2) {
    if (isNaN(d1) || isNaN(d2)) {
      return NaN;
    }
    return ((cmp(d1, d2) >= 0) ? d1 : d2);
  }

  /**
   * Mimics <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#min(double, double)">Math.min(double, double)</a>
   */
  public static long min(long d1, long d2) {
    if (isNaN(d1) || isNaN(d2)) {
      return NaN;
    }
    return ((cmp(d1, d2) < 0) ? d1 : d2);
  }

  private static int cmp(long d1, long d2) {
    if (d1 == d2) {
      return 0;
    } else if (d1 < 0L) {
      if (d2 < 0L) {
        return ((d1 < d2) ? 1 : -1);
      }
      return -1;
    } else if (d2 < 0) {
      return 1;
    }
    return ((d1 < d2) ? -1 : 1);
  }
  
  /////////////////////////////////////////////////////////////////////////////
  // Type conversion
  /////////////////////////////////////////////////////////////////////////////
  
  /** 
   * Convert the given <code>int</code> to a <code>double</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25214">section
   * 5.1.2 of the JLS</a>.  This is a widening primitive conversion which 
   * will result in neither a loss of magnitude nor precision.
   *
   * @param x the <code>int</code> to be converted
   * @return the <code>double</code> representation of the argument
   */
  public static long intToDouble(int x) {
    return longToDouble(x);
  }
  
  /** 
   * Convert the given <code>long</code> to a <code>double</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25214">section
   * 5.1.2 of the JLS</a>.  This is a widening primitive conversion which 
   * will not result in a loss of magnitude, but might result in a loss of
   * precision.
   *
   * @param x the <code>long</code> to be converted
   * @return the <code>double</code> representation of the argument
   */
  public static long longToDouble(long x) {
    if (x < 0) {
      return pack(true, 0, -x);
    }
    return pack(false, 0, x);
  }

  /** 
   * Convert the given <code>float</code> to a <code>double</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25214">section
   * 5.1.2 of the JLS</a>.  This is a widening primitive conversion which 
   * will result in neither a loss of magnitude nor precision.
   *
   * @param f the <code>float</code> to be converted
   * @return the <code>double</code> representation of the argument
   */
  public static long floatToDouble(int f) {
    if (MicroFloat.isNaN(f)) {
      return NaN;
    }
    boolean n = MicroFloat.unpackSign(f);
    if (MicroFloat.isZero(f)) {
      return (n ? NEGATIVE_ZERO : ZERO);
    } else if (MicroFloat.isInfinite(f)) {
      return (n ? NEGATIVE_INFINITY : POSITIVE_INFINITY);
    }
    int x = MicroFloat.unpackExponent(f);
    long m = MicroFloat.unpackMantissa(f);
    return pack(n, x, m);
  }
  
  /** 
   * Convert the given <code>double</code> to a <code>byte</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363">section
   * 5.1.3 of the JLS</a>.  This is a narrowing primitive conversion which 
   * may result in a loss of magnitude and/or precision.
   * <p>
   * Note that this is a non-intuitive conversion.  If the argument is outside
   * of the range of the byte type, the result is basically meaningless.
   *
   * @param d the <code>double</code> to be converted
   * @return the <code>byte</code> representation of the argument
   */
  public static byte byteValue(long d) {
    return (byte) intValue(d);
  }

  /** 
   * Convert the given <code>double</code> to a <code>short</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363">section
   * 5.1.3 of the JLS</a>.  This is a narrowing primitive conversion which 
   * may result in a loss of magnitude and/or precision.
   * <p>
   * Note that this is a non-intuitive conversion.  If the argument is outside
   * of the range of the short type, the result is basically meaningless.
   *
   * @param d the <code>double</code> to be converted
   * @return the <code>short</code> representation of the argument
   */
  public static short shortValue(long d) {
    return (short) intValue(d);
  }

  /** 
   * Convert the given <code>double</code> to an <code>int</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363">section
   * 5.1.3 of the JLS</a>.  This is a narrowing primitive conversion which 
   * may result in a loss of magnitude and/or precision.
   *
   * @param d the <code>double</code> to be converted
   * @return the <code>int</code> representation of the argument
   */
  public static int intValue(long d) {
    long x = longValue(d);
    if (x >= Integer.MAX_VALUE) {
      return Integer.MAX_VALUE;
    } else if (x <= Integer.MIN_VALUE) {
      return Integer.MIN_VALUE;
    }
    return (int) x;
  }

  /** 
   * Convert the given <code>double</code> to a <code>long</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363">section
   * 5.1.3 of the JLS</a>.  This is a narrowing primitive conversion which 
   * may result in a loss of magnitude and/or precision.
   *
   * @param d the <code>double</code> to be converted
   * @return the <code>long</code> representation of the argument
   */
  public static long longValue(long d) {
    if (isNaN(d)) {
      return 0;
    }
    boolean n = unpackSign(d);
    int x = unpackExponent(d);
    long m = unpackMantissa(d);
    if (x > 0) {
      if ((x >= 63) || ((m >> (63 - x)) != 0))  {
        return (n ? Long.MIN_VALUE : Long.MAX_VALUE);
      }
      m <<= x;
    } else if (x <= -53) {
      return 0;
    } else {
      m >>>= -x;
    }
    return (n ? -m : m);
  }

  /** 
   * Convert the given <code>double</code> to a <code>float</code> as would happen
   * in a casting operation specified by 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#25363">section
   * 5.1.3 of the JLS</a>.  This is a narrowing primitive conversion which 
   * may result in a loss of magnitude and/or precision.
   *
   * @param d the <code>double</code> to be converted
   * @return the <code>float</code> representation of the argument
   */
  public static int floatValue(long d) {
    return MicroFloat.doubleToFloat(d);
  }
  
  /////////////////////////////////////////////////////////////////////////////
  // Random number generation
  /////////////////////////////////////////////////////////////////////////////

  private static Random random;

  private static synchronized Random getRandom() {
    if (random == null) {
      random = new java.util.Random();
    }
    return random;
  }
  
  /**
   * Mimics <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#random()">Math.random()</a>
   */
  public static long random() {
    return pack(false, -64, getRandom().nextLong() << 11);
  }

  
  /////////////////////////////////////////////////////////////////////////////
  // Basic arithmetic
  /////////////////////////////////////////////////////////////////////////////

  /**
   * Returns the sum of the two <code>double</code> arguments according to 
   * <a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#13510">section
   * 15.18.2 of the JLS</a>.
   * <p>
   * This method takes the place of the <code>+</code> operator.
   *
   * @param   d1   the first <code>double</code> value to be summed.
   * @param   d2   the second <code>double</code> value to be summed.
   * @return  the sum of the two arguments
   */
  public static long add(long d1, long d2) {
    if (isNaN(d1) || isNaN(d2)) {

⌨️ 快捷键说明

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