what is wrong with the function?

Discussion in 'C Programming' started by yezi, Dec 9, 2005.

  1. yezi

    yezi Guest

    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.
     
    yezi, Dec 9, 2005
    #1
    1. Advertising

  2. 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)
     
    Richard Heathfield, Dec 9, 2005
    #2
    1. Advertising

  3. yezi

    Ian Malone Guest

    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
     
    Ian Malone, Dec 9, 2005
    #3
  4. yezi

    yezi Guest

    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.
     
    yezi, Dec 9, 2005
    #4
  5. yezi

    Ian Malone Guest

    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
     
    Ian Malone, Dec 9, 2005
    #5
  6. yezi

    Flash Gordon Guest

    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.
     
    Flash Gordon, Dec 9, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    7
    Views:
    744
    Priscilla Walmsley
    Jan 7, 2005
  2. James Vanns
    Replies:
    7
    Views:
    7,089
    Evan Carew
    Jan 21, 2004
  3. komal
    Replies:
    6
    Views:
    1,445
    msalters
    Jan 25, 2005
  4. Replies:
    2
    Views:
    944
    Bengt Richter
    Aug 1, 2005
  5. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,261
    Barry Schwarz
    Sep 5, 2005
Loading...

Share This Page