This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 1.0/0.0     -1.0/0.0      0.0/0.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "1.0/0.0     -1.0/0.0      0.0/0.0 " Watch "1.0/0.0     -1.0/0.0      0.0/0.0 " New topic
Author

1.0/0.0 -1.0/0.0 0.0/0.0

anilkuj
Greenhorn

Joined: Sep 18, 2000
Posts: 22
d1=-1.0;
d2=0;
byte b=0;
b=(byte)d1/d2;
System.out.println(b);


here the value of b is o.

d1=1.0;
d2=0;
byte b=0;
b=(byte)d1/d2;
System.out.println(b);

here the value of b is -1.

d1=0.0;
d2=0;
byte b=0;
b=(byte)d1/d2;
System.out.println(b);

here the value of b is 0 again.


Anybody explain these resulsts and give me the exact bit rep. of +ive infinity, -ive infinty, and NAN.I am totally confused.

Thanx

aNiL
[ May 21, 2005: Message edited by: Barry Gaunt ]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Your are on the right track i realizing that the byte represntations of -infinity, +infinity and NaN are responsible for your results. These values are for float and double only and not for any of the intragal(-5 sp) types. I don't know of any easy way to find out the bit reresentation of these values in Java and don't know their exact reprsentation. There are methods in Double and Float to test for these conditions.
Such as
double d1=-1.0;
int d2=0;
byte b=0;
double d3 = d1/d2;
if(Double.isNaN(d3) | | Double.isInfinite(d3))
{
System.out.println(d3);
}
else
{
b=(byte)d3;
System.out.println(b);
}
Hope this helps


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12758
    
    5
A weird thing about NaN values is that you can't test a float or double value against NaN with == it always returns false. You must use the Double.isNaN() or Float.isNaN() method.
Bill
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

I take the oopotunity to take the Dead out of the Graveyard

just needed an explanation how Java is implementing this
plz refer this code:


thanx
amit
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Floating-point value is converted to byte in two steps. First the value is converted to int then the int is converted to byte.

NaN is converted to int 0 then to byte 0.
Neg. infinity is converted to Integer.MIN_VALUE (0x80000000) which is converted to byte 0.
Pos. infinity is converted to Integer.MAX_Value (0x7FFFFFFF) which is converted to byte -1 (0xFF).
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
is it always like this that whenever we convert any floating-point number its first converted to int then to byte...???

thanx
amit
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
someone plz answer this question, i
ll be really gr8ful....

thanx
amit
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by Carl Trusiak:
Your are on the right track i realizing that the byte represntations of -infinity, +infinity and NaN are responsible for your results. These values are for float and double only and not for any of the intragal(-5 sp) types.

To see the bit representations of some NaN values, as well as MAX_VALUE and MIN_VALUE for float and double, you can run this code:

[ May 21, 2005: Message edited by: Joe Sanowitz ]

SCJA 1.0 (98%), SCJP 1.4 (98%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 1.0/0.0 -1.0/0.0 0.0/0.0
 
Similar Threads
Casting Related Doubt
Casting!!!!!!!!
ques
casting problem
Mock question from Jaworski