• 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

floating point arthmetic

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i try to compile the following code, it gives compilation error stating that "Incompatible type for declaration. Explicit cast needed to convert int to byte". This is perfectly understandable as arthemetic calculation is always at least an INT
"CODE"
class B {
public static void main (String[] args) {
byte b1=5;
byte b2=6;
byte b3=b1+b2;
}
}
"CODE"
In the similar lines , i thought even the below given code should also give the error as floating points are double by default.
"CODE"
class B {

public static void main (String[] args) {
float f1=23.45F;
float f2=23.55F;
float f3=f1+f2;
}
}
"CODE"
But it gets compiled properly. Why this is?
Someone please help!!
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Nope, If you add/multiply/compare/equate two floating point numbers the result is still a floating point number. Checkout some operend promotion examples from www.janeg.ca:
byte = byte + byte; // found int, required byte
int = float + int; // found float, required int
long = float + long; // found float, required long
float = double + float; // found double, required float
Hope this helps,
Sindhur.
reply
    Bookmark Topic Watch Topic
  • New Topic