473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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*(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
5 1491
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
4
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.