adding timeval's

J

Joe

I'm trying to add the current time + a randomly generated time w/
millisecond precision, but the numbers aren't making sense.

If I add rand_time + now_time on my calculator, I get a different result
than what's in sched_time. Anyone see what I'm doing wrong?

These are my results ..

rand_time = 4958.128418
now_time = 1225033344.000000
sched_time = 1225038336.000000
(my calc says rand_time + now_time = 1225038302.13)

struct timeval now;

gettimeofday(&now, NULL);

float rand_time = random_time();
float now_time = now.tv_sec + (now.tv_usec / 1000000.0);
float sched_time = now_time + rand_time;

printf("rand_time = %f\n", rand_time);
printf("now_time = %f\n", now_time);
printf("sched_time = %f\n", sched_time);



float random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}
 
J

Joe

Ok, I changed them to doubles, and have another question if I may.

I'm randomly generating a value between 0 and 5 seconds (or 5000
milliseconds). Am I properly adding the number of milliseconds to the
current time. Somehow I think I'm adding them wrongly and the sched_time
(future time) is turning out to be much longer in time that 0-5 seconds
from the current_time (now_time).

struct timeval now;

gettimeofday(&now, NULL);

double rand_time = random_time();
double now_time = now.tv_sec + (now.tv_usec / 1000000.0);
double sched_time = now_time + rand_time;

double random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}
 
R

red floyd

Joe said:
I'm trying to add the current time + a randomly generated time w/
millisecond precision, but the numbers aren't making sense.

If I add rand_time + now_time on my calculator, I get a different result
than what's in sched_time. Anyone see what I'm doing wrong?
Yeah, you're using floats, which on most platforms don't have enough
precision to deal with your data. Try using doubles instead.
 
K

Kai-Uwe Bux

Joe said:
Ok, I changed them to doubles, and have another question if I may.

I'm randomly generating a value between 0 and 5 seconds (or 5000
milliseconds). Am I properly adding the number of milliseconds to the
current time. Somehow I think I'm adding them wrongly and the sched_time
(future time) is turning out to be much longer in time that 0-5 seconds
from the current_time (now_time).

struct timeval now;

gettimeofday(&now, NULL);

double rand_time = random_time();
double now_time = now.tv_sec + (now.tv_usec / 1000000.0);
double sched_time = now_time + rand_time;

double random_time()
{
int const MAX_MILLISECONDS = 5000;

return (rand() / (RAND_MAX + 1.0) * MAX_MILLISECONDS);
}

The function random_time() will return a (non-uniform) value in [0,5000). If
you interpret that as seconds, then you are adding up to 5000 seconds to
your time. This is probably not what you want.

Hint: If you want to use floating point arithmetic, settle on a dimension
(e.g., seconds) and stick with it. Maybe, you should remind yourself using
a typedef:

typedef double time_in_seconds;

Also instead of building the bound into the function, you could do (still
non-uniform):

time_in_seconds random_time ( time_in_seconds upper ) {
return( rand() * upper / ( RAND_MAX + 1.0 ) );
}

and use random_time( 5.0 ) within client code.


[quote snipped since it is immaterial]

a) Please quote only relevant material.

b) Please do not top-post.


Best

Kai-Uwe Bux
 
I

Ian Collins

Yeah, you're using floats, which on most platforms don't have enough
precision to deal with your data. Try using doubles instead.
Or long long if your system has it.
 
J

James Kanze

Or long long if your system has it.

If his system has gettimeofday, it has long long:).

Personally, of course, I'd have just stuck with the timeval;
it's not that difficult to do a correct addition in base
1000000 (since you're pretty much guaranteed that adding two
values < 1000000 will not overflow).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top