Please let me know the difference between the following C functions.
static int mandel(float c_re, float c_im, int count) {
float z_re = c_re, z_im = c_im;
int i;
for (i = 0; i < count; ++i) {
if (z_re * z_re + z_im * z_im > 4.f)
break;
float new_re = z_re*z_re - z_im*z_im;
float new_im = 2.f * z_re * z_im;
z_re = c_re + new_re;
z_im = c_im + new_im;
}
return i;
}
And the following
static int mandel(float c_re, float c_im, int count) {
float z_re = c_re, z_im = c_im;
int i;
for (i = 0; i < count; ++i) {
if (z_re * z_re + z_im * z_im > 4.f)
break;
float new_im = 2.f * z_re * z_im;
z_re = c_re + z_re*z_re - z_im*z_im;//I have combined the statements here and removed float new_re
z_im = c_im + new_im;
}
return i;
}
Please see my comments for the change in code.The function gives different values for some inputs. Is the float getting erred off due to combining the two statements?
In a mathematics the two statements would be equivalent. However in computer hardware they may not be.
You could be getting round off error because the initial result (new_re) is rounded and then added to c_re .
As Niklas mention:
so the result of new_re may lose some floating points when stored to new_re, but if the intermediate values are added to c_re then a small value of c_re combined with lower significant values of new_re calculation may contribute to the end result.