File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pls. help division by zero 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 "pls. help division by zero" Watch "pls. help division by zero" New topic
Author

pls. help division by zero

deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi,

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.

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

Joined: Mar 17, 2000
Posts: 5782
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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Surya B
Ranch Hand

Joined: May 10, 2000
Posts: 98
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).]
Helen Yu
Greenhorn

Joined: Jul 13, 2000
Posts: 29
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


hyby2000@yahoo.com
Netla Reddy
Greenhorn

Joined: Jun 18, 2000
Posts: 15
Hi,
I am using JDK 1.2 & WIN NT and it prints correct results for me ie.,
Infinity
-Infinity
-Infinity
Thanks
[This message has been edited by Netla Reddy (edited July 31, 2000).]
thomas
Ranch Hand

Joined: May 26, 2002
Posts: 79
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pls. help division by zero
 
Similar Threads
Floating Point Number and dividebyzero arithmatic exception
need help
Double
Division by zero floating point question
please explain this Assertions problem