Thread: problem with my time code

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    problem with my time code

    It prints same values for in all the loop.
    and it doesn't add properly new_time.time and new_time.millitm
    meaning,
    when I print them separately , their value added together doesnt match up with last_time.
    Please help me , where am I going wrong (
    Code:
    void time()
    {
    	struct timeb new_time;
    	double  last_time,latest_time, interval_time = 0, real_interval_time;
    
    	ftime(&new_time); //init purpose
    	last_time =(double)new_time.time+(double)(new_time.millitm * 0.001 );
    	printf("last_time=%lf\n",last_time);
    
    	while(1)
    	{
    		ftime(&new_time);
    		latest_time=(double)new_time.time+(double)(new_time.millitm * 0.001);
    		printf("latest_time=%lf\t",latest_time);
    
    		real_interval_time = latest_time - last_time;
    		printf("rit=%lf\t",real_interval_time);
    		interval_time += real_interval_time;
    		printf("it=%lf\n",interval_time);
    		last_time = latest_time;
    	}
    	
    
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It works fine for me. In one loop not enough time passes, so in order for the "it" to change you need like 30 iterations or sth. "rit" is almost always 0 or like 0.01.

    In any case this is surely better:
    Code:
    last_time =(double)(1000*new_time.time)+(double)(new_time.millitm * 0.001 );
    ...
    latest_time=(double)(1000*new_time.time)+(double)(new_time.millitm);
    printf("it=%lf\n",interval_time / 1000);
    since you don't lose accuracy by dividing multiplying by 0.001!

    So it is always the same for you?

    EDIT: Also note that time() already exists in so you might want a different name <sys/time.h>
    Last edited by C_ntua; 10-21-2008 at 01:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. Pointer problem... i think
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 11-30-2005, 03:45 PM
  3. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  4. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM