Casting in mixed type calculations in C?
Posted
by yCalleecharan
on Stack Overflow
See other posts from Stack Overflow
or by yCalleecharan
Published on 2010-04-17T21:01:25Z
Indexed on
2010/04/17
21:03 UTC
Read the original article
Hit count: 147
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...
© Stack Overflow or respective owner