+ 4
[Java] Dividing by zero did not give error
For the following code: double x=-1.0/0.0; System.out.println(10.0/x); The output was -0.0 My questions are 1) Why doesn't the first line itself give any error? and 2) Why is there a negative sign with zero? Is 0.0 different from -0.0?? If this question has been asked before, please provide a link. Thanks in advance!
3 Réponses
+ 10
That's because JVM only throws arithmetic exception when divided by int 0
If you divide by floating-point 0.0 result will be infinity.
Here the sign is "-"
If you try to print x,
System.out.println(x);
You'll see it results in -Infinity
Now when you divide any number by -infinity it'll result in -0.0
https://www.javamadesoeasy.com/2015/12/when-dividing-by-zero-does-not-throw.html?m=1
+ 4
Basically JVM doesn't throw an exception for float pointing number . It gives output in-form of infinty/-infinty or NaN
Eg:-
Float a = 0.0f
Float b = a
System.out.print(b/a);
//For this code output will be "NaN"
Float a = 1.0f
Float b = 0.0f
System.out.print(a/b);
//For this output will be "infinity"
Float a = -1.0f
Float b = 0.0f
System.out.print(a/b);
//For this output will be "-infinity"
+ 3
Oh okay!! Thanks!