# Double and Floats

This is a discussion on Double and Floats within the C Programming forums, part of the General Programming Boards category; Whats the deal with using floats and double in same mathematical expressions? I have the foloowing function, and I just ...

1. ## Double and Floats

Whats the deal with using floats and double in same mathematical expressions?
I have the foloowing function, and I just noticed that three parameters, eta phi and omega_2 are floats, the rest are doubles. Is this going to change anything, because at the moment my code doesnt do what I expect of it, but I cant see another issue, however I know that "curve_to_fit" is calculating incorrectly.
Code:
```static double function2(int n, double y[])

double curve_to_fit[23],curve_to_fit_sum_abs = 0, c1[23], x_coeff_2[23], v1[23], v2[23];
float eta = y[0]; //parameters
float phi=y[1]; //parameters
float omega_2=y[2]; //parameters

int i;

for (i = 0; i<=22; i++)
{
v1[i] = (exp(a_global*2*pi)-cosh(a_global*b_global*init_T[i][0]));
v2[i] = sinh(a_global*b_global*init_T[i][0]);
x_coeff_2[i] = v1[i]/v2[i];
c1[i]= b_global*b_global*(x_coeff_2[i]*x_coeff_2[i]-1);
curve_to_fit[i] = c1[i]*exp(-2*a_global*init_T[i][1])+eta*((1+0.5*(4*a_global*a_global+1))*cos(init_T[i][1]+phi)-2*a_global*sin(init_T[i][1]+phi))+b_global*b_global-omega_2;
}
for (i = 0; i<=22; i++)
{
curve_to_fit_sum_abs = curve_to_fit_sum_abs + abs(curve_to_fit[i]);
printf("i=%d, c1[i] = %f, x_coeff_2[i] = %f, curve_to_fit[i] = %f\n",i, c1[i],x_coeff_2[i],curve_to_fit[i]);  //  printf("curve_to_fit = %f, curve_to_fit_sum_abs = %f\n",curve_to_fit[i], curve_to_fit_sum_abs);
}

return curve_to_fit_sum_abs;

}```
Cheers

2. Originally Posted by a.mlw.walker
I have the foloowing function, and I just noticed that three parameters, eta phi and omega_2 are floats, the rest are doubles. Is this going to change anything, because at the moment my code doesnt do what I expect of it, but I cant see another issue:
It might since there may be a loss of information due to a loss of precision as the elements of y are doubles. In the actual expressions themselves this may or may not be a problem since the floats will be promoted to double anyway.

3. You may want to peruse through the IEE 754 Reference Manual. Additionally, on this page you will find a link to the article What Every Computer Scientist Should Know about Floating-Point Arithmetic.

4. Have you ever tried to replicate matlab code in C? The above code is supposed to be my translation of matlab code, however its not producing the same x_coeff_2 value: Its strange because up to that point it is correct, and then that line is quite straight forward. The matlab code is: here T0 is column 0 of ini_T, and theta_f is the second column of init_T (above)
Code:
```eta = params2(1);
phi = params2(2);
wf2 = params2(3);
x = (exp(a*2*pi)-cosh(a*b*T0))./sinh(a*b*T0)
c1 = b^2*(x.^2-1)
f1 = c1.*exp(-2*a*theta_f)+eta*((1+0.5*(4*a^2+1))*cos(theta_f+phi)-2*a*sin(theta_f+phi))+b^2-wf2
f2 = abs(f1);
f = sum(f2);```
The point is the '.' operator in matlab.

5. Do it with a calculator. Which numbers do you believe?

6. Right. The '.' operator in matlab (IIRC) tells it to do the multiplication/division element-by-element, instead of using regular matrix multiplication. It would help immensely if you made your matlab and C variable names exactly the same. Your problem (I think), is one of order of operations:
Code:
```v1[i] = (exp(a_global*2*pi)-cosh(a_global*b_global*init_T[i][0]));
v2[i] = sinh(a_global*b_global*init_T[i][0]);
x_coeff_2[i] = v1[i]/v2[i];```
vs
Code:
`x = (exp(a*2*pi)-cosh(a*b*T0))./sinh(a*b*T0)`
Notice the difference in order of operations. The C code subtracts cosh() from exp() before dividing by sinh, whereas the matlab code divides cosh() by sinh() before subtracting that from exp(). Try something like:
Code:
```#define SIZE 23  // use named constants instead of magic numbers
double temp[SIZE];

for (i = 0; i < SIZE; i++) {  // note the more conventional for loop construct here with < instead of <=
v1[i] = cosh(a_global*b_global*init_T[i][0])) / sinh(a_global*b_global*init_T[i][0]);
x_coeff_2[i] - exp(a_global*2*pi) - temp[i]
c1[i]= b_global*b_global*(x_coeff_2[i]*x_coeff_2[i]-1);
...
}```

7. Originally Posted by anduril462
Notice the difference in order of operations. The C code subtracts cosh() from exp() before dividing by sinh, whereas the matlab code divides cosh() by sinh() before subtracting that from exp().
I think you've lost a set of parentheses.

8. What about the bracket before exp and before the '.'? dont they say do the subtraction first?
anduril462, in your alternative c code do you mean '=' instead of '-'? and should v1[i] be temp[i]?

9. Originally Posted by tabstop
I think you've lost a set of parentheses.
Yes, I have.

Originally Posted by a.mlw.walker
What about the bracket before exp and before the '.'? dont they say do the subtraction first?
anduril462, in your alternative c code do you mean '=' instead of '-'? and should v1[i] be temp[i]?
Yeah, I initially rewrote that using temp, then went back to v1.

As a matter of fact, just ignore my last post. Two weeks in the mountains and I've forgotten how to read and write.

10. So to conclude (i got a little confused with that) it isnt doing what you thought it was? and therefore the following code you suggested isnt the solution? Or is it? (I believe the Matlab)

11. Forget post #6 even happened. Take a look at the following line:
Code:
`curve_to_fit_sum_abs = curve_to_fit_sum_abs + abs(curve_to_fit[i]);`
That one is for ints, and will truncate your results. You probably want fabs for doubles.

12. That did a much better job, the function is a parameter estimation, and it gets the first one correct, however the other two are close but not that close (to the matlab - and the matlab is very close to the actual values), are there any other tricks like that i should be aware of?

13. forget last post. excellent. thanks guys