I came across a puzzle which I can not explain. The setup is SCO OpenServer 5.7 (32 bit OS) and native SCO compiler. double is 8 bytes long on this system. I am able to populate the double variable with two different sets of values that produces the same double value, please see below:
As for why two things can represent identical numbers, it's because of how the number works: A number times a base to an exponent. You could represent 256 with 1 * 2^8, or 2 * 2^7, etc.
Usually though these numbers would be normalized into the first, simplest form to ensure greatest accuracy in numeric operations.
Whatever compiler you are using is broken...get a newer one. Besides you are trying to print a double as a float with only 6 digits of precision so you can guess what happens if the exponent is a very large negative number and change the printf to
Except when it comes to the degree of precision desired...old compilers used to automatically convert floats to doubles but that is no longer practised and if that is indeed the case then all the more reason to use the double format specification instead of the float.
---------- Post updated at 02:59 PM ---------- Previous update was at 01:59 PM ----------
Its good you posted the results as I was getting confused why my systems were reporting different...so it is an endianness issue and here are the results from an AIX and HPUX machine which are both big endian.
d = 2.7178687957231882e-312 (0x0000008014ae1b40) /* seq1 */
d = -9.6037214055361180e-86 (0xae47e17a14ae1b40) /* seq2 */
I'm not saying float and double are the same. I'm saying %f is capable of handling double-precision numbers.
Consider that %f works with doubles and floats even though they're different sizes. stdargs has no way to warn printf which is used, so the compiler must be converting them to something consistent. So if double-precision works in printf at all, they're all being passed as doubles, and all being used as doubles, not floats.
All the implementations of printf that I have seen explicitly convert all floats to doubles in the code and do not leave it up to the compiler to decide whether to or not.