Casting in mixed type calculations in C?
- by yCalleecharan
Hi, If I define these variables:
double x0, xn, h;
int n;
and I have this mathematical expression:
h = (xn - x0)/n;
Is it necessary that I cast n into double prior doing the division for maximum accuracy like in
h = (xn - x0)/ (double) n;
I wrote a program to check the above but both expressions give the same answers. I understand that C will promote the integer to double type as variables xn and x0 are of type double but strangely enough in a book, the second expression with casting was emphasized.
Thanks a lot...