📄 faqcat973e.html
字号:
<TT>floor</TT> and <TT>ceil</TT>functions.)</p><p>You can round to a certain precision by scaling:<pre> (int)(x / precision + 0.5) * precision</pre>Handling negative numbers,orimplementingeven/odd rounding,is slightly trickier.</p><p>Note thatbecause truncation is otherwise the default,it'susuallya good idea to use an explicit roundingstepwhen converting floating-point numbers to integers.Unless you're careful,it'squite possiblefor a number which you thought was 8.0to be represented internally as 7.999999and to be truncated to 7.</p><p>Additional links:<a href="../../fp/sd17.html" rel=subdocument>further reading</a><hr><hr><hr><a name="pow"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../fp/pow.html"><!-- qtag -->Question 14.7</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>Why doesn't C have anexponentiation operator?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>One reason isprobablythatfew processors havea built-inexponentiation instruction.C has a<TT>pow</TT> function(declared in <TT><math.h></TT>)for performing exponentiation,although explicit multiplication is usually betterfor small positive integral exponents.<a href="../../fp/fn78.html" rel=subdocument>[footnote]</a>In other words, <TT>pow(x, 2.)</TT>is probably inferiorto<TT>x * x</TT>.(If you're tempted to makea <TT>Square()</TT> macro,though,check question <a href="faqcat204f.html?sec=cpp#safemacros">10.1</a> first.)</p><p>References:ISO Sec. 7.5.5.1<br>H&S Sec. 17.6 p. 393<hr><hr><hr><a name="mpi"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../fp/mpi.html"><!-- qtag -->Question 14.8</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>The predefined constant <TT>M_PI</TT>seems to be missing frommy machine's copy of<TT><math.h></TT>.</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>That constant(which is apparently supposed to be the value of pi,accurate to the machine's precision),is notstandard;in fact a standard-conforming copy of <TT><math.h></TT>should<em>not</em> <TT>#define</TT> a symbol <TT>M_PI</TT>.<a href="../../fp/fn79.html" rel=subdocument>[footnote]</a>If you need pi, you'll have to define it yourself,or compute it with <TT>4*atan(1.0)</TT>or<TT>acos(-1.0)</TT>.(You could useaconstructionlike<pre> #ifndef M_PI #define M_PI 3.1415926535897932385 #endif</pre>to provide your own #definitiononly if some system header file has not.)</p><p>References:PCS Sec. 13 p. 237<hr><hr><hr><a name="nan"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../fp/nan.html"><!-- qtag -->Question 14.9</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How do Iset variables to, ortest forIEEE NaN(``Not a Number'')and other special values?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Many systemswith high-quality IEEE floating-point implementations providefacilities(e.g.predefined constants,and functionslike<TT>isnan()</TT>,eitheras nonstandard extensionsin <TT><math.h></TT>or perhapsin<TT><ieee.h></TT>or<TT><nan.h></TT>)to deal with these values cleanly,andwork is being doneto formally standardize such facilities.A crude but usually effective test for NaNcan be written based on the fact that IEEE NaN'snever compare equal to anything,even themselves;thereforea number that doesn't compare equal to itselfmust be a NaN:<pre> #define isnan(x) ((x) != (x))</pre>Beware, though, thatnon-IEEE-aware compilers mayoptimize the test away.(Note also thateven if you do have a predefined constant like <TT>NAN</TT>,you <em>cannot</em> use it in comparisons like <TT>if(x == NAN)</TT>,again becauseNaN's do not compare equal to themselves.)</p><p>C99 provides<TT>isnan()</TT>,<TT>fpclassify()</TT>,and several other classification routines.</p><p>Anotherpossibilityisto formatthe value in questionusing<TT>sprintf</TT>:onmany systemsitgeneratesstrings like <TT>"NaN"</TT> and <TT>"Inf"</TT>which you could compare forin a pinch.</p><p>To initialize variables with these values(and if your system does not provide cleaner solutions),you may be able to get away with some compile-time``arithmetic'':<pre> double nan = 0./0.; double inf = 1./0.;</pre>Don't be toosurprised,though,if these don't work(or if they abort the compiler with a floating-point exception).</p><p>(The most reliable way of setting up these special valueswould use a hex representation of their internal bit patterns,but initializing a floating-point value with a bit patternwould require using a union or some other type punning mechanismandwould obviously bemachine-dependent.)</p><p>See also question <a href="faqcatea63.html?sec=osdep#fpexcepts">19.39</a>.</p><p>References:C9X Sec. 7.7.3<hr><hr><hr><a name="fpexcepts"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../fp/fpexcepts.html"><!-- qtag -->Question 14.10</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>How can Ihandlefloating-point exceptions gracefully?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>See question <a href="faqcatea63.html?sec=osdep#fpexcepts">19.39</a>.<hr><hr><hr><a name="complex"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../fp/complex.html"><!-- qtag -->Question 14.11</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>What's a good way to implement complex numbers in C?</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>It is straightforward to define a simple structureand some arithmetic functions to manipulate them.C99 supports <TT>complex</TT> as a standard type.<a href="../../fp/fn80.html" rel=subdocument>[footnote]</a>Here is atinyexample,to give you a feel for it:<pre>typedef struct { double real; double imag; } complex;#define Real(c) (c).real#define Imag(c) (c).imagcomplex cpx_make(double real, double imag){ complex ret; ret.real = real; ret.imag = imag; return ret;}complex cpx_add(complex a, complex b){ return cpx_make(Real(a) + Real(b), Imag(a) + Imag(b));}</pre>You can use these routineswith code like<pre> complex a = cpx_make(1, 2); complex b = cpx_make(3, 4); complex c = cpx_add(a, b);</pre>or, even more simply,<pre> complex c = cpx_add(cpx_make(1, 2), cpx_make(3, 4));</pre></p><p>See alsoquestions <a href="faqcat6b6b.html?sec=struct#firstclass">2.7</a>, <a href="faqcat6b6b.html?sec=struct#anonstruct">2.10</a>, and <a href="faqcat973e.html?sec=fp#mathsrc">14.12</a>.</p><p>References:C9X Sec. 6.1.2.5, Sec. 7.8<hr><hr><hr><a name="mathsrc"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../fp/mathsrc.html"><!-- qtag -->Question 14.12</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm looking for somecode to do:</p><p>Fast Fourier Transforms (FFT's)<br>matrix arithmetic (multiplication, inversion, etc.)<br>complex arithmetic<br></p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Ajay Shahhas prepared a niceindex of free numericalsoftwarewhich has been archived pretty widely;one URLis<a href="ftp://ftp.math.psu.edu/pub/FAQ/numcomp-free-c"><TT>ftp://ftp.math.psu.edu/pub/FAQ/numcomp-free-c</TT></a>.See alsoquestions <a href="faqcatccbd.html?sec=resources#tutsources">18.9b</a>, <a href="faqcatccbd.html?sec=resources#stdlibsrc">18.13</a>, <a href="faqcatccbd.html?sec=resources#miscsrcs">18.15c</a>, and <a href="faqcatccbd.html?sec=resources#sources">18.16</a>.<hr><hr><hr><a name="fpnotlinked"><h1>comp.lang.c FAQ list<font color=blue>·</font><a href="../../fp/fpnotlinked.html"><!-- qtag -->Question 14.13</a></h1><p><font face=Helvetica size=8 color=blue><b>Q:</b></font>I'm having trouble with a Turbo C program which crashes and sayssomething like``floating point formats not linked.''</p><p><hr><p><font face=Helvetica size=8 color=blue><b>A:</b></font>Some compilers for small machines, includingTurbo C(and Ritchie's original PDP-11 compiler),leave outcertainfloating point support if it looks like it will not be needed.In particular, the non-floating-point versionsof <TT>printf</TT>and <TT>scanf</TT>save space by not includingcode to handle <TT>%e</TT>, <TT>%f</TT>,and <TT>%g</TT>.It happens thatBorland'sheuristics for determiningwhether the program uses floating pointare insufficient,and theprogrammer mustsometimesinserta dummycallto a floating-point library function(such as <TT>sqrt</TT>; any willdo)to force loading offloating-point support.(See the comp.os.msdos.programmer FAQ list for moreinformation.)</p><p>A partially-related problem,resulting in a similar error message(perhaps ``floating point not loaded'')can apparently occurundersomeMS-DOS compilerswhenan incorrect variant of the floating-point library is linked.Check your compilermanual'sdescription of the variousfloating-point libraries.</p><p>Additional links:<a href="../../fp/sd18.html" rel=subdocument>another possibility</a><hr><hr><hr><hr><p>Read sequentially:<a href="faqcat1067.html?sec=lib" rev=precedes>prev</a><a href="faqcat744e.html?sec=varargs" 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> <a href="../../eskimo.html">about eskimo</a> <a href="../../search.html">search</a> <a href="../../feedback.html">feedback</a> <a href="../../fp/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=fp by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:58:20 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -