Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

1.0/0.0 -1.0/0.0 0.0/0.0

 
anilkuj
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someone plz answer this question, i
ll be really gr8ful....

thanx
amit
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic