• Status: Solved
• Priority: Medium
• Security: Public
• Views: 221

Math

How do I get the square root of a number?
0
p314159265
1 Solution

Commented:
0

Commented:
To calculate the square root of a number, use the sqrt function like so:

#include <math.h>
.
double number = 4.0;
double result = sqrt (number);
ASSERT (result == 2.0);
0

Commented:
If you have any additional questions, please feel free to ask.  Thanks!
0

Author Commented:
What does 'ASSERT' do?
0

Commented:
"ASSERT" performs a test that, if failed, will stop the program's execution.  In this case, it is basically saying, "If the square root of 4 is not 2, then stop the program from running."  Obviously, you would want that to happen because the square root of 4 is obviously 2.

If you have further questions, please ask.  Thanks!
0

Author Commented:
Thanks, that about covers it.
0

Commented:
Of course, you don't NEED to use ASSERT...  Thresher just put it there as a code snippet to prove that the sqrt function works.

a=sqrt(b) is the only code you really need.
0

Commented:
Yes, thank you scrapdog for clarifying :-)
0

Commented:
I think you can use this function to get n root of a
Give a = x^n
=> x = exp((1/n)*log(a))
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Featured Post

Tackle projects and never again get stuck behind a technical roadblock.