• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Casting Related Doubt  RSS feed

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai ranchers,

i know the fact that when a double number is divided by 0 it doesnt result in exception but it results in value of Infinty...
but the problem i have is when i try to type cast this value of infinity to byte...it results in value of -1 ....
sample code...

double d1 = 1.0;
double d2 = 0.0;
d1= d1/d2; //d1 results in Infinity
byte b = (byte) d1; //b gets assisned -1

can u help me understand y it gets this value assigned..
thnks in advance
 
Sheriff
Posts: 21747
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how infinity is defined in bits, but I'm thinking it's something like 0111 1111 1111 ... 1111 1111 1111. If you cast it to a byte, you cut off everything except for the last part: 1111 1111. This is -1.
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!