How to implement infinity in Java?

后端 未结 10 1325
别跟我提以往
别跟我提以往 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
    慢半拍i (楼主)
    2020-11-28 03:37

    To use Infinity, you can use Double which supports Infinity: -

        System.out.println(Double.POSITIVE_INFINITY);
        System.out.println(Double.POSITIVE_INFINITY * -1);
        System.out.println(Double.NEGATIVE_INFINITY);
    
        System.out.println(Double.POSITIVE_INFINITY - Double.NEGATIVE_INFINITY);
        System.out.println(Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY);
    

    OUTPUT: -

    Infinity
    -Infinity
    -Infinity
    
    Infinity 
    NaN
    

提交回复
热议问题