Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > what is wrong with the function?

Reply
Thread Tools

what is wrong with the function?

 
 
yezi
Guest
Posts: n/a
 
      12-09-2005
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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-09-2005
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)
 
Reply With Quote
 
 
 
 
Ian Malone
Guest
Posts: n/a
 
      12-09-2005
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 4.

> 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
 
Reply With Quote
 
yezi
Guest
Posts: n/a
 
      12-09-2005
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.

 
Reply With Quote
 
Ian Malone
Guest
Posts: n/a
 
      12-09-2005
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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-09-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Have I bought wrong product? enquirer Wireless Networking 2 06-10-2005 10:59 PM
Zero Config keeps connecting to the wrong AP =?Utf-8?B?ZGdyaWZmaXRo?= Wireless Networking 2 03-04-2005 05:52 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
wrong connection status Peter Welk Wireless Networking 0 12-22-2004 03:26 PM
XP SP2 Wrong IP on connection D Wells Wireless Networking 3 12-09-2004 03:35 AM



Advertisments