// code to investigate the Excel 2007 bug
// Chris Lomont 2007
// put =850*77.1 in a cell, text shows 100,000, correct is 65535
#include <iostream>
#include <cmath>

using namespace std;

#define Dump(a) DumpVal(#a,a)

void DumpVal(char * text, double v)
	{
	unsigned char * byte = reinterpret_cast<unsigned char*>(&v);
	cout << hex << "0x";
	for (int a = sizeof(double)-1; a >= 0; --a)
		{
		int val = byte[a];
		if (val < 10) cout << '0';
		cout << val << ' ';
		}
	cout << dec;
	cout << " = \t" << v << "\t = " << text;
	cout << endl;
	} // DumpVal

int main(void)
	{
	// some negative powers
	double e35 = pow(2.0,-35.0);
	double e36 = pow(2.0,-36.0);
	double e37 = pow(2.0,-37.0);
	double e38 = pow(2.0,-38.0);

	Dump(65535);
	Dump(850*77.1);
	Dump(850*77.1+1);
	Dump(850*77.1+0.5);
	Dump(65536);

	// show this too small, does not effect outcomes
	Dump(65535-e38);

	// all combinations of e35 to e38
	Dump(65535        -e37);
	Dump(65535    -e36    );
	Dump(65535    -e36-e37);
	Dump(65535-e35        );
	Dump(65535-e35    -e37);
	Dump(65535-e35-e36    );
	Dump(65535-e35-e36-e37);

	Dump(65536        -e37);
	Dump(65536    -e36    );
	Dump(65536    -e36-e37);
	Dump(65536-e35        );
	Dump(65536-e35    -e37);
	Dump(65536-e35-e36    );
	Dump(65536-e35-e36-e37);


	return 0;
	}
// end - ExcelBug.cpp