[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

adding timeval's

Joe, G.I.

10/26/2008 3:14:00 PM

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);
}
5 Answers

Joe, G.I.

10/26/2008 4:03:00 PM

0

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);
}

red floyd wrote:
> Joe wrote:
>> 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.
>
>>
>> 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);
>> }

red floyd

10/26/2008 8:30:00 PM

0

Joe wrote:
> 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.

>
> 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);
> }

Kai-Uwe Bux

10/26/2008 9:30:00 PM

0

Joe wrote:

> 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

Ian Collins

10/26/2008 9:41:00 PM

0

red floyd wrote:
> Joe wrote:
>> 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.
>
Or long long if your system has it.

--
Ian Collins

James Kanze

10/28/2008 9:31:00 AM

0

On Oct 26, 10:40 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> red floyd wrote:
> > Joe wrote:
> >> 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.

> 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).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34