Click here to Skip to main content
15,901,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i needed a pretty much efficient way to calculate square roots upto 100 decimal places(and 100% correct).i have been searching for square root algorithms on the internet.actually, i want a c++ adaptation of long division method that we learn in high school.by far,it seems most promising to me.but it's too much for my programming skills(i know only very basic c++,actually,i want to test the mathematical formula i made using a computer so that it produces high values.i even tested the method in which a value is estimated and average is taken,but it fails after only 10 digits).
Posted

You've got two choices here - one is to roll your own using a single digit method (as shown on the page Richard mentioned) but from what you've said you're not that experienced and implementing it looks a bit fiddly if not fundamentally difficult.

Another option would be to use an arbitrary precision arithmetic library. This page [^] has some you might like to consider. As you're not that experienced with C++ you could end up thinking you've changed the problem from "How do I calculate a high precision square root" to "how do I build and use this sodding library?"

If you want to keep using C++ then working out how to use libraries is probably a better bet. If you want to keep numerical programming in the language then rolling your own might be better for now.

And... (final point that might make the whole thing redundant) does it have to be C++? A fair number of languages have support for arbitrary numbers built in. Python, for example, has a class (Decimal) that can can crank you square roots out to an arbitrary precision. It might be worth learning enough Python so you can use that rather than fighting with C++.
 
Share this answer
 
Comments
Richard MacCutchan 22-May-12 14:08pm    
"how do I build and use this sodding library?"
You hum it and I'll play it, son.
My suggestion would be to work out the algorithm first (without worrying about code) and get the steps written down in natural language. Only when you have that done and you understand each step can you turn it into code.
 
Share this answer
 
Comments
utkarshs 22-May-12 7:54am    
thanks for your answer but i already tried it.i gets VERY complex,much dues to the fact that every number must be broken down into pairs of digits,and then acted on.i tried using arrays for it,but it gets too complex
Richard MacCutchan 22-May-12 8:16am    
Sounds like you need to work on something simpler.
utkarshs 22-May-12 9:06am    
any ideas?
utkarshs 22-May-12 9:20am    
is there no way to use this method in c++,even with advanced functions or something?
Richard MacCutchan 22-May-12 9:51am    
Like I said above, forget C++, until you have worked out what the algorithm should be. This is a question of mathematics rather than programming.

@Richard MacCutchan, thanks!!i think i have found the solution .i have to deal with set of numbers from 1 to 2.so,first i'll take an int named pair,and put the number equal to it.the pair would contain integral value to the number x(the number to be square rooted),and i will find the first digit of answer(a).then i will put x=(x-pair)*100,and pair=x,which would give me next pair.continuing the calculation,i would calculate the answer and put a=(10*a)+ans
 
Share this answer
 
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#include<stdlib.h>
int trt;
	int pair(double y,int k){
	y=y*pow(10,(k-1)*2);
	int n=y;
	y=y-n;
	y=y*100;
	n=y;
	return n;
}
int scal2(int s,int f){
	f=f*20;
	for(int j=9;j>=0;j--)
	{
		if((j*(j+f))<s){>
		return(s-(j*(j+f)));
		break;
		}
	}
}
int sqcalc(int g,int h){
	h=h*20;
	for(int j=9;j>=0;j--)
	{
		if((j*(j+h))<g){>
		return j;
		break;
		}
	}
}
double sqroot(double x,double res){
double ans;
int r,a;
	r=sqrt(x);
	ans=r;
	r=x-ans;
	
		for(int i=1;i<=res;i++){
		r=(r*100)+pair(x,i);
		double d=scal2(r,ans);
		ans=(ans*10)+sqcalc(r,ans);
		r=d;
	}	
		
		ans=ans/(pow(10,res));
		return ans;
}	
	int main(){
	cout << setprecision(10)<< sqroot(2,5);
	return(0);
}</stdlib.h></math.h></iomanip.h></iostream.h>


here sqroot(a,b) returns value of square root of a upto b decimal places.there is an another problem.for root 2 it works upto 5 places,but after that it goes to 60!what do you think could be the cause?(please help me i am a novice programmer,maybe even worse!)
 
Share this answer
 
Comments
Richard MacCutchan 23-May-12 4:28am    
This is a function of the algorithm. Square roots of large numbers are going to be increasingly difficult to calculate: that is a mathematical issue, nothing to do with programming. As I suggested earlier you may be better working on something that is more in keeping with your skill level than this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900