How to implement infinity in Java?

后端 未结 10 1331
别跟我提以往
别跟我提以往 2020-11-28 03:02

Does Java have anything to represent infinity for every numerical data type? How is it implemented such that I can do mathematical operations with it?

E.g.



        
10条回答
  •  旧时难觅i
    2020-11-28 03:22

    double supports Infinity

    double inf = Double.POSITIVE_INFINITY;
    System.out.println(inf + 5);
    System.out.println(inf - inf); // same as Double.NaN
    System.out.println(inf * -1); // same as Double.NEGATIVE_INFINITY
    

    prints

    Infinity
    NaN
    -Infinity
    

    note: Infinity - Infinity is Not A Number.

提交回复
热议问题