For example, as I know, a float point x, which x/n^2 may not be equal to x/n/n, because in x/n/n, which would create an intermediate x/n first. For example:
document.write(1.23456789/49 == 1.23456789/7/7);
However, is x/4 == x/2/2 an exception, if no overflow and underflow, which is always true?
I tested:
document.write(1.23456789/4 == 1.23456789/2/2);
Also for binary:
Binary of 1.23456789 (actual float : 1.2345678806304931640625) :
0 01111111 00111100000011001010010
binary of 0.61728394031524658203125 (1.2345678806304931640625/2)
0 10000000 00111100000011001010010
which seems when multiply only varies the "exponent" part (01111111 to 10000000), keeping the fraction part unchanged (00111100000011001010010).
So currently I have a "hypothesis" : for float number x without overflow and underflow, x/4 should always be equal to x/2/2, also for x * 4==x * 2 * 2, is that true?
2
Answers
I thought it would be only true for powers of 2. So I tried with your float and other prime numbers and found that it was apparently only not true for 7?, so decided to brute force it.
For random float numbers, yeh, only exponents of two survive. But at this point I’m more curious about the 7 and your 1.23456…
Without overflow and underflow, since the radix is 2, multiplications and divisions by powers of 2 are always exact operations. Since no rounding is involved here,
x/4
is equal tox/2/2
andx*4
is equal tox*2*2
, as this is true on the real numbers.Note that if
x
is ±0, the sign of the result will be the same with both expressions.EDIT: More generally, in each expression, you may have a single multiplication or division by a number that is not a power of 2. For instance,
x/2/3
andx/3/2
are always equal tox/6
.