I was doing some homework problems from my textbook and had a few questions on floating point rounding / precision for certain arithmetic operations.
If I have casted doubles from an int like so:
int x = random(); double dx = (double) x; And let's say the variables y, z, dy, and dz follow the same format.
Then would operations like:
(dx + dy) + dz == dx + (dy + dz) (dx * dy) * dz == dx * (dy * dz) be associative? I know that if we have fractional representations, then it would not be associative because some precision will be lost due to rounding depending on which operands add / multiply each other. However, since these are casted from ints, I feel like the precision would not be a problem and that these can be associative?
And lastly, the textbook I'm using does not explain FP division at all so I was wondering if this statement was true, or at least just how floating point division works in general:
dx / dx == dz / dz I looked this up online and I read in some areas like an operation like 3/3 can yield .999...9 but there wasn't enough information to explain how that happened or if it would vary with other division operations.
(dx * dy) * dz == dx * (dy * dz)a problem if the precision of adouble< twice precision of anint- which is often the case.(dx + dy) + dz == dx + (dy + dz)unlikely to be a problem asdoubleprecision certain more thanintprecision + 1.dx / dx == dz / dzobvious problem shoulddx==0ordz==0.dx * dyanddy * dzgreat than2^53, it may be have precision issue.double dx = (double)(INT_MAX); double dy = (double)(INT_MAX - 0x111111); double dz = (double)(INT_MAX - 0xabcd);for(dx * dy) * dz == dx * (dy * dz)is false.