Why dividing by zero with floating point numbers (or double precision) does not raise java.lang.ArithmeticException: / by zero in Java

The following statement throws java.lang.ArithmeticException: / by zero as obvious.

 System.out.println(0/0); 

because literal 0 is considered an int literal and division by zero is not allowed in integer arithmetic.

The following case, however, does not throw an exception, for example java.lang.ArithmeticException: / by zero .

 int a = 0; double b = 6.199; System.out.println((b/a)); 

Infinity displayed.

The following statement creates NaN (Not a Number) without exception.

 System.out.println(0D/0); //or 0.0/0, or 0.0/0.0 or 0/0.0 - floating point arithmetic. 

In this case, both operands are considered double.




Similarly, the following operators do not make an exception.

 double div1 = 0D/0; //or 0D/0D double div2 = 0/0D; //or 0D/0D System.out.printf("div1 = %s : div2 = %s%n", div1, div2); System.out.printf("div1 == div2 : %b%n", div1 == div2); System.out.printf("div1 == div1 : %b%n", div1 == div1); System.out.printf("div2 == div2 : %b%n", div2 == div2); System.out.printf("Double.NaN == Double.NaN : %b%n", Double.NaN == Double.NaN); System.out.printf("Float.NaN == Float.NaN : %b%n", Float.NaN == Float.NaN); 

They make the following conclusion.

 div1 = NaN : div2 = NaN div1 == div2 : false div1 == div1 : false div2 == div2 : false Double.NaN == Double.NaN : false Float.NaN == Float.NaN : false 

They all return false. Why is this operation (division by zero) allowed with floating point or double precision numbers?




By the way, I can understand that floating point numbers (double precision numbers) have their own values, which are positive infinity, negative infinity, and not a number ( NaN ) ...

+47
java arithmeticexception dividebyzeroexception
Oct 18 '12 at 12:05
source share
6 answers

In short, the way that he specified in the IEEE-754 standard, which is Java floating point operations , is based on.

Why doesn't dividing by zero (or overflow or underflow) stop the program or cause an error? Why does the number standard include "not-a-number" (NaN)?

Model 754 encourages reliable programs. It is intended not only for numerical analysts, but also for users of spreadsheets, database systems, or even coffee pots. Propagation rules for NaNs and infinities allow unrealizable exceptions to vanish. Similarly, gradual underflow maintains error properties in the precision range.

When exceptions need attention, they can be checked immediately through traps or at convenient times through status flags. Traps can be used to stop a program, but fatal situations are extremely rare. Ease of stopping a program is not an option for embedded systems or network agents. Most often, traps record diagnostic information or replace actual results.

Flags offer both predictable control flow and speed. Their use requires the programmer to be aware of exceptional conditions, but the stickiness flag allows programmers to defer handling of exceptional conditions until necessary.

+45
Oct 18 '12 at 12:16
source share

This is because it defined IEEE 754.

A floating-point unit of 0.0 gives NaN or +/- Inf, depending on whether the numerator is 0 or not.

Division by integer 0 does not extend to IEEE 754 and throws an exception - there is no other way to indicate an error, since int cannot represent NaN or Inf .

Throwing an exception is similar to ( int ) software generated by dividing by zero on x86 microprocessors.

+14
Oct 18 '12 at 12:16
source share

To infinity and beyond

 class DoubleDivision { public static void main(String[] args) { System.out.println(5.0/0.0); } } 

The above code and the above snippets give infinity .

Why Java uses Double to represent decimal places. Binary cannot fully represent a number, it can only represent an approximation, and therefore Javas cannot double.

Imagine a number incredibly close to zero. If you know the calculus, imagine a limit of zero. The variable will approach zero to some imaginary tiny distance, but it will never be exactly equal. You can imagine it, right? Well, suppose a number requires so much accuracy to represent Java, it refuses and calls it 0.0 , because it has no good alternative. What is happening here. Any regular number divided by a super close number to zero is basically infinity . Try: 5 / (10^-100) .

Also see: Math Errors Special Floating-Point Values for more information :)

Related question: why 1/0 gives an error, but 1.0 / 0/0 gives inf

UPDATE: INT has no infinity and NaN value in the set, while float has infinity and NaN value. (According to IEEE 754 standards that comply with java)

+8
Oct 18
source share

When there is division by zero, the computer cannot create a representation of the result as a number. The computer should signal that the result is not a number.

For floating point values, it can give a special value for an odd number, since there are 32-bit (for float ) and 64-bit (for double ) bit patterns that are not a number and, therefore, can be interpreted as a non-number ( NaN ).

For integer values ​​that use the general double complement scheme (as Java requires), all 32-bit (for int ) and 64-bit (for long ) bit patterns are numbers. Therefore, the computer cannot report the problem using a sentinel device. He must somehow report the problem "out of range." Throwing an exception is an out of range method chosen for Java.

+5
Oct 18 '12 at 12:16
source share

Java follows the IEEE 754 standard, which defines default return values ​​for dividing by zero. http://en.wikipedia.org/wiki/IEEE_754#Exception_handling

Division by zero (the operation with finite operands gives an exact infinite result, for example, 1/0 or log (0)) (by default it returns ± infinity).

+2
Oct 18 '12 at 12:15
source share
 import java.util.Scanner; public class Division { public static void main(String[] args) { int a, b; float result; Scanner input = new Scanner(System.in); System.out.println("Enter value for a : "); a = input.nextInt(); System.out.println("Enter value for b : "); b = input.nextInt(); try { /* here we used "if clause" because result will be in float note: Here result is in float, so JVM Cant Catch Exception. if we Try to Divide with Zero with Float Values It gives Output as Divided by Zero is Infinity */ if (b != 0) { result = (float) a / b; System.out.println("Result of " + a + " divided by " + b + " is : " + result); } else // here result will be in integer so Jvm can easily Catch The Exception { result = a / b; } } catch (ArithmeticException e) { System.out.println("Sorry Division By Zero Is Not Possible"); } } } /* Ouput : Enter value for a :2 Enter value for b : 7 Result of 2 divided by 7 is : 0.2857143 */ /* Ouput : Enter value for a : 16 Enter value for b : 0 Sorry Division By Zero Is Not Possible */ 
+1
Sep 18 '14 at 11:41
source share



All Articles