+ 2

Why ((+ or -)any numeric value)/0.0 gives (+ or -)infinity? Shouldn't it be an ArithmeticException?

16th Nov 2018, 5:16 PM
Sadman Sakib
Sadman Sakib - avatar
10 Answers
+ 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.
16th Nov 2018, 5:24 PM
Vedant Bang
Vedant Bang - avatar
+ 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.
16th Nov 2018, 5:55 PM
John Wells
John Wells - avatar
+ 1
Weird but true 🤷
16th Nov 2018, 5:24 PM
Vedant Bang
Vedant Bang - avatar
+ 1
John Wells Thanks! I get the gist!
17th Nov 2018, 10:40 PM
Sadman Sakib
Sadman Sakib - avatar
+ 1
Vedant Bang Thanks. However, could you please tell me what would create a 'NaN' value as you mentioned?
17th Nov 2018, 10:42 PM
Sadman Sakib
Sadman Sakib - avatar
+ 1
18th Nov 2018, 2:24 PM
Vedant Bang
Vedant Bang - avatar
0
Ḿổḩảḿḿẻď Ấḿiň It didn't really answer my question!
17th Nov 2018, 10:44 PM
Sadman Sakib
Sadman Sakib - avatar
0
Vedant Bang Thank you! 👍
18th Nov 2018, 2:42 PM
Sadman Sakib
Sadman Sakib - avatar
- 1
infinity is just neumaric
16th Nov 2018, 6:37 PM
Ḿổḩảḿḿẻď Ấḿiň
Ḿổḩảḿḿẻď Ấḿiň - avatar