infinity

How to implement infinity in Java?

为君一笑 提交于 2019-11-26 11:54:50
问题 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

In JavaScript, why does zero divided by zero return NaN, but any other divided by zero return Infinity?

允我心安 提交于 2019-11-26 11:20:12
问题 It seems to me that the code console.log(1 / 0) should return NaN , but instead it returns Infinity . However this code: console.log(0 / 0) does return NaN . Can someone help me to understand the reasoning for this functionality? Not only does it seem to be inconsistent, it also seems to be wrong, in the case of x / 0 where x !== 0 回答1: Because that's how floating-point is defined (more generally than just Javascript). See for example: http://en.wikipedia.org/wiki/Floating-point#Infinities

Adding to Number.MAX_VALUE

旧巷老猫 提交于 2019-11-26 07:49:04
问题 The answer to this question may be painfully obvious but I can\'t find it in the Mozilla docs nor on Google from a cursory search. If you have some code like this Number.MAX_VALUE + 1; // Infinity, right? Number.MIN_VALUE - 1; // -Infinity, right? Then I would expect adding anything to Number.MAX_VALUE would push it over to Infinity . The result is just Number.MAX_VALUE spat right back at me. However, when playing around in the Chrome JS console, I noticed that it didn\'t actually become