what is wrong with the function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yezi

    #1

    what is wrong with the function?

    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*(perCe ntage/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.

  • Richard Heathfield

    #2
    Re: what is wrong with the function?

    yezi said:
    [color=blue]
    > HI , all :
    >
    > I want to control the event gap onto microsecond level , so I called
    > the function :
    >
    > nanosleep(&req, &req);[/color]

    Try comp.unix.progr ammer - and check their FAQs first, just in case.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • Ian Malone

      #3
      Re: what is wrong with the function?

      yezi wrote:[color=blue]
      > HI , all :
      >
      > I want to control the event gap onto microsecond level , so I called
      > the function :
      >
      > nanosleep(&req, &req);[/color]

      nanosleep is POSIX I believe, so questions specific to its use should
      probably go to comp.unix.progr ammer, however nanosleep itself is
      not your problem.
      [color=blue]
      >
      > 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*(perCe ntage/100));[/color]

      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).
      [color=blue]
      > printf("the time is %f",req.tv_nsec );
      >[/color]

      [color=blue]
      > The output is :[/color]
      [color=blue]
      > the time is 1.000000395
      >[/color]

      "%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

      Comment

      • yezi

        #4
        Re: what is wrong with the function?

        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.

        Comment

        • Ian Malone

          #5
          Re: what is wrong with the function?

          yezi wrote:[color=blue]
          > 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.
          >[/color]

          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

          Comment

          • Flash Gordon

            #6
            Re: what is wrong with the function?

            yezi wrote:[color=blue]
            > 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.[/color]

            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.

            Comment

            Working...