what is wrong with the function?

Y

yezi

HI , all :

I want to control the event gap onto microsecond level , so I called
the function :

nanosleep(&req, &req);

according to the description of this function, I should set the

The structure timespec is used to specify intervals of time with
nanosecond precision. It is specified in <time.h> and has the
form


struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};


The value of the nanoseconds field must be in the range 0 to
999 999 999.

The code to control tv_nsec is:
float random;
int perCentage = 10;
int baseGap = 20000;

random = drand48();
req.tv_nsec = baseGap*(1- (perCentage/100) +
random*2*(perCentage/100));
printf("the time is %f",req.tv_nsec);

The output is :
baseGap is 20000
the random number is 8.700579e-01
the time is 1.000000395

My question is the time suppose is :

20000*(1- 0.1 + 2*0.8*0.1);


should around 20000.

BUt the value is not .

Why, thanks for any hint.
 
R

Richard Heathfield

yezi said:
HI , all :

I want to control the event gap onto microsecond level , so I called
the function :

nanosleep(&req, &req);

Try comp.unix.programmer - and check their FAQs first, just in case.
 
I

Ian Malone

yezi said:
HI , all :

I want to control the event gap onto microsecond level , so I called
the function :

nanosleep(&req, &req);

nanosleep is POSIX I believe, so questions specific to its use should
probably go to comp.unix.programmer, however nanosleep itself is
not your problem.
according to the description of this function, I should set the

The structure timespec is used to specify intervals of time with
nanosecond precision. It is specified in <time.h> and has the
form


struct timespec
{
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};


The value of the nanoseconds field must be in the range 0 to
999 999 999.

The code to control tv_nsec is:
float random;
int perCentage = 10;
int baseGap = 20000;

random = drand48();
req.tv_nsec = baseGap*(1- (perCentage/100) +
random*2*(perCentage/100));

perCentage is an int and 100 is an integer constant. perCentage/100 = 0
You must cast one of them to a float type, although you may want to
consider making percentage a float type to start with.

Is there any reason to prefer
float random;
over
double random;
? Especially since the name of drand48() suggests to me that it gives
48bits precision (although it could equally mean it was written in
1948, or that it always returns 48).
printf("the time is %f",req.tv_nsec);


The output is :
the time is 1.000000395

"%f" is the specifier for a float. req.tv_nseq is a long int (assuming
req is an instance of timespec), you want "%li".
 
Y

yezi

Well, I try the "%li" and "%ld" the situation is more weird .

This time it shows :

the time is 200008
the baseGap is 20000.000000


Even 10 times of supposed value.
 
I

Ian Malone

yezi said:
Well, I try the "%li" and "%ld" the situation is more weird .

This time it shows :

the time is 200008
the baseGap is 20000.000000


Even 10 times of supposed value.

First: <http://cfaj.freeshell.org/google/>

Second I suppose it depends on the actual code: do you
have a small complete program that does this?
 
F

Flash Gordon

yezi said:
Well, I try the "%li" and "%ld" the situation is more weird .

This time it shows :

the time is 200008
the baseGap is 20000.000000

Even 10 times of supposed value.

Then either you have an error calculating them or something else wrong.

Provide context with you posts, particularly important here since I
can't remember what you code was and won't bother looking it up
(assuming you have posted it before). See http://cfaj.freeshell.org/google/
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top