aspose file tools*
The moose likes Beginning Java and the fly likes Error : Square root a Float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error : Square root a Float" Watch "Error : Square root a Float" New topic
Author

Error : Square root a Float

Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
Posts: 27
222: incompatible types
found : double
required: java.lang.Float
VL = Math.sqrt(VL); //Square root


Can't you square root a number if it is a float?
(VL is declared as a float.)
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Conrad McLaughlin:
222: incompatible types
found : double
required: java.lang.Float
VL = Math.sqrt(VL); //Square root


Can't you square root a number if it is a float?
(VL is declared as a float.)


Sure you can. What the compiler is complaining about is assigning a double to a float, so cast it:

VL = (float) Math.sqrt(VL);


There is no emoticon for what I am feeling!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Conrad McLaughlin:
...VL is declared as a float...

I don't think so. It looks like VL is declared as a Float (object) -- not a float (primitive). The cast will work, but there's a lot of boxing/unboxing going on here.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Error : Square root a Float