zoukankan      html  css  js  c++  java
  • Double和Float中的NaN和Infinity

        对于Double和Float这种浮点型来说,存在无穷大(POSTIVE_INFINITY)和无穷小(NAGATIVE_INFINITY),NaN的概念。

          注意:NaN在任何时候都不会相等。

        

    public class Test {
        public static void main(String[] args) {
            System.out.println(Double.NEGATIVE_INFINITY==Float.NEGATIVE_INFINITY); // TRUE
                System.out.println(Double.NEGATIVE_INFINITY==Double.NEGATIVE_INFINITY); // TRUE
                System.out.println(Float.NEGATIVE_INFINITY==Float.NEGATIVE_INFINITY); // TRUE
                System.out.println(Float.NaN==Float.NaN); // FALSE
                System.out.println(Double.NaN==Double.NaN); // FALSE
        }
    }

         那么什么时候会出现这种情况呢?下图说明:

         

        如何去判断一个数是不是infinity或者NaN呢?

        1.isInifinite() 用于判断一个数是不是infinity(无穷大,包括正无穷和负无穷);

          

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

         

          2.isNaN() 用于判断一个数是否是一个数;

     

     1 /**
     2      * Returns {@code true} if the specified number is a
     3      * Not-a-Number (NaN) value, {@code false} otherwise.
     4      *
     5      * @param   v   the value to be tested.
     6      * @return  {@code true} if the value of the argument is NaN;
     7      *          {@code false} otherwise.
     8      */
     9 public static boolean isNaN(double v) {
    10     return (v != v);
    11 }
  • 相关阅读:
    web框架本质及第一个Django实例
    jQuery练习题HTML文件
    jQuery快速入门
    前端之JS
    前端基础之HTML
    前端基础之css
    并发编程之 协程
    Linux目录结构详解
    第三周 time库
    网络打印机拒绝访问,无法连接处理方法汇总
  • 原文地址:https://www.cnblogs.com/heureuxl/p/13432416.html
Copyright © 2011-2022 走看看