| | |
Square root??
![]() |
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,752
Reputation:
Solved Threads: 491
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++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






