epsilon.java

来自「一个一元曲线多项式数值演示例子」· Java 代码 · 共 44 行

JAVA
44
字号
package numbercruncher.mathutils;

/**
 * Compute the machine epsilon for the float and double types,
 * the largest positive floating-point value that, when added to 1,
 * results in a value equal to 1 due to roundoff.
 */
public final class Epsilon {
  private static final float floatEpsilon;
  private static final double doubleEpsilon;

  static {

    // Loop to compute the float epsilon value.
    float fTemp = 0.5f;
    while (1 + fTemp > 1) {
      fTemp /= 2;
    }
    floatEpsilon = fTemp;

    // Loop to compute the double epsilon value.
    double dTemp = 0.5;
    while (1 + dTemp > 1) {
      dTemp /= 2;
    }
    doubleEpsilon = dTemp;
  };

  /**
   * Return the float epsilon value.
   * @returns the value
   */
  public static float floatValue() {
    return floatEpsilon;
  }

  /**
   * Return the double epsilon value.
   * @returns the value
   */
  public static double doubleValue() {
    return doubleEpsilon;
  }
}

⌨️ 快捷键说明

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