NO

Author Topic: type errors in simple maths  (Read 3214 times)

martin

  • Guest
type errors in simple maths
« on: October 20, 2008, 10:51:30 PM »
I have this
Code: [Select]
LARGE_INTEGER lFreq, lNow, lLast, lElapsed;

QueryPerformanceFrequency(&lFreq);
QueryPerformanceCounter(&lNow);
 
    lElapsed = (lNow - lLast) * 1000/lFreq;//mS <-errors

I get errors as follows
Code: [Select]
error #2168: Operands of - have incompatible types 'LARGE_INTEGER' and 'LARGE_INTEGER'.
 error #2168: Operands of / have incompatible types 'int' and 'LARGE_INTEGER'.
 error #2168: Operands of = have incompatible types 'LARGE_INTEGER' and 'int'.
 error #2168: Operands of > have incompatible types 'LARGE_INTEGER' and 'int'.

I have tried casting the LARGE_INTEGERS to float but this is not allowed, and I have tried many other things but I obviously don't understand some basics.
Can someone point me to somewhere that explains how I do something so simple as a this little calculation.

Even these give errors.
Code: [Select]

lElapsed = lNow - lLast;
lElapsed -= lNow;

The same types of calculation can be made in Visual C++ without errors.

Romashka

  • Guest
Re: type errors in simple maths
« Reply #1 on: October 20, 2008, 11:31:29 PM »
seems like LARGE_INTEGER is not defined correctly  :-\
can you post a definition of this type?
and are you sure the file that defines it is #included?
« Last Edit: October 20, 2008, 11:34:18 PM by Romashka »

martin

  • Guest
Re: type errors in simple maths
« Reply #2 on: October 21, 2008, 12:21:05 AM »
Thanks Romashka, that gave me the answer. I thought that LARGE_INTEGER was long long or int64, but after seeing your post I looked up the definition and it is a union. So to use the values I want I have to say
Code: [Select]
QueryPerformanceFrequency(&lFreq);
QueryPerformanceCounter(&lNow);

lElapsed.QuadPart = (lNow.QuadPart - lLast.QuadPart) * 1000/lFreq.QuadPart;//mS
and it works!
Thanks. (Great progress for me  :) )

Offline Robert

  • Member
  • *
  • Posts: 245
Re: type errors in simple maths
« Reply #3 on: October 21, 2008, 05:22:09 AM »
Hi Martin:

It may "work" but does it do so correctly?

Robert Wishlaw

Code: [Select]

#include <windows.h>
#include <stdio.h>
#include <time.h> // contains needed prototype for _sleep function

// User Global Variables
static LARGE_INTEGER lFreq, lLast, lNow, lElapsed;
static double RElapsed;
static int RetVal;

void main(void) {

RetVal = QueryPerformanceFrequency(&lFreq);
if(RetVal == 0)
  {
    printf("%s\n","This computer does not have a high-performance timer.");
    fflush(stdout);
    ExitProcess(0);
  }

printf("%s\n", "Pelle's C _sleep ");
QueryPerformanceCounter(&lLast);
_sleep(2);
QueryPerformanceCounter(&lNow);

  lElapsed.QuadPart = (lNow.QuadPart - lLast.QuadPart) * 1000/lFreq.QuadPart;//mS
  printf("%s% .15G%s\n", "did not last ", (double)lElapsed.QuadPart, " seconds,");

  RElapsed = (double)(((lNow.QuadPart - lLast.QuadPart) * (1.0 / lFreq.QuadPart)));
  printf("%s% .15G%s\n", "it lasted ", RElapsed, " seconds.");
}


Offline Robert

  • Member
  • *
  • Posts: 245
Re: type errors in simple maths
« Reply #4 on: October 21, 2008, 05:39:17 AM »
Also, NOTE WELL that on some computers hardware errata exists that confounds the QueryPerformanceCounter function. Specifically, the result that is returned by the QueryPerformanceCounter function may unexpectedly leap forward from time to time. This leap may represent several seconds.  For more details see

'Performance counter value may unexpectedly leap forward'

available at

http://support.microsoft.com/kb/274323

Robert Wishlaw
« Last Edit: October 21, 2008, 05:41:46 AM by Robert »