Click here to Skip to main content
15,890,185 members
Home / Discussions / Algorithms
   

Algorithms

 
QuestionHow to determine whether change over time is an outlier Pin
GuyThiebaut7-Jul-13 0:02
professionalGuyThiebaut7-Jul-13 0:02 
AnswerRe: How to determine whether change over time is an outlier Pin
Manfred Rudolf Bihy23-Jul-13 11:22
professionalManfred Rudolf Bihy23-Jul-13 11:22 
GeneralRe: How to determine whether change over time is an outlier Pin
GuyThiebaut23-Jul-13 21:55
professionalGuyThiebaut23-Jul-13 21:55 
Questionfast division algoirthm Pin
khomeyni22-Jun-13 6:01
khomeyni22-Jun-13 6:01 
GeneralRe: fast division algoirthm Pin
harold aptroot22-Jun-13 10:32
harold aptroot22-Jun-13 10:32 
GeneralRe: fast division algoirthm Pin
khomeyni22-Jun-13 23:19
khomeyni22-Jun-13 23:19 
GeneralRe: fast division algoirthm Pin
harold aptroot23-Jun-13 1:42
harold aptroot23-Jun-13 1:42 
GeneralRe: fast division algoirthm Pin
khomeyni2-Jul-13 2:42
khomeyni2-Jul-13 2:42 
thanks harold for your complete and good reply
I cannot add a hardware to the FPGA (the program will be complicated) and don't have any reciprocal support. I wrote some algorithms as follows( from the pages you send to me and from others):
C++
void test1(){
	const long MAX_Iter=10000000;//10^7
	int *ar1=new int [MAX_Iter];/// array of integers to find the reciprocal
	double *R=new double [MAX_Iter];//array of double for saving the regular division 1/x
	double *R2=new double [MAX_Iter];//array of double for saving the algorithms division 1/x
	double *ERROR=new double [MAX_Iter];
	double max_error=pow(10,-100.0);;

	/*
	*  generate some number bigger than 2
	*/
	for(int i=0;i<MAX_Iter;i++){
		ar1[i]=rand()+2+clock();
	}
	cout<<"===================================BESME Allah============================="<<endl<<endl;;
	cout<<"==========================regular algorithm 1/integer============================="<<endl;
	clock_t Start = clock();
	/*
	*regualr division 
	*/
	for(int i=0;i<MAX_Iter;i++){
		R[i]=1.0/ar1[i];
	}
	cout<<"				"<<clock()-Start<<endl;
	cout<<"==========================your suggestion algorithm (ASM):1/integer============================="<<endl;
	Start = clock();
	/*
	*suggested division (ASM)
	*/
	float inv;
	for(int i=0;i<MAX_Iter;i++){
		  //float inv=inverse(x);
		float x=ar1[i];
		unsigned int *y = (unsigned int*)&x;
		//*y=0x7EEEEBB3-*y;
		*y=0x7EF39252-*y;
		//inv=ar1[i];
		inv=x;
		  for(int j=0;j<3;j++){
			 inv=inv*(2-inv*ar1[i]);
		  }
		  R2[i]=inv;
	}
	cout<<"				"<<clock()-Start<<endl;
	max_error=pow(10,-100.0);
	for(int i=0;i<MAX_Iter;i++){
		ERROR[i]=abs(R[i]-R2[i]);
		if(max_error<ERROR[i])
			max_error=ERROR[i];
	}
	cout<<"					maximmum   error="<<max_error<<endl;
	cout<<"========================newton raphson division algorithm time=================="<<endl;
	Start = clock();
	/*
	*newton raphson division 
	*/
	int iter=15;
	for(int i=0;i<MAX_Iter;i++){
		//this part is for finding an approximate
		int j=32;
		double x=0.0000000004656612873077392578125;//2^(-32)
		while(!((ar1[i]<<(32-j))& 2147483648)){
			j--;
			x*=2;
		}
		//end of part of finding aproximate
		//newton raphson method
		for(int j=0;j<iter;j++){
			x=x*(2.0-ar1[i]*x);//x=x*(2-d*x);
		}
		R2[i]=x;
		//
	}
	cout<<"					"<<clock()-Start<<endl;
	max_error=pow(10,-100.0);
	for(int i=0;i<MAX_Iter;i++){
		ERROR[i]=abs(R[i]-R2[i]);
		if(max_error<ERROR[i])
			max_error=ERROR[i];
	}
	cout<<"					maximmum   error="<<max_error<<endl;
	cout<<"===========================goldschmidt division algorithm=========================="<<endl;
	/*
	*	goldschmidt division algorithm
	*/
	//
	#define BASE 31

	typedef unsigned int uint32;
	typedef unsigned long long int uint64;
	
	Start = clock();
//s	int iter=20;
	for(int i=0;i<MAX_Iter;i++){
		int shl = 0; // normalization shift
		uint32 nfp = ar1[i];//
		while ( (nfp & 0x80000000) == 0 ) { nfp <<= 1; shl++; } // use "clz" instead

		uint64 q = 0x100000000ULL; // 2^32
		uint64 e = 0x100000000ULL - (uint64)nfp; // 2^32-NFP
		int j;
		for (j=0;j<5;j++) //for (i=0;i<100;i++) //for (i=0;i<4;i++) // iterate
		{
			// Both multiplications are actually
			// 32x32 bits truncated to the 32 high bits
			q += (q*e)>>(uint64)32;
			e = (e*e)>>(uint64)32;
			//printf("Q=0x%llx E=0x%llx\n",q,e);
		}
		// Here, (Q/2^32) is the inverse of (NFP/2^32).
		// We have 2^31<=NFP<2^32 and 2^32<Q<=2^33
		R2[i]=(uint32)(q>>(64-2*BASE-shl));
	}
	cout<<"					"<<clock()-Start<<endl;
	max_error=pow(10,-100.0);
	for(int i=0;i<MAX_Iter;i++){
		ERROR[i]=abs(R[i]-R2[i]);
		if(max_error<ERROR[i])
			max_error=ERROR[i];
	}
	cout<<"					maximmum    error="<<max_error<<endl;
}


It gives me this answers:
===================================BESME Allah=============================

==========================regular algorithm 1/integer===========================
==
101
==========================your suggestion algorithm (ASM):1/integer=============
================
280
maximmum error=1.15959e-010
========================newton raphson division algorithm time==================

2146
maximmum error=4.12998e-006
===========================goldschmidt division algorithm=======================
===
2343
maximmum error=0.00328947
Press any key to continue . . .


as you see the best algorithm now is the ASM algorithm( it's my name for it) . but I have some problem with it;I can't use any pointer!!!
I wrote the same algorithm without pointer but it goes wrong(and hasn't any logic to answer correct);

C++
void test3(){
	float x;
	float inv;
	cout<<endl;
	float main_x;
	while(cin>>x){
		main_x=x;
		unsigned int *y = (unsigned int*)&x;
		//*y=0x7EEEEBB3-*y;
		*y=0x7EF39252-*y;
		//inv=ar1[i];
		inv=x;
		  for(int j=0;j<3;j++){
			 inv=inv*(2-inv*main_x);
		  }
		  cout<<"1/x="<<(1.0/main_x)<<"   our inv="<<inv<<"     error="<<(1.0/main_x)-inv<<endl;
	}
}
void test4(){
	float x;
	float inv;
	cout<<endl;
	float main_x;
	while(cin>>x){
		main_x=x;
		//unsigned int *y = (unsigned int*)&x;
		//*y=0x7EF39252-*y;
		x=(0x7EF39252-(unsigned int)x);
		inv=x;
		/*  for(int j=0;j<3;j++){
			 inv=inv*(2-inv*main_x);
		  }*/
		  cout<<"1/x="<<(1.0/main_x)<<"   our inv="<<inv<<"     error="<<(1.0/main_x)-inv<<endl;
	}
}


test3 works correct but work4 not! why? how it works? can I use a trick to use this procedure without using any pointer?(I can use only for, if , while, and some simple commands. pointer implementation in fpga will be hard).
thanks in advance;

modified 2-Jul-13 8:50am.

GeneralRe: fast division algoirthm Pin
harold aptroot2-Jul-13 3:11
harold aptroot2-Jul-13 3:11 
GeneralRe: fast division algoirthm Pin
khomeyni2-Jul-13 8:58
khomeyni2-Jul-13 8:58 
GeneralRe: fast division algoirthm Pin
harold aptroot2-Jul-13 9:44
harold aptroot2-Jul-13 9:44 
QuestionAlgorithm Sum From Offset To Number Pin
A*****17-Jun-13 12:37
A*****17-Jun-13 12:37 
AnswerRe: Algorithm Sum From Offset To Number Pin
A*****17-Jun-13 17:07
A*****17-Jun-13 17:07 
GeneralRe: Algorithm Sum From Offset To Number Pin
dusty_dex17-Jun-13 23:03
dusty_dex17-Jun-13 23:03 
GeneralRe: Algorithm Sum From Offset To Number[Edited] Pin
A*****18-Jun-13 12:50
A*****18-Jun-13 12:50 
GeneralRe: Algorithm Sum From Offset To Number[Edited] Pin
dusty_dex18-Jun-13 13:20
dusty_dex18-Jun-13 13:20 
GeneralRe: Algorithm Sum From Offset To Number[Edited] Pin
A*****18-Jun-13 13:24
A*****18-Jun-13 13:24 
AnswerRe: Algorithm Sum From Offset To Number Pin
Bernhard Hiller18-Jun-13 20:57
Bernhard Hiller18-Jun-13 20:57 
GeneralRe: Algorithm Sum From Offset To Number Pin
dusty_dex18-Jun-13 22:38
dusty_dex18-Jun-13 22:38 
QuestionWikipedia's comments on Interpolation Search Pin
harold aptroot11-Jun-13 0:59
harold aptroot11-Jun-13 0:59 
AnswerRe: Wikipedia's comments on Interpolation Search Pin
Alan Balkany13-Jun-13 8:27
Alan Balkany13-Jun-13 8:27 
GeneralRe: Wikipedia's comments on Interpolation Search Pin
harold aptroot13-Jun-13 8:59
harold aptroot13-Jun-13 8:59 
Questionalgorithm that finds the m smallest numbers in a list of numbers Pin
demo 29-Jun-13 21:00
demo 29-Jun-13 21:00 
AnswerRe: algorithm that finds the m smallest numbers in a list of numbers Pin
Richard MacCutchan9-Jun-13 21:14
mveRichard MacCutchan9-Jun-13 21:14 
GeneralRe: algorithm that finds the m smallest numbers in a list of numbers Pin
demo 29-Jun-13 23:50
demo 29-Jun-13 23:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.