Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

System.out.println(0/0); 

because the literal 0 is considered to be an int literal and divide by zero is not allowed in integer arithmetic.

The following case however doesn't throw any exception like java.lang.ArithmeticException: / by zero.

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

It displays Infinity.

The following statement produces NaN (Not a Number) with no 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 of the operands are considered to be double.


Similarly, the following statements don't throw any 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 produce the following output.

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 values that represent positive infinity, negative infinity, not a number (NaN)...

like image 960
Tiny Avatar asked Oct 18 '12 12:10

Tiny


People also ask

What happens when you divide a floating point value by 0 in Java?

Java will not throw an exception if you divide by float zero. It will detect a run-time error only if you divide by integer zero not double zero. If you divide by 0.0, the result will be INFINITY.

How do you fix a floating point division by zero?

Floating point error means that there is a division by a zero value in your code. It can be a variable, input, or a function in use that returns zero value. You need to identify this variable/input/function and make sure that the returned value is not zero.

Why is divide by zero an error in programming?

Any number divided by zero gives the answer “equal to infinity.” Unfortunately, no data structure in the world of programming can store an infinite amount of data. Hence, if any number is divided by zero, we get the arithmetic exception .

How do you fix a divide by zero error in Java?

Divide by Floating Point Zero Exception in Java lang. ArithmeticException , the following case does not produce an exception when divided by zero. It expresses the infinite . Copy int x = 0; double y = 3.2500; System.


2 Answers

In short, that's the way it's specified in the IEEE-754 standard, which is what Java's Floating-Point Operations are based on.

Why doesn't division by zero (or overflow, or underflow) stop the program or trigger an error? Why does a standard on numbers include "not-a-number" (NaN)?

The 754 model encourages robust programs. It is intended not only for numerical analysts but also for spreadsheet users, database systems, or even coffee pots. The propagation rules for NaNs and infinities allow inconsequential exceptions to vanish. Similarly, gradual underflow maintains error properties over a precision's range.

When exceptional situations need attention, they can be examined immediately via traps or at a convenient time via status flags. Traps can be used to stop a program, but unrecoverable situations are extremely rare. Simply stopping a program is not an option for embedded systems or network agents. More often, traps log diagnostic information or substitute valid results.

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

like image 187
Bill the Lizard Avatar answered Oct 02 '22 23:10

Bill the Lizard


It's that way because that's how IEEE 754 defined it.

Division by a floating point 0.0 yields NaN or +/-Inf, depending on whether the numerator is 0 or not.

Division by an integer 0 is not covered by IEEE 754, and generates an exception - there's no other way of indicating the error because an int can't represent NaN or Inf.

Generating an exception is similar to the (software) INT generated by a division by zero on x86 microprocessors.

like image 27
Alnitak Avatar answered Oct 03 '22 00:10

Alnitak