Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
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:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# creating a calculator application - need some math help

Ranch Hand
Posts: 49
I've created buttons that add, subtract, multiply and divide and these work fine.

I now need to add squared, cubed and sqaure root.

For the sqaured button, I wrote this but it's not working:

int total = totalsofar^2

what am i doing wrong?

also, how would i sqaure root?

charlie mills
Ranch Hand
Posts: 49
i've just discovered the Math class so I have answered my own questions there. However, now I have a new question

I previously had all my values stored as integers and then decided that I needed them stored as doubles.

so at the moment i have:
I'm not really sure how to describe it, but is it because I'm reading in Integers from the string? i'm unsure of how to get it to read in the decimals.

author and iconoclast
Sheriff
Posts: 24220
40
You can use "Double.parseDouble()" instead of "Integer.parseInt()".

Ranch Hand
Posts: 2458
To warn, do not use floating points when doing precise arithmetic calculations. Use BigDecimal.

Also see http://docs.sun.com/source/806-3568/ncg_goldberg.html

Marshal
Posts: 60125
188
If you look here, you find the ^ operator does something completely different from what you thought.

Sheriff
Posts: 21458
96
Math has a method called pow that does what you think ^ does. Or, for squares and cubes, simply calculate "totalsofar * totalsofar" or "totalsofar * totalsofar * totalsofar".

Campbell Ritchie
Marshal
Posts: 60125
188
Calculators are never really precise; you can use a double, knowing that the imprecision will be slight. If you do get \$1.10 - \$0.99 = \$0.109999999999999983465092374625 or similar, will that actually cause any problems?

Rob Spoor
Sheriff
Posts: 21458
96