• 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

Error : Square root a Float

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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);
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic