I don't really understand casting in C. Can anyone help me with a question in the book Computer Systems: A Programmer's Perspective:
We generate arbitrary integer values x, y, and z, and convert them to values of type double as follows:
int x = random();
int y = random();
int z = random();
double dx = (double) x;
double dy = (double) y;
double dz = (double) z;
For each of the following C expressions, you are to indicate whether or not the expression always yields 1. If it always yields 1, describe the underlying mathematical principles. Otherwise, give an example of arguments that make it yields 0
A. (float) x == (float) dx
B. dx - dy == (double) (x-y)
C. (dx + dy) + dz == dx + (dy + dz)
D. (dx * dy) * dz == dx * (dy * dz)
E. dx / dx == dz / dz
Since you are just converting x,y,z to their double form , dx,dy,dz are nothing but x,y,z having some number of zeroes after a decimal. Depending upon compiler, it may happen that they are added with some other digit than zero. In that case , equality will not hold. For example : x=5 and dx=5.000000 but it may happen dx is made 5.000001 because it is still almost equivalent to x. I also advise to not go in such pointless topic of equality of floating point numbers.