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

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

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

posted

0

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

posted

0

someone plz answer this question, i ll be really gr8ful....

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: