+ 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

5th Oct 2025, 8:00 AM
Pou10mine
Pou10mine - avatar
3 Antwoorden
+ 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.
5th Oct 2025, 8:02 AM
Ferdous 👾
Ferdous 👾 - avatar
+ 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
16th Oct 2025, 10:59 PM
zemiak
0
Bonjour
6th Oct 2025, 2:32 PM
Audrey Banza
Audrey Banza - avatar