⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 faqcat1067.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<hr><hr><hr><a name="fprand"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/fprand.html"><!-- qtag -->Question 13.19b</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I generate floating-point random numbers?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat1067.html?sec=lib#rand48">13.21</a> for some examples.<hr><hr><hr><a name="gaussian"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/gaussian.html"><!-- qtag -->Question 13.20</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can I generate random numbers with anormal orGaussian distribution?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>There are a number of ways of doing this.</p><OL><li>ExploittheCentral Limit Theorem(``law of large numbers'')and add up several uniformly-distributed random numbers:<pre>#include &lt;stdlib.h&gt;#include &lt;math.h&gt;#define NSUM 25double gaussrand(){	double x = 0;	int i;	for(i = 0; i &lt; NSUM; i++)		x += (double)rand() / RAND_MAX;	x -= NSUM / 2.0;	x /= sqrt(NSUM / 12.0);	return x;}</pre>(Don't overlook the <TT>sqrt(NSUM&nbsp;/&nbsp;12.)</TT> correction,thoughit's easy to do so accidentally,especially when <TT>NSUM</TT> is 12.)<li>Use a method described byAbramowitzandStegun:<pre>#include &lt;stdlib.h&gt;#include &lt;math.h&gt;#define PI 3.141592654double gaussrand(){	static double U, V;	static int phase = 0;	double Z;	if(phase == 0) {		U = (rand() + 1.) / (RAND_MAX + 2.);		V = rand() / (RAND_MAX + 1.);		Z = sqrt(-2 * log(U)) * sin(2 * PI * V);	} else		Z = sqrt(-2 * log(U)) * cos(2 * PI * V);	phase = 1 - phase;	return Z;}</pre><li>Use amethoddiscussedinKnuthand due originallyto Marsaglia:<pre>#include &lt;stdlib.h&gt;#include &lt;math.h&gt;double gaussrand(){	static double V1, V2, S;	static int phase = 0;	double X;	if(phase == 0) {		do {			double U1 = (double)rand() / RAND_MAX;			double U2 = (double)rand() / RAND_MAX;			V1 = 2 * U1 - 1;			V2 = 2 * U2 - 1;			S = V1 * V1 + V2 * V2;			} while(S &gt;= 1 || S == 0);		X = V1 * sqrt(-2 * log(S) / S);	} else		X = V2 * sqrt(-2 * log(S) / S);	phase = 1 - phase;	return X;}</pre></OL>Thesemethodsallgenerate numbers with mean 0and standard deviation1.(To adjust to someother distribution,multiply bythe standard deviationand add the mean.)Method 1 is poor ``in the tails''(especially if <TT>NSUM</TT> is small),but methods 2 and 3 performquitewell.See the references for more information.<p><a href="../../lib/sd16.html" rel=subdocument>Additional links</a></p><p>References:Knuth Sec. 3.4.1 p. 117<br>Box and Muller, ``A Note on the Generation of Random Normal Deviates''<br>Marsaglia and Bray, ``A Convenient Method for Generating Normal Variables''<br>Abramowitz and Stegun, <I>Handbook of Mathematical Functions</I><br>Press et al., <I>Numerical Recipes in C</I> Sec. 7.2 pp. 288-290<hr><hr><hr><a name="rand48"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/rand48.html"><!-- qtag -->Question 13.21</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm porting this program, and it calls a routine<TT>drand48</TT>,which my library doesn't have.What isit?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font><TT>drand48</TT>is aUnix System Vroutinewhichreturns floating point random numbers(presumablywith 48 bits of precision)in the half-open interval[0,&nbsp;1)<a href="../../lib/fn76.html" rel=subdocument>[footnote]</a>.(Its companion seed routine is <TT>srand48</TT>;neither is in the C Standard.)It's easy to write a low-precision replacement:<pre>#include &lt;stdlib.h&gt;double drand48(){	return rand() / (RAND_MAX + 1.);}</pre></p><p>To more accurately simulate <TT>drand48</TT>'s semantics,you can try to give it closer to48 bitsworth of precision:<pre>#define PRECISION 2.82e14	/* 2**48, rounded up */double drand48(){	double x = 0;	double denom = RAND_MAX + 1.;	double need;	for(need = PRECISION; need &gt; 1;			need /= (RAND_MAX + 1.)) {		x += rand() / denom;		denom *= RAND_MAX + 1.;	}	return x;}</pre></p><p>Before using code like this, though,beware that it is numerically suspect,particularly if(as is usually the case)the period of <TT>rand</TT> ison the order ofRAND_MAX.(If you have a longer-period random number generator available,such as BSD <TT>random</TT>,definitelyuse itwhen simulating <TT>drand48</TT>.)</p><p>References:PCS Sec. 11 p. 149<hr><hr><hr><a name="exitvsreturn"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/exitvsreturn.html"><!-- qtag -->Question 13.22</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Is <TT>exit(status)</TT> truly equivalentto returning the same <TT>status</TT> from <TT>main</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#exitvsreturn">11.16</a>.<hr><hr><hr><a name="memmove"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/memmove.html"><!-- qtag -->Question 13.23</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's the difference between<TT>memcpy</TT> and<TT>memmove</TT>?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcat7d4b.html?sec=ansi#memmove">11.25</a>.<hr><hr><hr><a name="oldlibfcns"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/oldlibfcns.html"><!-- qtag -->Question 13.24</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm trying to port this old program.Why do I get ``undefined external'' errorsfor some library functions?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Someold orsemistandard functions have been renamed or replaced over the years;<br>if you need:/you should instead:<dl><dt><TT>index</TT></dt><dd>use <TT>strchr</TT>.</dd><dt><TT>rindex</TT></dt><dd>use <TT>strrchr</TT>.</dd><dt><TT>bcopy</TT></dt><dd>use<TT>memmove</TT>,after interchanging the first and second arguments(see also question<a href="faqcat7d4b.html?sec=ansi#memmove">11.25</a>).</dd><dt><TT>bcmp</TT></dt><dd>use <TT>memcmp</TT>.</dd><dt><TT>bzero</TT></dt><dd>use <TT>memset</TT>,with a second argument of 0.</dd></dl></p><p>See also questions <a href="faqcat1d60.html?sec=stdio#sprintfret">12.22</a>and <a href="faqcat1067.html?sec=lib#rand48">13.21</a>.</p><p>References:PCS Sec. 11<hr><hr><hr><a name="extlibs"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/extlibs.html"><!-- qtag -->Question 13.25</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Ikeep gettingerrors due to library functions being undefined,but I'm #including allthe right header files.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>In the general case of calling code in anexternal library,using <TT>#include</TT> to pull inthe right header file(s) is only halfof the story; you also have to tell the linker to search theexternal library itself.The declarations in the header fileonly tell the compiler how to call the external functions;the header file doesn't supply the definitions of the externalfunctions, or tell the compiler/linker where to find thosedefinitions.</p><p>In some cases(especially if the functions are nonstandard)obtainingthose definitionsmay require explicitly askingfor the correct libraries to besearchedwhen you link the program.(Some systems may be able to arrangethat whenever you <TT>#include</TT> a header,its associated library, if nonstandard,is automatically requested at link time,but such a facility is notwidespread.)See also questions<a href="faqcat204f.html?sec=cpp#missinghdr">10.11</a>,<a href="faqcat7d4b.html?sec=ansi#preansilib">11.30</a>,<a href="faqcat1067.html?sec=lib#libsearch">13.26</a>,<a href="faqcat973e.html?sec=fp#libm">14.3</a>,and<a href="faqcatea63.html?sec=osdep#networking">19.40</a>.<hr><hr><hr><a name="libsearch"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/libsearch.html"><!-- qtag -->Question 13.26</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm still getting errors due to library functions being undefined,even though I'mexplicitlyrequesting the right librarieswhile linking.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Manylinkers make one passover the list of object files and libraries you specify,and extract from librariesonly those moduleswhich satisfy referenceswhich have so far come up as undefined.Therefore, theorder in which libraries arelistedwith respect to object files(and each other)is significant;usually,youwant tosearch the libraries last.</p><p>For example,underUnix,a command line like<pre>	cc -lm myprog.c		# WRONG</pre>usually won't work.Instead,put any <TT>-l</TT> optionsatthe end of the command line:<pre>	cc myprog.c -lm</pre></p><p>If you list a library first,the linker doesn't know that itneeds anything out of it yet,andpasses it by.See also question <a href="faqcat1067.html?sec=lib#endundef">13.28</a>.<hr><hr><hr><a name="bloat"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/bloat.html"><!-- qtag -->Question 13.27</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why is my simple program,which hardly does more than print ``Hello, world!''in a window,compiling to such a huge executable(several hundred K)?Should I <TT>#include</TT> fewer header files?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>What you're seeing is the current(poor)state of the ``art'' in library design.As run-time libraries accrete more and more features(especiallyhaving to dowithGraphical User Interfaces),and when one library function calls another library function to do part of its job(which ought to be a Good Thing;that's what library functions are for),itcan happenthatcalling anything in the library(particularly something relatively powerful like <TT>printf</TT>)eventually pulls in practically everythingelse,leading to horribly bloated executables.</p><p>#including fewer header files probably won't help,because declaring a few functions which you don't call(which is mostly all that happens when you #include a header you don't need)shouldn't result in those functions being placed in your executable,unlessthey actually do get called.See also question <a href="faqcat1067.html?sec=lib#extlibs">13.25</a>.</p><p>You may be able to track downand derailachainof unnecessarily-coupled functions which are bloating your executable,or maybe complain to your vendor to clean up the libraries.</p><p>References:H&amp;S Sec. 4.8.6 pp. 103-4<hr><hr><hr><a name="endundef"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/endundef.html"><!-- qtag -->Question 13.28</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What does it mean when the linker says that <TT>_end</TT> is undefined?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>That messageis a quirk of the old Unix linkers.Yougetan error about <TT>_end</TT> being undefinedonlywhen othersymbolsare undefined,too--fixthe others,and the error about <TT>_end</TT> will disappear.(See also questions <a href="faqcat1067.html?sec=lib#extlibs">13.25</a>and <a href="faqcat1067.html?sec=lib#libsearch">13.26</a>.)<hr><hr><hr><a name="noprintf"><h1>comp.lang.c FAQ list<font color=blue>&middot;</font><a href="../../lib/noprintf.html"><!-- qtag -->Question 13.29</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>My compiler is complaining that <TT>printf</TT> is undefined!How can this be?It's the world's most popular C function...</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Allegedly, there are C compilers for Microsoft Windowswhich do not support <TT>printf</TT>,on the argument that<TT>printf</TT> is for printing to old-fashioned terminals,while under Windows the right way to display textis to call xxx to open a windowand then xxx to display text in it.It may be possible to convince such a compilerthat what you are writing is a``console application''meaning that it willtake care of openinga ``console window''for you automatically,and condescend to let you call <TT>printf</TT> to print stuff to it.</p><p>See also question <a href="faqcatea63.html?sec=osdep#waitforkey">19.4b</a>.<hr><hr><hr><hr><p>Read sequentially:<a href="faqcat1d60.html?sec=stdio" rev=precedes>prev</a><a href="faqcat973e.html?sec=fp" rel=precedes>next</a><a href="faqcat.html" rev=subdocument>up</a></p><hr><p><br><!-- lastfooter --><a href="../../about.html">about this FAQ list</a>&nbsp;<a href="../../eskimo.html">about eskimo</a>&nbsp;<a href="../../search.html">search</a>&nbsp;<a href="../../feedback.html">feedback</a>&nbsp;<a href="../../lib/copyright.html">copyright</a><p>Hosted by<a href="http://www.eskimo.com/"><img src="../../../www.eskimo.com/img/link/eskitiny.gif" alt="Eskimo North"></a></body><!-- Mirrored from c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=lib by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:18 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -