1

I found a small question yesterday :

If you write down the code like this and run it:

 System.out.println(1/0);

It will throw a Exception.

But if you write this:

System.out.println(1/0.0);

It will print 'Infinity' on your screen.

I want to know that ,why can 0.0 be a divisor? The answer I got online was

0.0 and 0 are not the same, 0.0 is a float, 0 is a int.

or

Computers use binary representations of numeric values, and some numbers cannot be accurately represented, such as 0.1, so 0.0 and 0 is not the same,When the value of a floating-point number is 0.0, the actual stored value is not the exact 0.0, but a value very close to zero.

My idea is that for the first statement, 0.0 and 0 are essentially the same when doing arithmetic, regardless of type, If 0 cannot be a divisor, 0.0 cannot be either. For the second statement, although the binary number can not accurately represent some numbers, but the binary can accurately represent 0.0 .

Why is it normal to use 0.0 as a divisor?

1 Answers1

0

Exact 0 is not a divisor, except in the case of 0/0

Proof:

Let p <> 0 be a number, so that

p / 0 = q

Let's multiply the equation with 0:

p = 0 * q

which is possible if and only if p is 0. But then q can be anything, so dividing a non-zero number with 0 is undefined, while dividing 0 with 0 is ambivalent.

A number infinitely close, but not equal to 0 is a divisor

Proof

Let p -> 0, p <> 0

In this case, dividing a normal q, not 0 number with p, which is extremely close to 0 yields infinity, since you will need to multiply p (which is infinitely close to 0) with infinity to get 1, but in that case you multiply q with infinity as well, hence the result.

0.0 represents a number infinitely close to 0.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    "*In this case, dividing a normal q, not 0 number with p, which is extremely close to 0 yields infinity,...*" or negative infinity, which is the reason you cannot analytically define division by 0. "*0.0 represents a number infinitely close to 0.*" - IEEE 754 defines "division by 0" as "invalid operation"(Sect. 7.1). Furthermore Sect. 7.2 describes the behaviour explicitly: "*If the divisor is zero and the dividend is a finite nonzero number, then the division by zero exception shall be signaled. The result, when no trap occurs, shall be a correctly signed INFINITY.*" – Turing85 Apr 16 '18 at 15:00
  • @Turing85 I explained the division by 0 in mathematical terms and pointed out my opinion as of why things work as they do in Java. Thanks for the quote from the docs. – Lajos Arpad Apr 16 '18 at 15:07