Thread: floating point problems

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    Question floating point problems

    Hello,

    This is a strange error I've been dealing with. Basically I've created a Clock class that takes advantage of the timeGetTime() function and I created several member functions. What the program does is basically return the mouse's coordinates on the screen and the time elapsed (which refreshes every time you move the mouse).

    There is no error and the program actually runs, but the time output is just random numbers (just jibberish). I can't figure out why. When I go to the class declaration and change from float to DWORD, everything works fine (i.e. output in milliseconds, not seconds). Please advise. Your help is appreciated.

    --Bill

    Code:
    /* Process Mouse Messages. */
    
    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    #include "Clock.h"
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    
    char szWinName[] = "MyWin"; /* name of window class */
    
    char str[255] = ""; /* holds output string */
    
    MMRESULT begin = timeBeginPeriod(1);
    MMRESULT end = timeEndPeriod(2);
    
    DWORD start=timeGetTime();
    
    LONG xI=0;
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                       LPSTR lpszArgs, int nWinMode)
    {
      HWND hwnd;
      MSG msg;
      WNDCLASSEX wcl;
      
      /* Define a window class. */
      wcl.cbSize = sizeof(WNDCLASSEX); 
    
      wcl.hInstance = hThisInst; /* handle to this instance */
      wcl.lpszClassName = szWinName; /* window class name */
      wcl.lpfnWndProc = WindowFunc; /* window function */
      wcl.style = 0; /* default style */
    
      wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* standard icon */
      wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO); /* small icon */
      wcl.hCursor = LoadCursor(NULL, IDC_ARROW); /* cursor style */
    
      wcl.lpszMenuName = NULL; /* no main menu */
      wcl.cbClsExtra = 0; /* no extra */
      wcl.cbWndExtra = 0; /* information needed */
    
      /* Make the window white. */
      wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
    
      /* Register the window class. */
      if(!RegisterClassEx(&wcl)) return 0;
    
      /* Now that a window class has been registered, a window
         can be created. */
      hwnd = CreateWindow(
        szWinName, /* name of window class */
        "Processing Mouse Messages", /* title */
        WS_OVERLAPPEDWINDOW, /* window style - normal */
        CW_USEDEFAULT, /* X coordinate - let Windows decide */
        CW_USEDEFAULT, /* Y coordinate - let Windows decide */
        CW_USEDEFAULT, /* width - let Windows decide */
        CW_USEDEFAULT, /* height - let Windows decide */
        HWND_DESKTOP, /* no parent window */
        NULL, /* no menu */
        hThisInst, /* handle of this instance of the program */
        NULL /* no additional arguments */
      );
    
      /* Display the window. */
      ShowWindow(hwnd, nWinMode);
      UpdateWindow(hwnd);
    
      /* Create the message loop. */
      while(GetMessage(&msg, NULL, 0, 0))
      {
        TranslateMessage(&msg); /* translate keyboard messages */
        DispatchMessage(&msg); /* return control to Windows 98 */
      }
      return msg.wParam;
    }
    
    /* This function is called by Windows 98 and is passed 
       messages from the message queue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
                                WPARAM wParam, LPARAM lParam)
    {
      HDC hdc;
      Clock timer;
      
      switch(message) {
        case WM_RBUTTONDOWN: /* process right button */
          hdc = GetDC(hwnd); /* get DC */
          sprintf(str, "Right button is down at %d, %d",
                  LOWORD(lParam), HIWORD(lParam));
          TextOut(hdc, LOWORD(lParam), HIWORD(lParam),
                  str, strlen(str));
          ReleaseDC(hwnd, hdc); /* Release DC */
          break;
        case WM_LBUTTONDOWN: /* process left button */
          hdc = GetDC(hwnd); /* get DC */
    	  TextOut(hdc, 10, 50, "Angular velocity is                                   ", 54);
    	  sprintf(str, "Time passed is %d pixels/sec", timer.time());
    	  TextOut(hdc, 10, 50, str, strlen(str));
          ReleaseDC(hwnd, hdc); /* Release DC */
          break;
    	case WM_MOUSEMOVE:
    	  hdc = GetDC(hwnd);
      	  TextOut(hdc, 10, 50, "                                                 ", 49);
    	  sprintf(str, "Time passed is %d milliseconds", timer.time());
    	  TextOut(hdc, 10, 50, str, strlen(str));
    	  TextOut(hdc, 10, 10, "Mouse coordinates are at                                   ", 59);
    	  sprintf(str, "Mouse coordinates are at (%d, %d)", LOWORD(lParam), HIWORD(lParam));
    	  TextOut(hdc, 10, 10, str, strlen(str));
    	  ReleaseDC(hwnd,hdc);
    	  break;
        case WM_DESTROY: /* terminate the program */
          PostQuitMessage(0);
          break;
        default:
           /* Let Windows 98 process any messages not specified in
             the preceding switch statement. */
          return DefWindowProc(hwnd, message, wParam, lParam);
      }
      return 0;
    }
    
    /* Clock class definition (FYI)
    class Clock
    {
    	private:
    		float timestarted;
    	public:
    		Clock();
    		VOID reset();
    		VOID wait(const float &tsec);
    		float time();
    };
    
    Clock::Clock()
    {
    	(*this).reset();
    }
    
    VOID Clock::reset()
    {
    	timestarted=timeGetTime();
    	return;
    }
    
    float Clock::time()
    {
    	return timeGetTime()-timestarted;
    }
    
    VOID wait(const float &tsec)
    {
    	DWORD beginning = timeGetTime();
    	while(beginning + tsec*1000 > timeGetTime())
    	{ /* wait */}
    	return;
    } */

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    "%d" is not used for a float.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    Thanks

    Thank you for your reply. Is there an easier way than TextOut or printf to output data onto the screen? (something like C++'s cout)? I am not familiar with the %d or other notations.

    --Bill

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Bill83
    (something like C++'s cout)? I am not familiar with the %d or other notations.
    How about a stringstream?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    Thank you

    Thanks again. You've been a great help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  3. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 04:29 PM
  4. help needed in floating point
    By akshayabc in forum C Programming
    Replies: 5
    Last Post: 06-27-2005, 11:00 AM
  5. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM