Problems in calculation

Hi

Can you tell me, why those 2 operations give different results?

z.power(2);

z = z*z;

given:

__device__ void power(int p){

		float R=r;

		float I=i;

		float tmpR=0;

		float tmpI=0;

		

		for(int i=1; i<p; i++) { 

			tmpR = r*R - i*I;

			tmpI = r*I + i*R;

			R = tmpR;

			I = tmpI;

		}

		

		r=R;

		i=I;

	}

and

__device__ cudaComplex operator*(const cudaComplex& a) {

        return cudaComplex(r*a.r - i*a.i, i*a.r + r*a.i);

    }

where cudaComplex is my class

class cudaComplex {

public:

    float   r;

    float   i;

...

};

Please help.

Kind regards

Possibly to do with having i as the imaginary part of your complex number and the iterator for your loop?? In the body of your loop i is equal to 1,2 etc and not the imaginary part of your complex number as it should be, at least that’s what I think is happening.

You have scope issues in your power function - the local int i will mask the member variable i.

oh my…

Thank you very much, such a fool I am …

Please delete this topic External Image