• 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

Problem with casting Infinity??

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers!!
My problem is why infinity be casted, means how is it possible. Second problem is after casting how the result be determined? What is the rule for this type of casting?
Please help me
class q1{
public static void main(String args[]){
double d1 = -9.0;
double d2 = 0.0;
d1 = d1/d2; // d1 = -Infinity
System.out.println((byte) d1); //print 0
System.out.println((int) d1); //print -2147483648
System.out.println((long) d1); //print -9223372036854775808
}
}
regards
Farhan

[This message has been edited by Farhan Tariq (edited June 02, 2001).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi farhan,
When numeric value except float and double is divided by 0 it will give u the ArtihmaticException but float and double extend till -ve/+ve infinity the 0.0/0.0 or eg. 6.0/0.0 will give u Infinity intead of exception
khaki
-------------------
class q1{
public static void main(String args[]){
double d1 = -9.0;
double d2 = 0.0;
d1 = d1/d2; // d1 = -Infinity
System.out.println((byte) d1); //print 0
System.out.println((int) d1); //print -2147483648
System.out.println((long) d1); //print -9223372036854775808
}
}
regards
Farhan

------------------
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!!
My question was not based on floating point dividing by zero. My question still is when we divide any floating point by 0 it will give +ve infinity or -ve infinity, but when we cast that infinity into byte, short, int or long, how it will be cast? And how JVM determined that after casting the infinity "that" particular value "must" be generated?
thanx
Farhan
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The special values NaN, POSITIVE_INFINITY etc. all have a binary representation that is recognized by the floating point math operations. When you cast to an integer type, you are getting the integer equivalent of that binary representation.
Bill

------------------
author of:
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI!!
Thanx for reply to me. But is that a "hard rule", casting -ve infinity to long results all 64 bits to one and positive infinity to long results 63 bits to 1 and last bit to zero. Same is the case with int type. But if we cast it into byte or short the result will not be in that manner, instead of getting all bits to one we get 0 for +ve infinity and -1 for -ve infinity. Why the rule changes for byte or short, what is the reason behind this?
Reagards
Farhan
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farhan.
This is the same problem with me.The rule applies to long & int logically but fails in short & int case.
Regards,
Hassan
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read what I said again. The special float and double values have unique bit pattern representations. NOT all 1 or all 0, a special bit pattern, that is recognized by the math operators for float and double.
If your calculation could result in one of the special values, it is up to you, the programmer, to check for them before attempting to cast to an integer type.
 
Farhan Tariq
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks William!!
I got that, thanx for your help
Regards
Farhan
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic