• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

castin primitives problem

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
given the code:
System.out.println("(int)Float.NEGATIVE_INFINITY==" +(int)Float.NEGATIVE_INFINITY);
// A float value too big to fit gives largest int value:
System.out.println("(int)-1e20f==" + (int)-1e20f);
System.out.println("(int)Float.MIN_VALUE== "+ (int)Float.MIN_VALUE);
System.out.println("(int)Float.MAX_VALUE== "+ (int)Float.MAX_VALUE);
when I run it I get the following output:
int)Float.NEGATIVE_INFINITY==-2147483648
(int)-1e20f==-2147483648
(int)Float.MIN_VALUE== 0
(int)Float.MAX_VALUE== 2147483647
Now, why do I get zero on the third line?
it should be the same as line 1 an line 2 the smallest value of int
I used the j2sdk1.4.2.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some facts (don't ask me why, it is the JAVA's way to deal with numbers):
  • Float.NEGATIVE_INFINITY is equal to Double.NEGATIVE_INFINITY
  • When casting Float/Double.NEGATIVE_INFINITY to integer, the result will be equal to Integer.MIN_VALUE.
  • Integer.MIN_VALUE return the smallest negative number an integer can hold.
  • Float.NEGATIVE_INFINITY returns the smallest negative number float can hold.
  • Float.MIN_VALUE return the smallest positive number (greater than zero) float can hold.


  • Hope this answers your question.
    [ February 07, 2004: Message edited by: Vicken Karaoghlanian ]
     
    Dan Andrei
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanx a bunch, your last line saved the day
    so I should use :
    System.out.println("(int)Float.MIN_VALUE== "+ (int)((-1)*Float.MAX_VALUE));
    instead of
    System.out.println("(int)Float.MIN_VALUE== "+ (int)(*Float.MIN_VALUE));
    to get the desired result, that is the smallest value of int
     
    Ranch Hand
    Posts: 59
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    good analysis.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic