finity和infinity

infinity:无穷的; finity:有限的; 看了下jdk1.8中Double的API,有两个方法:测试

/**
     * Returns {@code true} if the specified number is infinitely
     * large in magnitude, {@code false} otherwise.
     *
     * @param   v   the value to be tested.
     * @return  {@code true} if the value of the argument is positive
     *          infinity or negative infinity; {@code false} otherwise.
     */
    public static boolean isInfinite(double v) {
        return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
    }

    /**
     * Returns {@code true} if the argument is a finite floating-point
     * value; returns {@code false} otherwise (for NaN and infinity
     * arguments).
     *
     * @param d the {@code double} value to be tested
     * @return {@code true} if the argument is a finite
     * floating-point value, {@code false} otherwise.
     * @since 1.8
     */
    public static boolean isFinite(double d) {
        return Math.abs(d) <= DoubleConsts.MAX_VALUE;
    }

而后写了个测试类,实验了下code

public class UnitTest {
  @Test
  public void testDouble(){
    double d=0.00/20.00;    
    System.out.println(d+"是否为有限"+Double.isFinite(d));
    double e=20.00/0.00;
    System.out.println(e+"是否为无限:"+Double.isInfinite(e));

     DecimalFormat df = new DecimalFormat("0.00");//格式化小数   
    String s = df.format(e);
    System.out.println(s);
    System.out.println(df.format(d));
  }

若是有用到求百分比的,应该判断下是否为无限值,这样会避免一些显示问题;orm

  • 虽然double是原生类,但JVM实际上是把它当成引入类使用的.全部输出e为'infinity'
相关文章
相关标签/搜索