srinivas sridaragaddi

Ranch Hand

Posts: 225

posted 9 years ago

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

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

Posts: 1274

Craig Bayley

Ranch Hand

Posts: 46

posted 9 years ago

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:

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

Greenhorn

Posts: 16

posted 9 years ago

Check here (see the tables) for different cases when making mathematical operations on different variable types.

[ October 08, 2007: Message edited by: Jan Nowak ]

[ October 08, 2007: Message edited by: Jan Nowak ]

It is sorta covered in the JavaRanch Style Guide. |