• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

float or double

 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
when 1.0 is passed as parameter to abs method Math.abs(1.0), is 1.0 considered as float value or a double value.
thanks in adv
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.0 is by default a double literal. So the method Math.abs(double) will be chosen. And you will get a double as the returned value.
So float f = Math.abs(1.0) will not compile without casting the returned value to a float.
After saying that I better go try it to see if I'm correct.
-Seey you later.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double
To pass a float, you would have to use 1.0f
Cheers
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no no...it will compile bcoz abs() method is overloaded and one of which takes double N returns double value.
Thankz for replying.....
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep it's gottabe: float f = (float)Math.abs(1.0);
Have a happy new Year.
 
Ben Ritchie
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or float f = Math.abs(1.0f);
I don't like casting primitives unless strictly necessary
[ December 20, 2002: Message edited by: Ben Ritchie ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic