| | |
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,829
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 application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






