+ 2
Why java math Is not mathing?
If I divide a number in java by zero, it outputs an error. Makes sense! Example (5/0) But if I divide an double by zero, it output "infinite". Why? Example (5.0/0) https://sololearn.com/compiler-playground/cZUb9M516Rke/?ref=app
3 Réponses
+ 6
Java handles integer and floating point division by zero differently:
Integer division by zero (5/0) throws ArithmeticException because integers have no way to represent infinity
Floating point division by zero (5.0/0) returns Infinity because the IEEE 754 floating point standard (which Java follows) has special values to represent positive/negative infinity
This happens because 5.0 is a double, and floating point types have built in representations for infinity, while integers do not.
+ 1
where it is useful
for (double d = -0.01; d <=0.01 ; d += 0.001) {
double result = 1.0 / d;
System.out.println(result);
}
-333.3333333333333
-500.0
-1000.0
Infinity
1000.0
500.0
333.3333333333333
0
Bonjour



