By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

what is wrong with the function?

P: n/a
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.

Dec 9 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 9 '05 #2

P: n/a
yezi wrote:
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".

--
imalone
Dec 9 '05 #3

P: n/a
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.

Dec 9 '05 #4

P: n/a
yezi wrote:
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?

--
imalone
Dec 9 '05 #5

P: n/a
yezi wrote:
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/
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 9 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.