| | |
Square root??
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Read this
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
How about the sqrt function from cmath?
http://www.cplusplus.com/reference/c...math/sqrt.html
http://www.cplusplus.com/reference/c...math/sqrt.html
You can try to bruteforce it out and just increment by 0.01 maybe that will work out.
But the answer wouldnt be exact.
So u can try in for an approx val.
Then test it up. If the value you got. is squared is lesser than the main number of which you should find the square root then increment. But if the the square of the value increases take the previous number as the approximate square root. So you will only be missing the minutest details
But the answer wouldnt be exact.
So u can try in for an approx val.
Then test it up. If the value you got. is squared is lesser than the main number of which you should find the square root then increment. But if the the square of the value increases take the previous number as the approximate square root. So you will only be missing the minutest details
A simple method for calculating square root of a number "a" is given by newton iteration as
where x0 is initialized to 1. Use float comparisons and loop till xn equals x0 here I have used a loop for simplicity. Again why cant you use sqrt()?
C++ Syntax (Toggle Plain Text)
for(int i=0;i<10;i++) { xn=.5*(x0+(a/x0)); x0=xn; }
where x0 is initialized to 1. Use float comparisons and loop till xn equals x0 here I have used a loop for simplicity. Again why cant you use sqrt()?
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
•
•
•
•
A simple method for calculating square root of a number "a" is given by newton iteration as
C++ Syntax (Toggle Plain Text)
for(int i=0;i<10;i++) { xn=.5*(x0+(a/x0)); x0=xn; }
where x0 is initialized to 1. Use float comparisons and loop till xn equals x0 here I have used a loop for simplicity. Again why cant you use sqrt()?
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Windows API - Use QT Now?
- Next Thread: Hi,Can Anybody give me some addvice please
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






