+ 2
Why ((+ or -)any numeric value)/0.0 gives (+ or -)infinity? Shouldn't it be an ArithmeticException?
10 Antworten
+ 2
That's one good question.
In Java, for floating point numbers, certain special values have been defined, such as 'NaN' and infinity, for operations which may not give a simple mathematical result.
Hence dividing by 0.0 will give you infinity (it's been defined that way)
However if you try it with 0, i.e. try 1/0, you'll get an arithmetic exception, because such values haven't been defined for integers.
+ 6
The picking of infinity is not just a random choice. If you divide by smaller and smaller numbers, you see a trend of approaching infinity so making 0 be infinity is not that huge of step.
+ 1
Weird but true 🤷
+ 1
John Wells Thanks! I get the gist!
+ 1
Vedant Bang Thanks. However, could you please tell me what would create a 'NaN' value as you mentioned?
+ 1
Sadman Sakib Here is a code which ends up in NaN
https://code.sololearn.com/cW6yGYPiiUWp/?ref=app
0
Ḿổḩảḿḿẻď Ấḿiň It didn't really answer my question!
0
Vedant Bang Thank you! 👍
- 1
infinity is just neumaric