I've managed to cobble together a very basic windows program that shows the trouble. Run the program and left click in the window to bring up the messagebox. Here's what my messagebox shows:
Original tScoref1: 197.010000
Original tScoref2: 298.010000
Original tScoref3: 200.016000
Original tScoref4: 200.010000
Total tScoref: 895.046000
tScore = (int)tScoref: 895.045898
tScoref *= 1000: 895046.000000
tScore *= 1000: 895045.500104
tScoref -= tScore: 46.000000
tScore = (int)tScoref: 45.999969
All my code is under "case WM_LBUTTONDOWN:" in the WindowProcedure. You can change values and specifiers to see what's going on with my larger program.
Here's the windows source. I'm compiling under WindowsXP using Dev-C++ 4.9.9.2.
#include <windows.h>
#include <fstream>
#include <iostream>
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
"Double to Int Conversion Problem",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
320,
240,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nFunsterStil);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:{
char tempTest[20][20], fullText[400];
int tScore;
double tScoref1= 197.010;
sprintf(tempTest[0], "%f", tScoref1);
double tScoref2= 298.010;
sprintf(tempTest[1], "%f", tScoref2);
double tScoref3= 200.016;
sprintf(tempTest[2], "%f", tScoref3);
double tScoref4= 200.010;
sprintf(tempTest[3], "%f", tScoref4);
double tScoref = tScoref1+tScoref2+tScoref3+tScoref4;
sprintf(tempTest[4], "%f", tScoref);
tScore = (int)tScoref;
sprintf(tempTest[5], "%f", tScore);
tScoref *= 1000;
sprintf(tempTest[6], "%f", tScoref);
tScore *= 1000;
sprintf(tempTest[7], "%f", tScore);
tScoref -= tScore;
sprintf(tempTest[8], "%f", tScoref);
tScore = (int)tScoref;
sprintf(tempTest[9], "%f", tScore);
strcpy(fullText, "Original tScoref1: ");
strcat(fullText, tempTest[0]);
strcat(fullText, "\nOriginal tScoref2: ");
strcat(fullText, tempTest[1]);
strcat(fullText, "\nOriginal tScoref3: ");
strcat(fullText, tempTest[2]);
strcat(fullText, "\nOriginal tScoref4: ");
strcat(fullText, tempTest[3]);
strcat(fullText, "\nTotal tScoref: ");
strcat(fullText, tempTest[4]);
strcat(fullText, "\ntScore = (int)tScoref: ");
strcat(fullText, tempTest[5]);
strcat(fullText, "\ntScoref *= 1000: ");
strcat(fullText, tempTest[6]);
strcat(fullText, "\ntScore *= 1000: ");
strcat(fullText, tempTest[7]);
strcat(fullText, "\ntScoref -= tScore: ");
strcat(fullText, tempTest[8]);
strcat(fullText, "\ntScore = (int)tScoref: ");
strcat(fullText, tempTest[9]);
MessageBox(hwnd, fullText, "Compilation of values", MB_OK);
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Thanks again for any help.
Take Care,
Ed