# pls. help division by zero

deekasha gunwant

Ranch Hand

Posts: 396

posted 15 years ago

hi,

it's clearly mentioned in jaworaski (chap 3)that

so i write a small program but it is not behaving expectedly

class DivisionTest

{

public static void main(String m[])

{

int i =10;

float j = 10.0f;

System.out.println(j/0);

System.out.println(-j/0);

System.out.println(-j/(-0));

}

}

infinity

-infinity

infinity

DivisionTest.java:7: Arithmetic exception.

System.out.println(j/0);

^

DivisionTest.java:8: Arithmetic exception.

System.out.println(-j/0);

^

DivisionTest.java:9: Arithmetic exception.

System.out.println(-j/(-0));

^

3 errors

can anybody pls. throw some light why this is happening?

i'm using WinNT /jdk1.2.1

thanx in advance

deekasha

it's clearly mentioned in jaworaski (chap 3)that

an integer by zero an ArithmeticException.

positive floating-point value by zero POSITIVE_INFINITY.

negative floating point value by zero NEGATIVE_INFINITY.

Note that when the sign of zero is negative, such as -0, the sign of the result is reversed.

an integer by zero an ArithmeticException.

positive floating-point value by zero POSITIVE_INFINITY.

negative floating point value by zero NEGATIVE_INFINITY.

Note that when the sign of zero is negative, such as -0, the sign of the result is reversed.

so i write a small program but it is not behaving expectedly

class DivisionTest

{

public static void main(String m[])

{

int i =10;

float j = 10.0f;

System.out.println(j/0);

System.out.println(-j/0);

System.out.println(-j/(-0));

}

}

**according to my understanding the o/p should be**infinity

-infinity

infinity

**but the out put is**DivisionTest.java:7: Arithmetic exception.

System.out.println(j/0);

^

DivisionTest.java:8: Arithmetic exception.

System.out.println(-j/0);

^

DivisionTest.java:9: Arithmetic exception.

System.out.println(-j/(-0));

^

3 errors

can anybody pls. throw some light why this is happening?

i'm using WinNT /jdk1.2.1

thanx in advance

deekasha

Ajith Kallambella

Sheriff

Posts: 5782

posted 15 years ago

Hmmm.. that's interesting.

I am running JDK 1.2.2 and it prints correct results for me ie.,

Infinity

-Infinity

-Infinity

May be you can run a quick search on Sun's bug parade to see if this inconsistency was reported in JDK 1.2.1 and fixed later.

Ajith

I am running JDK 1.2.2 and it prints correct results for me ie.,

Infinity

-Infinity

-Infinity

May be you can run a quick search on Sun's bug parade to see if this inconsistency was reported in JDK 1.2.1 and fixed later.

Ajith

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).

Surya B

Ranch Hand

Posts: 98

posted 15 years ago

Hi

I think its got something to do with the java compiler,if i use

JDK 1.2.2 then it compiles fine,but if i use JDK1.1.6 then it gives the compile time errors.I think the same is the problem with

JDK1.2.1.Thanks.

Surya

[This message has been edited by Surya B (edited July 31, 2000).]

I think its got something to do with the java compiler,if i use

JDK 1.2.2 then it compiles fine,but if i use JDK1.1.6 then it gives the compile time errors.I think the same is the problem with

JDK1.2.1.Thanks.

Surya

[This message has been edited by Surya B (edited July 31, 2000).]

Helen Yu

Greenhorn

Posts: 29

posted 15 years ago

Hi,there:

----------------------------------------------

an integer by zero an ArithmeticException.

positive floating-point value by zero POSITIVE_INFINITY.

negative floating point value by zero NEGATIVE_INFINITY.

Note that when the sign of zero is negative, such as -0, the sign of the result is reversed.

so i write a small program but it is not behaving expectedly

class DivisionTest

{

public static void main(String m[])

{

int i =10;

float j = 10.0f;

System.out.println(j/0);

System.out.println(-j/0);

System.out.println(-j/(-0));

}

}

-----------------------------------------

I tried my 1.2.2 compiler, the result is same as above, but when you say" if sign of zero is negative, the sign of result is reserve??? The last two statements print all the same " -Infinity", why? please advise.

Thanks

----------------------------------------------

an integer by zero an ArithmeticException.

positive floating-point value by zero POSITIVE_INFINITY.

negative floating point value by zero NEGATIVE_INFINITY.

Note that when the sign of zero is negative, such as -0, the sign of the result is reversed.

so i write a small program but it is not behaving expectedly

class DivisionTest

{

public static void main(String m[])

{

int i =10;

float j = 10.0f;

System.out.println(j/0);

System.out.println(-j/0);

System.out.println(-j/(-0));

}

}

-----------------------------------------

I tried my 1.2.2 compiler, the result is same as above, but when you say" if sign of zero is negative, the sign of result is reserve??? The last two statements print all the same " -Infinity", why? please advise.

Thanks

Netla Reddy

Greenhorn

Posts: 15

thomas

Ranch Hand

Posts: 79

posted 15 years ago

To answer Helen's qn:

The primitive integer types do not differentiate between +0 and

-0. So when the integral operand is promoted to a floating-point type, it simply becomes 0.0.

10.0/0 will give Infinity

10.0/-0 will also give Infinity

But the floating-point types differentiate between +0.0 and -0.0.

10.0/0.0 will give Infinity

10.0/-0.0 will give -Infinity

The primitive integer types do not differentiate between +0 and

-0. So when the integral operand is promoted to a floating-point type, it simply becomes 0.0.

10.0/0 will give Infinity

10.0/-0 will also give Infinity

But the floating-point types differentiate between +0.0 and -0.0.

10.0/0.0 will give Infinity

10.0/-0.0 will give -Infinity