File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float.POSITIVE_INFINITY...?

 
Prashant Neginahal
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
int j = (int)Float.POSITIVE_INFINITY;
j==Integer.MAX_VALUE; It gives true
then,
short i = (short)Float.POSITIVE_INFINITY;
i == Short.MAX_VALUE;It gives false
I really not understanding these two cases, will anyone please explian me.?
-----------
Prashant
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of casts to integer types other than long or int. They do not work the way you might think.
When you convert a float (or double) to a byte, short, or char, the number is first converted to an int and then truncated to a short.
So what happens in this case is that Float.POSITIVE_INFINITY is converted to Integer.MAX_VALUE (01111111111111111111111111111111), then this value is simply truncated to a 16 bit short value (1111111111111111). The result is therefore -1 rather than Short.MAX_VALUE (0111111111111111).
The details are in the JLS section 5.1.3 Narrowing Primitive Conversions.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic