3

I had already searched through different questions on this topic but not get a clear idea. Check this code:

class Test{
    public static void main(String[] s){
        int a=5;
        float b=(float)a/0;
        System.out.print(b);
    }
}

the output is Infinity. But the thing I'm not getting is a is an int and a/0 must throw an exception. So how can it show output Infinity?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • Because [infinity](http://steve.hollasch.net/cgindex/coding/ieeefloat.html#infinity) is a valid floating point number – Justin Sep 08 '11 at 15:45

8 Answers8

8

The reason is that

(float)a/0;

is interpreted as

((float)a)/0;

and not

(float)(a/0);

so you actually are converting a to a float before doing the division, not doing an integer division and then converting the result.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    why dividing a `float` number by `0` don't have exception and results `infinity` – Mohammad Faisal Sep 08 '11 at 01:49
  • @Mohammad Faisal The result is actually a number but the number is so large that when you convert it into a string it comes up with `infinity` – fireshadow52 Sep 08 '11 at 01:50
  • 3
    @Mohammad Faisal- This is an arbitrary decision that the designers of the computer hardware that does floating-point arithmetic made many years ago. They could just as easily have chosen to cause errors in this case, but they opted not to. – templatetypedef Sep 08 '11 at 01:51
  • 6
    @fireshadow: No, the result is exactly "infinity". – R. Martinho Fernandes Sep 08 '11 at 01:53
  • @fireshadow: because it is specified that division by 0 yields infinity. – R. Martinho Fernandes Sep 08 '11 at 01:55
  • @fireshadow52- The IEEE floating-point formats include values for +infinity, -infinity, and NaN in addition to the standard real values. It is possible, therefore, to have a `float` equal to infinity. – templatetypedef Sep 08 '11 at 01:55
  • @R. Martino Fernandes OK I get it. Thanks. :) – fireshadow52 Sep 08 '11 at 01:56
  • 1
    Addition: the Java Language Specification (http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html) defines the two operators you use in sections 15.16 and 15.17. These sections are generally ordered by their precedence, so since typecasts are defined in section 15.16, they have higher precedence than the division from section 15.17. – Roland Illig Sep 08 '11 at 02:01
4

You are not dividing an integer by zero. You're dividing a float by zero, because your expression is equivalent to:

float b=((float)a)/0;

If you force the division to occur with only integers instead, like in the following example, the expected ArithmeticException will be thrown.

float b=(float)(a/0);
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
2

All floating-point computations follow the IEEE 754 specification. In particular, there are three special floating-point values to denote overflows and errors:

• Positive infinity • Negative infinity • NaN (not a number)

For example, the result of dividing a positive number by 0 is positive infinity. Computing 0/0 or the square root of a negative number yields NaN.

see also

CAUTION: Floating-point numbers are not suitable for financial calculation in which roundoff errors cannot be tolerated. For example, the command System.out.println(2.0 - 1.1) prints 0.8999999999999999, not 0.9 as you would expect. Such roundoff errors are caused by the fact that floating-point numbers are represented in the binary number system. There is no precise binary representation of the fraction 1/10, just as there is no accurate representation of the fraction 1/3 in the decimal system. If you need precise numerical computations without roundoff errors, use the BigDecimal class, which is introduced later in this chapter.

from core Java Volume 1 chapter 3

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
confucius
  • 13,127
  • 10
  • 47
  • 66
1

a is an int, except you cast it to a float at the time the division occurs. Note that the cast has higher precedence than division - with brackets for clarity it would be:

float b = ((float) a)/0;

So while a is an int, you're doing floating point division.

Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
1

This is because Java doesn't allow division by zero with ints and it does with floating-point values.

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
1

Infinity is produced if a floating point operation creates such a large floating-point number that it cannot be represented normally.

The cast to float of a generates a automatic cast of 0 to float aswell

Ben
  • 13,297
  • 4
  • 47
  • 68
1

Because you're casting a to a float, then dividing by zero. Floats have +/- infinity.

http://www.velocityreviews.com/forums/t137207-division-by-zero-float-vs-int.html

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

The binary / operator performs division, producing the quotient of its operands. The left-hand operand is the dividend and the right-hand operand is the divisor.

Integer division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d·q||n|; moreover, q is positive when |n||d| and n and d have the same sign, but q is negative when |n||d| and n and d have opposite signs. There is one special case that does not satisfy this rule: if the dividend is the negative integer of largest possible magnitude for its type, and the divisor is -1, then integer overflow occurs and the result is equal to the dividend. Despite the overflow, no exception is thrown in this case. On the other hand, if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.

The result of a floating-point division is determined by the specification of IEEE arithmetic:

If either operand is NaN, the result is NaN.
If the result is not NaN, the sign of the result is positive if both operands have the same sign, negative if the operands have different signs.
Division of an infinity by an infinity results in NaN.
Division of an infinity by a finite value results in a signed infinity. The sign is determined by the rule stated above.
Division of a finite value by an infinity results in a signed zero. The sign is determined by the rule stated above.
Division of a zero by a zero results in NaN; division of zero by any other finite value results in a signed zero. The sign is determined by the rule stated above.
Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.
In the remaining cases, where neither an infinity nor NaN is involved, the exact mathematical quotient is computed. A floating-point value set is then chosen:
    If the division expression is FP-strict (§15.4):
        If the type of the division expression is float, then the float value set must be chosen.
        If the type of the division expression is double, then the double value set must be chosen. 
    If the division expression is not FP-strict:
        If the type of the division expression is float, then either the float value set or the float-extended-exponent value set may be chosen, at the whim of the implementation.
        If the type of the division expression is double, then either the double value set or the double-extended-exponent value set may be chosen, at the whim of the implementation. 

Next, a value must be chosen from the chosen value set to represent the quotient. If the magnitude of the quotient is too large to represent, we say the operation overflows; the result is then an infinity of appropriate sign. Otherwise, the quotient is rounded to the nearest value in the chosen value set using IEEE 754 round-to-nearest mode. The Java programming language requires support of gradual underflow as defined by IEEE 754 (§4.2.4). 

Despite the fact that overflow, underflow, division by zero, or loss of information may occur, evaluation of a floating-point division operator / never throws a run-time exception.

This can be verified at: http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.17.2

reevesy
  • 3,452
  • 1
  • 26
  • 23
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117