This integer division yields 0 because the magnitude of the denominator is greater than that of the numerator:
1/100000
You need to use at least one floating point number here. For example
1.0/100000
Question
I have an issue with a piece of code in which I have an if
statement that checks the diagonal elements of a complex array, and if they are (1,0) (meaning the real part is 1 and the imaginary part is 0) or very close to it then proceed; otherwise stop. But for some reason the operation always returns 0 although the elements are (0.999999, 0.000000), (1.000001, 0.000000), etc.
Here's the code I have:
for (i=1; i<=n; i++){
if ((real(c[i][i][1]) < (1/100000)+1) && (imag(c[i][i][1]) < 1/100000) && (real(c[i][i][1]) > (-1/100000+1) && (imag(c[i][i][1]) > -1/100000)){
cout<<"Operation continues...";
}
else
return 0;
}
Thanks in advance.
Solution
This integer division yields 0 because the magnitude of the denominator is greater than that of the numerator:
1/100000
You need to use at least one floating point number here. For example
1.0/100000
OTHER TIPS
1 / 100000 == 0
as it is integer division:
you may write 0.00001
or 1. /100000.
BTW, you may write your loop:
constexpr double thresold = 0.00001;
for (i = 1; i <= n; i++) {
if (std::abs(real(c[i][i][1]) - 1.) < thresold
&& std::abs(imag(c[i][i][1]) - 1.) < thresold) {
cout << "Operation continues...";
}
else
return 0;
}