About Store Forum Documentation Contact



Post Reply 
Time.curTime() bug
Author Message
cmontiel Offline
Member

Post: #1
Time.curTime() bug
Sometimes Time.curTime() gives negative values.

IRC: irc.freenode.net
Channel: #Esenthel
12-20-2012 02:08 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Time.curTime() bug
Could you please give example of at which code location you can receive negative values?
(like, secondary/main thread? before/after InitPre?)
Can you attach sample code to reproduce the problem?
On what platform are you experiencing this issue? (do other platforms work ok?)
12-22-2012 03:26 PM
Find all posts by this user Quote this message in a reply
cmontiel Offline
Member

Post: #3
RE: Time.curTime() bug
Quote:Could you please give example of at which code location you can receive negative values? (like, secondary/main thread? before/after InitPre?)

Main thread , inside InitPre.

Quote:Can you attach sample code to reproduce the problem?

Code:
MainServer.cpp

#include "stdafx.h"
#include "Server.h"
#include "GameData.h"

void InitPre()
{
    AllocConsole();
    freopen("CONIN$","rb",stdin);
    freopen("CONOUT$","wb",stdout);
    freopen("CONOUT$","wb",stderr);
    App.name("Prison Survival Server");
    App.flag=APP_WORK_IN_BACKGROUND|APP_NO_PAUSE_ON_WINDOW_MOVE_SIZE|APP_NO_FX|​APP_MINIMIZABLE|APP_EXIT_IMMEDIATELY;
    DataPath("Data");

    server.Init();

    while(server.Update()){};

    server.Shutdown();
}
/******************************************************************************/
Bool Init(){return false;}
/******************************************************************************/
void Shut(){}
/******************************************************************************/
Bool Update(){return false;}
/******************************************************************************/
void Draw(){}

Code:
Server.cpp

Bool Server::Update()
{
    deltaTime = Time.curTime() - last_time;

       //Here deltaTime is negative sometimes due to Time.curTime().

    last_time = Time.curTime();

    // Update Npcs.
    FREPA(ServerNpcs)
    {
        ServerNpcs[i].update();
    }

    // Update NetManager
    netmngr.update();

    return true;
}

Quote:On what platform are you experiencing this issue? (do other platforms work ok?)

Windows 7. I didn't check in other platforms.

IRC: irc.freenode.net
Channel: #Esenthel
(This post was last modified: 12-22-2012 03:58 PM by cmontiel.)
12-22-2012 03:57 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: Time.curTime() bug
I've tested following code and could not reproduce the problem
Code:
Flt deltaTime, last_time;
Bool update()
{
   deltaTime = Time.curTime() - last_time;

   //Here deltaTime is negative sometimes due to Time.curTime().
   if(deltaTime<0)
   {
      int z=0; // it never enters here
   }

   last_time = Time.curTime();
   return true;
}
void InitPre()
{
      while(update()){};
}
Is your last_time value not initialized to zero in constructor of the class?
12-23-2012 02:07 PM
Find all posts by this user Quote this message in a reply
cmontiel Offline
Member

Post: #5
RE: Time.curTime() bug
Yes, deltaTime = 0.f; , last_time = 0.f; in constructor.

I added :

if(deltaTime<0)
{
int z=0; // it never enters here
}

And a breakpoint at int z=0, compiler stops there with deltatime negative value with lots of decimals.

It occurs passed 4 or 5 minutes, sometimes passed 40 minutes. Totally random.

At moment I am using function clock() suggested by SkyKill

Code:
deltaTime = static_cast<double>(clock() - last_time) / CLOCKS_PER_SEC;
last_time = clock();

It seems works well.

IRC: irc.freenode.net
Channel: #Esenthel
12-23-2012 07:01 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #6
RE: Time.curTime() bug
Time.curTime() most certainly does not have a bug
If you're experiencing this issue, it can be caused by numerical imprecisions, conversions between Dbl and Flt

try using this code:
Code:
Flt delta, last_time;
Bool update()
{
   Flt cur_time=Time.curTime(); // first copy to backup variable
   delta=cur_time-last_time;

   if(delta<0)
   {
      Exit(S+(Dbl)delta+"\n"+(Dbl)cur_time+"\n"+(Dbl)last_time);
   }

   last_time=cur_time; // use cur_time
   return true;
}
if this still won't work, then replace Flt with Dbl for time variables
12-23-2012 07:35 PM
Find all posts by this user Quote this message in a reply
cmontiel Offline
Member

Post: #7
RE: Time.curTime() bug
Quote:Flt cur_time=Time.curTime(); // first copy to backup variable

Solved! That's the key line. Strange because delta and last_time are floats and Time.curTime() returns float too.

IRC: irc.freenode.net
Channel: #Esenthel
12-23-2012 10:57 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #8
RE: Time.curTime() bug
Cpu's work that way that returned value of a floating point function is always treated as Dbl until it's converted into a Flt variable.
This is because returned values of function are accessed from cpu fpu hw registers which are at least Dbl in precision.
12-24-2012 10:53 AM
Find all posts by this user Quote this message in a reply
Post Reply