File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArithmeticException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ArithmeticException" Watch "ArithmeticException" New topic


srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225

segment1 and segement3 is giving Arithmetic exception as expected
but segment 2 is compiling fine without any errors or exceptions and
generating and output "infinity" can anyone explain

SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274

When you divide a number by a small number, you get a large number.

When you divide a number by a smaller number, you get a larger number.

When you divide a number by the smallest possible number, you get infinity.


all events occur in real time
Craig Bayley
Ranch Hand

Joined: Sep 27, 2007
Posts: 46
Not exactly a helpful answer, since he has already indicated he expects divide by zero to cause an ArithmeticException...>What he doesn't understand is why float zero division gives infinity.

Here is a description of why you can divide by zero float value in java and "get away with it". To cut a long story short, java lets you divide by zero with float and not have a problem:

Floating-point numbers in the JVM use a radix of two. Floating-point numbers in the JVM, therefore, have the following form:

sign * mantissa * 2 exponent

The mantissa of a floating-point number in the JVM is expressed as a binary number. A normalized mantissa has its binary point (the base-two equivalent of a decimal point) just to the left of the most significant non-zero digit. Because the binary number system has just two digits -- zero and one -- the most significant digit of a normalized mantissa is always a one.

The most significant bit of a float or double is its sign bit. The mantissa occupies the 23 least significant bits of a float and the 52 least significant bits of a double. The exponent, 8 bits in a float and 11 bits in a double, sits between the sign and mantissa. The format of a float is shown below. The sign bit is shown as an "s," the exponent bits are shown as "e," and the mantissa bits are shown as "m":

Bit layout of Java float
s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm

The exponent field is interpreted in one of three ways. An exponent of all ones indicates the floating-point number has one of the special values of plus or minus infinity, or "not a number" (NaN). NaN is the result of certain operations, such as the division of zero by zero. An exponent of all zeros indicates a denormalized floating-point number. Any other exponent indicates a normalized floating-point number.

The JVM throws no exceptions as a result of any floating-point operations. Special values, such as positive and negative infinity or NaN, are returned as the result of suspicious operations such as division by zero. An exponent of all ones indicates a special floating-point value. An exponent of all ones with a mantissa whose bits are all zero indicates an infinity. The sign of the infinity is indicated by the sign bit. An exponent of all ones with any other mantissa is interpreted to mean "not a number" (NaN). The JVM always produces the same mantissa for NaN, which is all zeros except for the most significant mantissa bit that appears in the number. These values are shown for a float below:

Special float values
ValueFloat bits (sign exponent mantissa)
+Infinity0 11111111 00000000000000000000000
-Infinity1 11111111 00000000000000000000000
Jan Nowak

Joined: Aug 12, 2007
Posts: 16
Check here (see the tables) for different cases when making mathematical operations on different variable types.
[ October 08, 2007: Message edited by: Jan Nowak ]
I agree. Here's the link:
subject: ArithmeticException
It's not a secret anymore!