What's new

What is C++ operator for squaring/square rooting a number?

  • Thread starter lowpro
  • Start date
  • Views 42,399
lowpro

lowpro

Professional Abecedarian
Programmer Mythical Veteran Mr. Nice Guy
Messages
4,528
Reaction score
2,041
Points
725
I'm new at C++, and I was wondering what the operator was for squaring and square rooting a number.

Example: 144 square rooted is 12
12 squared is 144

^so what would I put in where the words square rooted and squared are?
Thanks for any help. :smile:
 
amd42

amd42

Getting There
Messages
826
Reaction score
595
Points
180
First, you need to include cmath:
Code:
#include <cmath>

Then you can do exponents with
Code:
std::pow(base, exponent);
(use 2 as the exponent or just do x*x if you want to square something)

and square roots with
Code:
std::sqrt(value);

If you have a using namespace std; line at the top of your code then you don't need the std:: prefixes, but personally I don't like to do that because it pollutes the global namespace and prevents you from e.g. having a variable called "sqrt".
 
Top Bottom
Login
Register