How to implement infinity in Java?
问题 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. int myInf = infinity; //However it is done myInf + 5; //returns infinity myInf*(-1); //returns negative infinity I have tried using very large numbers, but I want a proper, easy solution. 回答1: double supports Infinity double inf = Double.POSITIVE_INFINITY; System.out.println(inf + 5); System.out.println(inf - inf); // same as Double.NaN