math.texi 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. @c We need some definitions here.
  2. @ifclear mult
  3. @ifhtml
  4. @set mult ·
  5. @set infty ∞
  6. @set pie π
  7. @end ifhtml
  8. @iftex
  9. @set mult @cdot
  10. @set infty @infty
  11. @end iftex
  12. @ifclear mult
  13. @set mult *
  14. @set infty oo
  15. @set pie pi
  16. @end ifclear
  17. @macro mul
  18. @value{mult}
  19. @end macro
  20. @macro infinity
  21. @value{infty}
  22. @end macro
  23. @ifnottex
  24. @macro pi
  25. @value{pie}
  26. @end macro
  27. @end ifnottex
  28. @end ifclear
  29. @node Mathematics, Arithmetic, Syslog, Top
  30. @c %MENU% Math functions, useful constants, random numbers
  31. @chapter Mathematics
  32. This chapter contains information about functions for performing
  33. mathematical computations, such as trigonometric functions. Most of
  34. these functions have prototypes declared in the header file
  35. @file{math.h}. The complex-valued functions are defined in
  36. @file{complex.h}.
  37. @pindex math.h
  38. @pindex complex.h
  39. All mathematical functions which take a floating-point argument
  40. have three variants, one each for @code{double}, @code{float}, and
  41. @code{long double} arguments. The @code{double} versions are mostly
  42. defined in @w{ISO C89}. The @code{float} and @code{long double}
  43. versions are from the numeric extensions to C included in @w{ISO C99}.
  44. Which of the three versions of a function should be used depends on the
  45. situation. For most calculations, the @code{float} functions are the
  46. fastest. On the other hand, the @code{long double} functions have the
  47. highest precision. @code{double} is somewhere in between. It is
  48. usually wise to pick the narrowest type that can accommodate your data.
  49. Not all machines have a distinct @code{long double} type; it may be the
  50. same as @code{double}.
  51. @Theglibc{} also provides @code{_Float@var{N}} and
  52. @code{_Float@var{N}x} types. These types are defined in @w{ISO/IEC TS
  53. 18661-3}, which extends @w{ISO C} and defines floating-point types that
  54. are not machine-dependent. When such a type, such as @code{_Float128},
  55. is supported by @theglibc{}, extra variants for most of the mathematical
  56. functions provided for @code{double}, @code{float}, and @code{long
  57. double} are also provided for the supported type. Throughout this
  58. manual, the @code{_Float@var{N}} and @code{_Float@var{N}x} variants of
  59. these functions are described along with the @code{double},
  60. @code{float}, and @code{long double} variants and they come from
  61. @w{ISO/IEC TS 18661-3}, unless explicitly stated otherwise.
  62. Support for @code{_Float@var{N}} or @code{_Float@var{N}x} types is
  63. provided for @code{_Float32}, @code{_Float64} and @code{_Float32x} on
  64. all platforms.
  65. It is also provided for @code{_Float128} and @code{_Float64x} on
  66. powerpc64le (PowerPC 64-bits little-endian), x86_64, x86, ia64,
  67. aarch64, alpha, mips64, riscv, s390 and sparc.
  68. @menu
  69. * Mathematical Constants:: Precise numeric values for often-used
  70. constants.
  71. * Trig Functions:: Sine, cosine, tangent, and friends.
  72. * Inverse Trig Functions:: Arcsine, arccosine, etc.
  73. * Exponents and Logarithms:: Also pow and sqrt.
  74. * Hyperbolic Functions:: sinh, cosh, tanh, etc.
  75. * Special Functions:: Bessel, gamma, erf.
  76. * Errors in Math Functions:: Known Maximum Errors in Math Functions.
  77. * Pseudo-Random Numbers:: Functions for generating pseudo-random
  78. numbers.
  79. * FP Function Optimizations:: Fast code or small code.
  80. @end menu
  81. @node Mathematical Constants
  82. @section Predefined Mathematical Constants
  83. @cindex constants
  84. @cindex mathematical constants
  85. The header @file{math.h} defines several useful mathematical constants.
  86. All values are defined as preprocessor macros starting with @code{M_}.
  87. The values provided are:
  88. @vtable @code
  89. @item M_E
  90. The base of natural logarithms.
  91. @item M_LOG2E
  92. The logarithm to base @code{2} of @code{M_E}.
  93. @item M_LOG10E
  94. The logarithm to base @code{10} of @code{M_E}.
  95. @item M_LN2
  96. The natural logarithm of @code{2}.
  97. @item M_LN10
  98. The natural logarithm of @code{10}.
  99. @item M_PI
  100. Pi, the ratio of a circle's circumference to its diameter.
  101. @item M_PI_2
  102. Pi divided by two.
  103. @item M_PI_4
  104. Pi divided by four.
  105. @item M_1_PI
  106. The reciprocal of pi (1/pi)
  107. @item M_2_PI
  108. Two times the reciprocal of pi.
  109. @item M_2_SQRTPI
  110. Two times the reciprocal of the square root of pi.
  111. @item M_SQRT2
  112. The square root of two.
  113. @item M_SQRT1_2
  114. The reciprocal of the square root of two (also the square root of 1/2).
  115. @end vtable
  116. These constants come from the Unix98 standard and were also available in
  117. 4.4BSD; therefore they are only defined if
  118. @code{_XOPEN_SOURCE=500}, or a more general feature select macro, is
  119. defined. The default set of features includes these constants.
  120. @xref{Feature Test Macros}.
  121. All values are of type @code{double}. As an extension, @theglibc{}
  122. also defines these constants with type @code{long double}. The
  123. @code{long double} macros have a lowercase @samp{l} appended to their
  124. names: @code{M_El}, @code{M_PIl}, and so forth. These are only
  125. available if @code{_GNU_SOURCE} is defined.
  126. Likewise, @theglibc{} also defines these constants with the types
  127. @code{_Float@var{N}} and @code{_Float@var{N}x} for the machines that
  128. have support for such types enabled (@pxref{Mathematics}) and if
  129. @code{_GNU_SOURCE} is defined. When available, the macros names are
  130. appended with @samp{f@var{N}} or @samp{f@var{N}x}, such as @samp{f128}
  131. for the type @code{_Float128}.
  132. @vindex PI
  133. @emph{Note:} Some programs use a constant named @code{PI} which has the
  134. same value as @code{M_PI}. This constant is not standard; it may have
  135. appeared in some old AT&T headers, and is mentioned in Stroustrup's book
  136. on C++. It infringes on the user's name space, so @theglibc{}
  137. does not define it. Fixing programs written to expect it is simple:
  138. replace @code{PI} with @code{M_PI} throughout, or put @samp{-DPI=M_PI}
  139. on the compiler command line.
  140. @node Trig Functions
  141. @section Trigonometric Functions
  142. @cindex trigonometric functions
  143. These are the familiar @code{sin}, @code{cos}, and @code{tan} functions.
  144. The arguments to all of these functions are in units of radians; recall
  145. that pi radians equals 180 degrees.
  146. @cindex pi (trigonometric constant)
  147. The math library normally defines @code{M_PI} to a @code{double}
  148. approximation of pi. If strict ISO and/or POSIX compliance
  149. are requested this constant is not defined, but you can easily define it
  150. yourself:
  151. @smallexample
  152. #define M_PI 3.14159265358979323846264338327
  153. @end smallexample
  154. @noindent
  155. You can also compute the value of pi with the expression @code{acos
  156. (-1.0)}.
  157. @deftypefun double sin (double @var{x})
  158. @deftypefunx float sinf (float @var{x})
  159. @deftypefunx {long double} sinl (long double @var{x})
  160. @deftypefunx _FloatN sinfN (_Float@var{N} @var{x})
  161. @deftypefunx _FloatNx sinfNx (_Float@var{N}x @var{x})
  162. @standards{ISO, math.h}
  163. @standardsx{sinfN, TS 18661-3:2015, math.h}
  164. @standardsx{sinfNx, TS 18661-3:2015, math.h}
  165. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  166. These functions return the sine of @var{x}, where @var{x} is given in
  167. radians. The return value is in the range @code{-1} to @code{1}.
  168. @end deftypefun
  169. @deftypefun double cos (double @var{x})
  170. @deftypefunx float cosf (float @var{x})
  171. @deftypefunx {long double} cosl (long double @var{x})
  172. @deftypefunx _FloatN cosfN (_Float@var{N} @var{x})
  173. @deftypefunx _FloatNx cosfNx (_Float@var{N}x @var{x})
  174. @standards{ISO, math.h}
  175. @standardsx{cosfN, TS 18661-3:2015, math.h}
  176. @standardsx{cosfNx, TS 18661-3:2015, math.h}
  177. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  178. These functions return the cosine of @var{x}, where @var{x} is given in
  179. radians. The return value is in the range @code{-1} to @code{1}.
  180. @end deftypefun
  181. @deftypefun double tan (double @var{x})
  182. @deftypefunx float tanf (float @var{x})
  183. @deftypefunx {long double} tanl (long double @var{x})
  184. @deftypefunx _FloatN tanfN (_Float@var{N} @var{x})
  185. @deftypefunx _FloatNx tanfNx (_Float@var{N}x @var{x})
  186. @standards{ISO, math.h}
  187. @standardsx{tanfN, TS 18661-3:2015, math.h}
  188. @standardsx{tanfNx, TS 18661-3:2015, math.h}
  189. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  190. These functions return the tangent of @var{x}, where @var{x} is given in
  191. radians.
  192. Mathematically, the tangent function has singularities at odd multiples
  193. of pi/2. If the argument @var{x} is too close to one of these
  194. singularities, @code{tan} will signal overflow.
  195. @end deftypefun
  196. In many applications where @code{sin} and @code{cos} are used, the sine
  197. and cosine of the same angle are needed at the same time. It is more
  198. efficient to compute them simultaneously, so the library provides a
  199. function to do that.
  200. @deftypefun void sincos (double @var{x}, double *@var{sinx}, double *@var{cosx})
  201. @deftypefunx void sincosf (float @var{x}, float *@var{sinx}, float *@var{cosx})
  202. @deftypefunx void sincosl (long double @var{x}, long double *@var{sinx}, long double *@var{cosx})
  203. @deftypefunx _FloatN sincosfN (_Float@var{N} @var{x}, _Float@var{N} *@var{sinx}, _Float@var{N} *@var{cosx})
  204. @deftypefunx _FloatNx sincosfNx (_Float@var{N}x @var{x}, _Float@var{N}x *@var{sinx}, _Float@var{N}x *@var{cosx})
  205. @standards{GNU, math.h}
  206. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  207. These functions return the sine of @var{x} in @code{*@var{sinx}} and the
  208. cosine of @var{x} in @code{*@var{cosx}}, where @var{x} is given in
  209. radians. Both values, @code{*@var{sinx}} and @code{*@var{cosx}}, are in
  210. the range of @code{-1} to @code{1}.
  211. All these functions, including the @code{_Float@var{N}} and
  212. @code{_Float@var{N}x} variants, are GNU extensions. Portable programs
  213. should be prepared to cope with their absence.
  214. @end deftypefun
  215. @cindex complex trigonometric functions
  216. @w{ISO C99} defines variants of the trig functions which work on
  217. complex numbers. @Theglibc{} provides these functions, but they
  218. are only useful if your compiler supports the new complex types defined
  219. by the standard.
  220. @c XXX Change this when gcc is fixed. -zw
  221. (As of this writing GCC supports complex numbers, but there are bugs in
  222. the implementation.)
  223. @deftypefun {complex double} csin (complex double @var{z})
  224. @deftypefunx {complex float} csinf (complex float @var{z})
  225. @deftypefunx {complex long double} csinl (complex long double @var{z})
  226. @deftypefunx {complex _FloatN} csinfN (complex _Float@var{N} @var{z})
  227. @deftypefunx {complex _FloatNx} csinfNx (complex _Float@var{N}x @var{z})
  228. @standards{ISO, complex.h}
  229. @standardsx{csinfN, TS 18661-3:2015, complex.h}
  230. @standardsx{csinfNx, TS 18661-3:2015, complex.h}
  231. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  232. @c There are calls to nan* that could trigger @mtslocale if they didn't get
  233. @c empty strings.
  234. These functions return the complex sine of @var{z}.
  235. The mathematical definition of the complex sine is
  236. @ifnottex
  237. @math{sin (z) = 1/(2*i) * (exp (z*i) - exp (-z*i))}.
  238. @end ifnottex
  239. @tex
  240. $$\sin(z) = {1\over 2i} (e^{zi} - e^{-zi})$$
  241. @end tex
  242. @end deftypefun
  243. @deftypefun {complex double} ccos (complex double @var{z})
  244. @deftypefunx {complex float} ccosf (complex float @var{z})
  245. @deftypefunx {complex long double} ccosl (complex long double @var{z})
  246. @deftypefunx {complex _FloatN} ccosfN (complex _Float@var{N} @var{z})
  247. @deftypefunx {complex _FloatNx} ccosfNx (complex _Float@var{N}x @var{z})
  248. @standards{ISO, complex.h}
  249. @standardsx{ccosfN, TS 18661-3:2015, complex.h}
  250. @standardsx{ccosfNx, TS 18661-3:2015, complex.h}
  251. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  252. These functions return the complex cosine of @var{z}.
  253. The mathematical definition of the complex cosine is
  254. @ifnottex
  255. @math{cos (z) = 1/2 * (exp (z*i) + exp (-z*i))}
  256. @end ifnottex
  257. @tex
  258. $$\cos(z) = {1\over 2} (e^{zi} + e^{-zi})$$
  259. @end tex
  260. @end deftypefun
  261. @deftypefun {complex double} ctan (complex double @var{z})
  262. @deftypefunx {complex float} ctanf (complex float @var{z})
  263. @deftypefunx {complex long double} ctanl (complex long double @var{z})
  264. @deftypefunx {complex _FloatN} ctanfN (complex _Float@var{N} @var{z})
  265. @deftypefunx {complex _FloatNx} ctanfNx (complex _Float@var{N}x @var{z})
  266. @standards{ISO, complex.h}
  267. @standardsx{ctanfN, TS 18661-3:2015, complex.h}
  268. @standardsx{ctanfNx, TS 18661-3:2015, complex.h}
  269. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  270. These functions return the complex tangent of @var{z}.
  271. The mathematical definition of the complex tangent is
  272. @ifnottex
  273. @math{tan (z) = -i * (exp (z*i) - exp (-z*i)) / (exp (z*i) + exp (-z*i))}
  274. @end ifnottex
  275. @tex
  276. $$\tan(z) = -i \cdot {e^{zi} - e^{-zi}\over e^{zi} + e^{-zi}}$$
  277. @end tex
  278. @noindent
  279. The complex tangent has poles at @math{pi/2 + 2n}, where @math{n} is an
  280. integer. @code{ctan} may signal overflow if @var{z} is too close to a
  281. pole.
  282. @end deftypefun
  283. @node Inverse Trig Functions
  284. @section Inverse Trigonometric Functions
  285. @cindex inverse trigonometric functions
  286. These are the usual arcsine, arccosine and arctangent functions,
  287. which are the inverses of the sine, cosine and tangent functions
  288. respectively.
  289. @deftypefun double asin (double @var{x})
  290. @deftypefunx float asinf (float @var{x})
  291. @deftypefunx {long double} asinl (long double @var{x})
  292. @deftypefunx _FloatN asinfN (_Float@var{N} @var{x})
  293. @deftypefunx _FloatNx asinfNx (_Float@var{N}x @var{x})
  294. @standards{ISO, math.h}
  295. @standardsx{asinfN, TS 18661-3:2015, math.h}
  296. @standardsx{asinfNx, TS 18661-3:2015, math.h}
  297. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  298. These functions compute the arcsine of @var{x}---that is, the value whose
  299. sine is @var{x}. The value is in units of radians. Mathematically,
  300. there are infinitely many such values; the one actually returned is the
  301. one between @code{-pi/2} and @code{pi/2} (inclusive).
  302. The arcsine function is defined mathematically only
  303. over the domain @code{-1} to @code{1}. If @var{x} is outside the
  304. domain, @code{asin} signals a domain error.
  305. @end deftypefun
  306. @deftypefun double acos (double @var{x})
  307. @deftypefunx float acosf (float @var{x})
  308. @deftypefunx {long double} acosl (long double @var{x})
  309. @deftypefunx _FloatN acosfN (_Float@var{N} @var{x})
  310. @deftypefunx _FloatNx acosfNx (_Float@var{N}x @var{x})
  311. @standards{ISO, math.h}
  312. @standardsx{acosfN, TS 18661-3:2015, math.h}
  313. @standardsx{acosfNx, TS 18661-3:2015, math.h}
  314. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  315. These functions compute the arccosine of @var{x}---that is, the value
  316. whose cosine is @var{x}. The value is in units of radians.
  317. Mathematically, there are infinitely many such values; the one actually
  318. returned is the one between @code{0} and @code{pi} (inclusive).
  319. The arccosine function is defined mathematically only
  320. over the domain @code{-1} to @code{1}. If @var{x} is outside the
  321. domain, @code{acos} signals a domain error.
  322. @end deftypefun
  323. @deftypefun double atan (double @var{x})
  324. @deftypefunx float atanf (float @var{x})
  325. @deftypefunx {long double} atanl (long double @var{x})
  326. @deftypefunx _FloatN atanfN (_Float@var{N} @var{x})
  327. @deftypefunx _FloatNx atanfNx (_Float@var{N}x @var{x})
  328. @standards{ISO, math.h}
  329. @standardsx{atanfN, TS 18661-3:2015, math.h}
  330. @standardsx{atanfNx, TS 18661-3:2015, math.h}
  331. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  332. These functions compute the arctangent of @var{x}---that is, the value
  333. whose tangent is @var{x}. The value is in units of radians.
  334. Mathematically, there are infinitely many such values; the one actually
  335. returned is the one between @code{-pi/2} and @code{pi/2} (inclusive).
  336. @end deftypefun
  337. @deftypefun double atan2 (double @var{y}, double @var{x})
  338. @deftypefunx float atan2f (float @var{y}, float @var{x})
  339. @deftypefunx {long double} atan2l (long double @var{y}, long double @var{x})
  340. @deftypefunx _FloatN atan2fN (_Float@var{N} @var{y}, _Float@var{N} @var{x})
  341. @deftypefunx _FloatNx atan2fNx (_Float@var{N}x @var{y}, _Float@var{N}x @var{x})
  342. @standards{ISO, math.h}
  343. @standardsx{atan2fN, TS 18661-3:2015, math.h}
  344. @standardsx{atan2fNx, TS 18661-3:2015, math.h}
  345. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  346. This function computes the arctangent of @var{y}/@var{x}, but the signs
  347. of both arguments are used to determine the quadrant of the result, and
  348. @var{x} is permitted to be zero. The return value is given in radians
  349. and is in the range @code{-pi} to @code{pi}, inclusive.
  350. If @var{x} and @var{y} are coordinates of a point in the plane,
  351. @code{atan2} returns the signed angle between the line from the origin
  352. to that point and the x-axis. Thus, @code{atan2} is useful for
  353. converting Cartesian coordinates to polar coordinates. (To compute the
  354. radial coordinate, use @code{hypot}; see @ref{Exponents and
  355. Logarithms}.)
  356. @c This is experimentally true. Should it be so? -zw
  357. If both @var{x} and @var{y} are zero, @code{atan2} returns zero.
  358. @end deftypefun
  359. @cindex inverse complex trigonometric functions
  360. @w{ISO C99} defines complex versions of the inverse trig functions.
  361. @deftypefun {complex double} casin (complex double @var{z})
  362. @deftypefunx {complex float} casinf (complex float @var{z})
  363. @deftypefunx {complex long double} casinl (complex long double @var{z})
  364. @deftypefunx {complex _FloatN} casinfN (complex _Float@var{N} @var{z})
  365. @deftypefunx {complex _FloatNx} casinfNx (complex _Float@var{N}x @var{z})
  366. @standards{ISO, complex.h}
  367. @standardsx{casinfN, TS 18661-3:2015, complex.h}
  368. @standardsx{casinfNx, TS 18661-3:2015, complex.h}
  369. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  370. These functions compute the complex arcsine of @var{z}---that is, the
  371. value whose sine is @var{z}. The value returned is in radians.
  372. Unlike the real-valued functions, @code{casin} is defined for all
  373. values of @var{z}.
  374. @end deftypefun
  375. @deftypefun {complex double} cacos (complex double @var{z})
  376. @deftypefunx {complex float} cacosf (complex float @var{z})
  377. @deftypefunx {complex long double} cacosl (complex long double @var{z})
  378. @deftypefunx {complex _FloatN} cacosfN (complex _Float@var{N} @var{z})
  379. @deftypefunx {complex _FloatNx} cacosfNx (complex _Float@var{N}x @var{z})
  380. @standards{ISO, complex.h}
  381. @standardsx{cacosfN, TS 18661-3:2015, complex.h}
  382. @standardsx{cacosfNx, TS 18661-3:2015, complex.h}
  383. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  384. These functions compute the complex arccosine of @var{z}---that is, the
  385. value whose cosine is @var{z}. The value returned is in radians.
  386. Unlike the real-valued functions, @code{cacos} is defined for all
  387. values of @var{z}.
  388. @end deftypefun
  389. @deftypefun {complex double} catan (complex double @var{z})
  390. @deftypefunx {complex float} catanf (complex float @var{z})
  391. @deftypefunx {complex long double} catanl (complex long double @var{z})
  392. @deftypefunx {complex _FloatN} catanfN (complex _Float@var{N} @var{z})
  393. @deftypefunx {complex _FloatNx} catanfNx (complex _Float@var{N}x @var{z})
  394. @standards{ISO, complex.h}
  395. @standardsx{catanfN, TS 18661-3:2015, complex.h}
  396. @standardsx{catanfNx, TS 18661-3:2015, complex.h}
  397. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  398. These functions compute the complex arctangent of @var{z}---that is,
  399. the value whose tangent is @var{z}. The value is in units of radians.
  400. @end deftypefun
  401. @node Exponents and Logarithms
  402. @section Exponentiation and Logarithms
  403. @cindex exponentiation functions
  404. @cindex power functions
  405. @cindex logarithm functions
  406. @deftypefun double exp (double @var{x})
  407. @deftypefunx float expf (float @var{x})
  408. @deftypefunx {long double} expl (long double @var{x})
  409. @deftypefunx _FloatN expfN (_Float@var{N} @var{x})
  410. @deftypefunx _FloatNx expfNx (_Float@var{N}x @var{x})
  411. @standards{ISO, math.h}
  412. @standardsx{expfN, TS 18661-3:2015, math.h}
  413. @standardsx{expfNx, TS 18661-3:2015, math.h}
  414. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  415. These functions compute @code{e} (the base of natural logarithms) raised
  416. to the power @var{x}.
  417. If the magnitude of the result is too large to be representable,
  418. @code{exp} signals overflow.
  419. @end deftypefun
  420. @deftypefun double exp2 (double @var{x})
  421. @deftypefunx float exp2f (float @var{x})
  422. @deftypefunx {long double} exp2l (long double @var{x})
  423. @deftypefunx _FloatN exp2fN (_Float@var{N} @var{x})
  424. @deftypefunx _FloatNx exp2fNx (_Float@var{N}x @var{x})
  425. @standards{ISO, math.h}
  426. @standardsx{exp2fN, TS 18661-3:2015, math.h}
  427. @standardsx{exp2fNx, TS 18661-3:2015, math.h}
  428. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  429. These functions compute @code{2} raised to the power @var{x}.
  430. Mathematically, @code{exp2 (x)} is the same as @code{exp (x * log (2))}.
  431. @end deftypefun
  432. @deftypefun double exp10 (double @var{x})
  433. @deftypefunx float exp10f (float @var{x})
  434. @deftypefunx {long double} exp10l (long double @var{x})
  435. @deftypefunx _FloatN exp10fN (_Float@var{N} @var{x})
  436. @deftypefunx _FloatNx exp10fNx (_Float@var{N}x @var{x})
  437. @standards{ISO, math.h}
  438. @standardsx{exp10fN, TS 18661-4:2015, math.h}
  439. @standardsx{exp10fNx, TS 18661-4:2015, math.h}
  440. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  441. These functions compute @code{10} raised to the power @var{x}.
  442. Mathematically, @code{exp10 (x)} is the same as @code{exp (x * log (10))}.
  443. The @code{exp10} functions are from TS 18661-4:2015.
  444. @end deftypefun
  445. @deftypefun double log (double @var{x})
  446. @deftypefunx float logf (float @var{x})
  447. @deftypefunx {long double} logl (long double @var{x})
  448. @deftypefunx _FloatN logfN (_Float@var{N} @var{x})
  449. @deftypefunx _FloatNx logfNx (_Float@var{N}x @var{x})
  450. @standards{ISO, math.h}
  451. @standardsx{logfN, TS 18661-3:2015, math.h}
  452. @standardsx{logfNx, TS 18661-3:2015, math.h}
  453. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  454. These functions compute the natural logarithm of @var{x}. @code{exp (log
  455. (@var{x}))} equals @var{x}, exactly in mathematics and approximately in
  456. C.
  457. If @var{x} is negative, @code{log} signals a domain error. If @var{x}
  458. is zero, it returns negative infinity; if @var{x} is too close to zero,
  459. it may signal overflow.
  460. @end deftypefun
  461. @deftypefun double log10 (double @var{x})
  462. @deftypefunx float log10f (float @var{x})
  463. @deftypefunx {long double} log10l (long double @var{x})
  464. @deftypefunx _FloatN log10fN (_Float@var{N} @var{x})
  465. @deftypefunx _FloatNx log10fNx (_Float@var{N}x @var{x})
  466. @standards{ISO, math.h}
  467. @standardsx{log10fN, TS 18661-3:2015, math.h}
  468. @standardsx{log10fNx, TS 18661-3:2015, math.h}
  469. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  470. These functions return the base-10 logarithm of @var{x}.
  471. @code{log10 (@var{x})} equals @code{log (@var{x}) / log (10)}.
  472. @end deftypefun
  473. @deftypefun double log2 (double @var{x})
  474. @deftypefunx float log2f (float @var{x})
  475. @deftypefunx {long double} log2l (long double @var{x})
  476. @deftypefunx _FloatN log2fN (_Float@var{N} @var{x})
  477. @deftypefunx _FloatNx log2fNx (_Float@var{N}x @var{x})
  478. @standards{ISO, math.h}
  479. @standardsx{log2fN, TS 18661-3:2015, math.h}
  480. @standardsx{log2fNx, TS 18661-3:2015, math.h}
  481. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  482. These functions return the base-2 logarithm of @var{x}.
  483. @code{log2 (@var{x})} equals @code{log (@var{x}) / log (2)}.
  484. @end deftypefun
  485. @deftypefun double logb (double @var{x})
  486. @deftypefunx float logbf (float @var{x})
  487. @deftypefunx {long double} logbl (long double @var{x})
  488. @deftypefunx _FloatN logbfN (_Float@var{N} @var{x})
  489. @deftypefunx _FloatNx logbfNx (_Float@var{N}x @var{x})
  490. @standards{ISO, math.h}
  491. @standardsx{logbfN, TS 18661-3:2015, math.h}
  492. @standardsx{logbfNx, TS 18661-3:2015, math.h}
  493. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  494. These functions extract the exponent of @var{x} and return it as a
  495. floating-point value. If @code{FLT_RADIX} is two, @code{logb} is equal
  496. to @code{floor (log2 (x))}, except it's probably faster.
  497. If @var{x} is de-normalized, @code{logb} returns the exponent @var{x}
  498. would have if it were normalized. If @var{x} is infinity (positive or
  499. negative), @code{logb} returns @math{@infinity{}}. If @var{x} is zero,
  500. @code{logb} returns @math{@infinity{}}. It does not signal.
  501. @end deftypefun
  502. @deftypefun int ilogb (double @var{x})
  503. @deftypefunx int ilogbf (float @var{x})
  504. @deftypefunx int ilogbl (long double @var{x})
  505. @deftypefunx int ilogbfN (_Float@var{N} @var{x})
  506. @deftypefunx int ilogbfNx (_Float@var{N}x @var{x})
  507. @deftypefunx {long int} llogb (double @var{x})
  508. @deftypefunx {long int} llogbf (float @var{x})
  509. @deftypefunx {long int} llogbl (long double @var{x})
  510. @deftypefunx {long int} llogbfN (_Float@var{N} @var{x})
  511. @deftypefunx {long int} llogbfNx (_Float@var{N}x @var{x})
  512. @standards{ISO, math.h}
  513. @standardsx{ilogbfN, TS 18661-3:2015, math.h}
  514. @standardsx{ilogbfNx, TS 18661-3:2015, math.h}
  515. @standardsx{llogbfN, TS 18661-3:2015, math.h}
  516. @standardsx{llogbfNx, TS 18661-3:2015, math.h}
  517. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  518. These functions are equivalent to the corresponding @code{logb}
  519. functions except that they return signed integer values. The
  520. @code{ilogb}, @code{ilogbf}, and @code{ilogbl} functions are from ISO
  521. C99; the @code{llogb}, @code{llogbf}, @code{llogbl} functions are from
  522. TS 18661-1:2014; the @code{ilogbfN}, @code{ilogbfNx}, @code{llogbfN},
  523. and @code{llogbfNx} functions are from TS 18661-3:2015.
  524. @end deftypefun
  525. @noindent
  526. Since integers cannot represent infinity and NaN, @code{ilogb} instead
  527. returns an integer that can't be the exponent of a normal floating-point
  528. number. @file{math.h} defines constants so you can check for this.
  529. @deftypevr Macro int FP_ILOGB0
  530. @standards{ISO, math.h}
  531. @code{ilogb} returns this value if its argument is @code{0}. The
  532. numeric value is either @code{INT_MIN} or @code{-INT_MAX}.
  533. This macro is defined in @w{ISO C99}.
  534. @end deftypevr
  535. @deftypevr Macro {long int} FP_LLOGB0
  536. @standards{ISO, math.h}
  537. @code{llogb} returns this value if its argument is @code{0}. The
  538. numeric value is either @code{LONG_MIN} or @code{-LONG_MAX}.
  539. This macro is defined in TS 18661-1:2014.
  540. @end deftypevr
  541. @deftypevr Macro int FP_ILOGBNAN
  542. @standards{ISO, math.h}
  543. @code{ilogb} returns this value if its argument is @code{NaN}. The
  544. numeric value is either @code{INT_MIN} or @code{INT_MAX}.
  545. This macro is defined in @w{ISO C99}.
  546. @end deftypevr
  547. @deftypevr Macro {long int} FP_LLOGBNAN
  548. @standards{ISO, math.h}
  549. @code{llogb} returns this value if its argument is @code{NaN}. The
  550. numeric value is either @code{LONG_MIN} or @code{LONG_MAX}.
  551. This macro is defined in TS 18661-1:2014.
  552. @end deftypevr
  553. These values are system specific. They might even be the same. The
  554. proper way to test the result of @code{ilogb} is as follows:
  555. @smallexample
  556. i = ilogb (f);
  557. if (i == FP_ILOGB0 || i == FP_ILOGBNAN)
  558. @{
  559. if (isnan (f))
  560. @{
  561. /* @r{Handle NaN.} */
  562. @}
  563. else if (f == 0.0)
  564. @{
  565. /* @r{Handle 0.0.} */
  566. @}
  567. else
  568. @{
  569. /* @r{Some other value with large exponent,}
  570. @r{perhaps +Inf.} */
  571. @}
  572. @}
  573. @end smallexample
  574. @deftypefun double pow (double @var{base}, double @var{power})
  575. @deftypefunx float powf (float @var{base}, float @var{power})
  576. @deftypefunx {long double} powl (long double @var{base}, long double @var{power})
  577. @deftypefunx _FloatN powfN (_Float@var{N} @var{base}, _Float@var{N} @var{power})
  578. @deftypefunx _FloatNx powfNx (_Float@var{N}x @var{base}, _Float@var{N}x @var{power})
  579. @standards{ISO, math.h}
  580. @standardsx{powfN, TS 18661-3:2015, math.h}
  581. @standardsx{powfNx, TS 18661-3:2015, math.h}
  582. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  583. These are general exponentiation functions, returning @var{base} raised
  584. to @var{power}.
  585. Mathematically, @code{pow} would return a complex number when @var{base}
  586. is negative and @var{power} is not an integral value. @code{pow} can't
  587. do that, so instead it signals a domain error. @code{pow} may also
  588. underflow or overflow the destination type.
  589. @end deftypefun
  590. @cindex square root function
  591. @deftypefun double sqrt (double @var{x})
  592. @deftypefunx float sqrtf (float @var{x})
  593. @deftypefunx {long double} sqrtl (long double @var{x})
  594. @deftypefunx _FloatN sqrtfN (_Float@var{N} @var{x})
  595. @deftypefunx _FloatNx sqrtfNx (_Float@var{N}x @var{x})
  596. @standards{ISO, math.h}
  597. @standardsx{sqrtfN, TS 18661-3:2015, math.h}
  598. @standardsx{sqrtfNx, TS 18661-3:2015, math.h}
  599. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  600. These functions return the nonnegative square root of @var{x}.
  601. If @var{x} is negative, @code{sqrt} signals a domain error.
  602. Mathematically, it should return a complex number.
  603. @end deftypefun
  604. @cindex cube root function
  605. @deftypefun double cbrt (double @var{x})
  606. @deftypefunx float cbrtf (float @var{x})
  607. @deftypefunx {long double} cbrtl (long double @var{x})
  608. @deftypefunx _FloatN cbrtfN (_Float@var{N} @var{x})
  609. @deftypefunx _FloatNx cbrtfNx (_Float@var{N}x @var{x})
  610. @standards{BSD, math.h}
  611. @standardsx{cbrtfN, TS 18661-3:2015, math.h}
  612. @standardsx{cbrtfNx, TS 18661-3:2015, math.h}
  613. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  614. These functions return the cube root of @var{x}. They cannot
  615. fail; every representable real value has a representable real cube root.
  616. @end deftypefun
  617. @deftypefun double hypot (double @var{x}, double @var{y})
  618. @deftypefunx float hypotf (float @var{x}, float @var{y})
  619. @deftypefunx {long double} hypotl (long double @var{x}, long double @var{y})
  620. @deftypefunx _FloatN hypotfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  621. @deftypefunx _FloatNx hypotfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  622. @standards{ISO, math.h}
  623. @standardsx{hypotfN, TS 18661-3:2015, math.h}
  624. @standardsx{hypotfNx, TS 18661-3:2015, math.h}
  625. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  626. These functions return @code{sqrt (@var{x}*@var{x} +
  627. @var{y}*@var{y})}. This is the length of the hypotenuse of a right
  628. triangle with sides of length @var{x} and @var{y}, or the distance
  629. of the point (@var{x}, @var{y}) from the origin. Using this function
  630. instead of the direct formula is wise, since the error is
  631. much smaller. See also the function @code{cabs} in @ref{Absolute Value}.
  632. @end deftypefun
  633. @deftypefun double expm1 (double @var{x})
  634. @deftypefunx float expm1f (float @var{x})
  635. @deftypefunx {long double} expm1l (long double @var{x})
  636. @deftypefunx _FloatN expm1fN (_Float@var{N} @var{x})
  637. @deftypefunx _FloatNx expm1fNx (_Float@var{N}x @var{x})
  638. @standards{ISO, math.h}
  639. @standardsx{expm1fN, TS 18661-3:2015, math.h}
  640. @standardsx{expm1fNx, TS 18661-3:2015, math.h}
  641. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  642. These functions return a value equivalent to @code{exp (@var{x}) - 1}.
  643. They are computed in a way that is accurate even if @var{x} is
  644. near zero---a case where @code{exp (@var{x}) - 1} would be inaccurate owing
  645. to subtraction of two numbers that are nearly equal.
  646. @end deftypefun
  647. @deftypefun double log1p (double @var{x})
  648. @deftypefunx float log1pf (float @var{x})
  649. @deftypefunx {long double} log1pl (long double @var{x})
  650. @deftypefunx _FloatN log1pfN (_Float@var{N} @var{x})
  651. @deftypefunx _FloatNx log1pfNx (_Float@var{N}x @var{x})
  652. @standards{ISO, math.h}
  653. @standardsx{log1pfN, TS 18661-3:2015, math.h}
  654. @standardsx{log1pfNx, TS 18661-3:2015, math.h}
  655. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  656. These functions return a value equivalent to @w{@code{log (1 + @var{x})}}.
  657. They are computed in a way that is accurate even if @var{x} is
  658. near zero.
  659. @end deftypefun
  660. @cindex complex exponentiation functions
  661. @cindex complex logarithm functions
  662. @w{ISO C99} defines complex variants of some of the exponentiation and
  663. logarithm functions.
  664. @deftypefun {complex double} cexp (complex double @var{z})
  665. @deftypefunx {complex float} cexpf (complex float @var{z})
  666. @deftypefunx {complex long double} cexpl (complex long double @var{z})
  667. @deftypefunx {complex _FloatN} cexpfN (complex _Float@var{N} @var{z})
  668. @deftypefunx {complex _FloatNx} cexpfNx (complex _Float@var{N}x @var{z})
  669. @standards{ISO, complex.h}
  670. @standardsx{cexpfN, TS 18661-3:2015, complex.h}
  671. @standardsx{cexpfNx, TS 18661-3:2015, complex.h}
  672. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  673. These functions return @code{e} (the base of natural
  674. logarithms) raised to the power of @var{z}.
  675. Mathematically, this corresponds to the value
  676. @ifnottex
  677. @math{exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))}
  678. @end ifnottex
  679. @tex
  680. $$\exp(z) = e^z = e^{{\rm Re}\,z} (\cos ({\rm Im}\,z) + i \sin ({\rm Im}\,z))$$
  681. @end tex
  682. @end deftypefun
  683. @deftypefun {complex double} clog (complex double @var{z})
  684. @deftypefunx {complex float} clogf (complex float @var{z})
  685. @deftypefunx {complex long double} clogl (complex long double @var{z})
  686. @deftypefunx {complex _FloatN} clogfN (complex _Float@var{N} @var{z})
  687. @deftypefunx {complex _FloatNx} clogfNx (complex _Float@var{N}x @var{z})
  688. @standards{ISO, complex.h}
  689. @standardsx{clogfN, TS 18661-3:2015, complex.h}
  690. @standardsx{clogfNx, TS 18661-3:2015, complex.h}
  691. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  692. These functions return the natural logarithm of @var{z}.
  693. Mathematically, this corresponds to the value
  694. @ifnottex
  695. @math{log (z) = log (cabs (z)) + I * carg (z)}
  696. @end ifnottex
  697. @tex
  698. $$\log(z) = \log |z| + i \arg z$$
  699. @end tex
  700. @noindent
  701. @code{clog} has a pole at 0, and will signal overflow if @var{z} equals
  702. or is very close to 0. It is well-defined for all other values of
  703. @var{z}.
  704. @end deftypefun
  705. @deftypefun {complex double} clog10 (complex double @var{z})
  706. @deftypefunx {complex float} clog10f (complex float @var{z})
  707. @deftypefunx {complex long double} clog10l (complex long double @var{z})
  708. @deftypefunx {complex _FloatN} clog10fN (complex _Float@var{N} @var{z})
  709. @deftypefunx {complex _FloatNx} clog10fNx (complex _Float@var{N}x @var{z})
  710. @standards{GNU, complex.h}
  711. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  712. These functions return the base 10 logarithm of the complex value
  713. @var{z}. Mathematically, this corresponds to the value
  714. @ifnottex
  715. @math{log10 (z) = log10 (cabs (z)) + I * carg (z) / log (10)}
  716. @end ifnottex
  717. @tex
  718. $$\log_{10}(z) = \log_{10}|z| + i \arg z / \log (10)$$
  719. @end tex
  720. All these functions, including the @code{_Float@var{N}} and
  721. @code{_Float@var{N}x} variants, are GNU extensions.
  722. @end deftypefun
  723. @deftypefun {complex double} csqrt (complex double @var{z})
  724. @deftypefunx {complex float} csqrtf (complex float @var{z})
  725. @deftypefunx {complex long double} csqrtl (complex long double @var{z})
  726. @deftypefunx {complex _FloatN} csqrtfN (_Float@var{N} @var{z})
  727. @deftypefunx {complex _FloatNx} csqrtfNx (complex _Float@var{N}x @var{z})
  728. @standards{ISO, complex.h}
  729. @standardsx{csqrtfN, TS 18661-3:2015, complex.h}
  730. @standardsx{csqrtfNx, TS 18661-3:2015, complex.h}
  731. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  732. These functions return the complex square root of the argument @var{z}. Unlike
  733. the real-valued functions, they are defined for all values of @var{z}.
  734. @end deftypefun
  735. @deftypefun {complex double} cpow (complex double @var{base}, complex double @var{power})
  736. @deftypefunx {complex float} cpowf (complex float @var{base}, complex float @var{power})
  737. @deftypefunx {complex long double} cpowl (complex long double @var{base}, complex long double @var{power})
  738. @deftypefunx {complex _FloatN} cpowfN (complex _Float@var{N} @var{base}, complex _Float@var{N} @var{power})
  739. @deftypefunx {complex _FloatNx} cpowfNx (complex _Float@var{N}x @var{base}, complex _Float@var{N}x @var{power})
  740. @standards{ISO, complex.h}
  741. @standardsx{cpowfN, TS 18661-3:2015, complex.h}
  742. @standardsx{cpowfNx, TS 18661-3:2015, complex.h}
  743. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  744. These functions return @var{base} raised to the power of
  745. @var{power}. This is equivalent to @w{@code{cexp (y * clog (x))}}
  746. @end deftypefun
  747. @node Hyperbolic Functions
  748. @section Hyperbolic Functions
  749. @cindex hyperbolic functions
  750. The functions in this section are related to the exponential functions;
  751. see @ref{Exponents and Logarithms}.
  752. @deftypefun double sinh (double @var{x})
  753. @deftypefunx float sinhf (float @var{x})
  754. @deftypefunx {long double} sinhl (long double @var{x})
  755. @deftypefunx _FloatN sinhfN (_Float@var{N} @var{x})
  756. @deftypefunx _FloatNx sinhfNx (_Float@var{N}x @var{x})
  757. @standards{ISO, math.h}
  758. @standardsx{sinhfN, TS 18661-3:2015, math.h}
  759. @standardsx{sinhfNx, TS 18661-3:2015, math.h}
  760. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  761. These functions return the hyperbolic sine of @var{x}, defined
  762. mathematically as @w{@code{(exp (@var{x}) - exp (-@var{x})) / 2}}. They
  763. may signal overflow if @var{x} is too large.
  764. @end deftypefun
  765. @deftypefun double cosh (double @var{x})
  766. @deftypefunx float coshf (float @var{x})
  767. @deftypefunx {long double} coshl (long double @var{x})
  768. @deftypefunx _FloatN coshfN (_Float@var{N} @var{x})
  769. @deftypefunx _FloatNx coshfNx (_Float@var{N}x @var{x})
  770. @standards{ISO, math.h}
  771. @standardsx{coshfN, TS 18661-3:2015, math.h}
  772. @standardsx{coshfNx, TS 18661-3:2015, math.h}
  773. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  774. These functions return the hyperbolic cosine of @var{x},
  775. defined mathematically as @w{@code{(exp (@var{x}) + exp (-@var{x})) / 2}}.
  776. They may signal overflow if @var{x} is too large.
  777. @end deftypefun
  778. @deftypefun double tanh (double @var{x})
  779. @deftypefunx float tanhf (float @var{x})
  780. @deftypefunx {long double} tanhl (long double @var{x})
  781. @deftypefunx _FloatN tanhfN (_Float@var{N} @var{x})
  782. @deftypefunx _FloatNx tanhfNx (_Float@var{N}x @var{x})
  783. @standards{ISO, math.h}
  784. @standardsx{tanhfN, TS 18661-3:2015, math.h}
  785. @standardsx{tanhfNx, TS 18661-3:2015, math.h}
  786. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  787. These functions return the hyperbolic tangent of @var{x},
  788. defined mathematically as @w{@code{sinh (@var{x}) / cosh (@var{x})}}.
  789. They may signal overflow if @var{x} is too large.
  790. @end deftypefun
  791. @cindex hyperbolic functions
  792. There are counterparts for the hyperbolic functions which take
  793. complex arguments.
  794. @deftypefun {complex double} csinh (complex double @var{z})
  795. @deftypefunx {complex float} csinhf (complex float @var{z})
  796. @deftypefunx {complex long double} csinhl (complex long double @var{z})
  797. @deftypefunx {complex _FloatN} csinhfN (complex _Float@var{N} @var{z})
  798. @deftypefunx {complex _FloatNx} csinhfNx (complex _Float@var{N}x @var{z})
  799. @standards{ISO, complex.h}
  800. @standardsx{csinhfN, TS 18661-3:2015, complex.h}
  801. @standardsx{csinhfNx, TS 18661-3:2015, complex.h}
  802. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  803. These functions return the complex hyperbolic sine of @var{z}, defined
  804. mathematically as @w{@code{(exp (@var{z}) - exp (-@var{z})) / 2}}.
  805. @end deftypefun
  806. @deftypefun {complex double} ccosh (complex double @var{z})
  807. @deftypefunx {complex float} ccoshf (complex float @var{z})
  808. @deftypefunx {complex long double} ccoshl (complex long double @var{z})
  809. @deftypefunx {complex _FloatN} ccoshfN (complex _Float@var{N} @var{z})
  810. @deftypefunx {complex _FloatNx} ccoshfNx (complex _Float@var{N}x @var{z})
  811. @standards{ISO, complex.h}
  812. @standardsx{ccoshfN, TS 18661-3:2015, complex.h}
  813. @standardsx{ccoshfNx, TS 18661-3:2015, complex.h}
  814. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  815. These functions return the complex hyperbolic cosine of @var{z}, defined
  816. mathematically as @w{@code{(exp (@var{z}) + exp (-@var{z})) / 2}}.
  817. @end deftypefun
  818. @deftypefun {complex double} ctanh (complex double @var{z})
  819. @deftypefunx {complex float} ctanhf (complex float @var{z})
  820. @deftypefunx {complex long double} ctanhl (complex long double @var{z})
  821. @deftypefunx {complex _FloatN} ctanhfN (complex _Float@var{N} @var{z})
  822. @deftypefunx {complex _FloatNx} ctanhfNx (complex _Float@var{N}x @var{z})
  823. @standards{ISO, complex.h}
  824. @standardsx{ctanhfN, TS 18661-3:2015, complex.h}
  825. @standardsx{ctanhfNx, TS 18661-3:2015, complex.h}
  826. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  827. These functions return the complex hyperbolic tangent of @var{z},
  828. defined mathematically as @w{@code{csinh (@var{z}) / ccosh (@var{z})}}.
  829. @end deftypefun
  830. @cindex inverse hyperbolic functions
  831. @deftypefun double asinh (double @var{x})
  832. @deftypefunx float asinhf (float @var{x})
  833. @deftypefunx {long double} asinhl (long double @var{x})
  834. @deftypefunx _FloatN asinhfN (_Float@var{N} @var{x})
  835. @deftypefunx _FloatNx asinhfNx (_Float@var{N}x @var{x})
  836. @standards{ISO, math.h}
  837. @standardsx{asinhfN, TS 18661-3:2015, math.h}
  838. @standardsx{asinhfNx, TS 18661-3:2015, math.h}
  839. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  840. These functions return the inverse hyperbolic sine of @var{x}---the
  841. value whose hyperbolic sine is @var{x}.
  842. @end deftypefun
  843. @deftypefun double acosh (double @var{x})
  844. @deftypefunx float acoshf (float @var{x})
  845. @deftypefunx {long double} acoshl (long double @var{x})
  846. @deftypefunx _FloatN acoshfN (_Float@var{N} @var{x})
  847. @deftypefunx _FloatNx acoshfNx (_Float@var{N}x @var{x})
  848. @standards{ISO, math.h}
  849. @standardsx{acoshfN, TS 18661-3:2015, math.h}
  850. @standardsx{acoshfNx, TS 18661-3:2015, math.h}
  851. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  852. These functions return the inverse hyperbolic cosine of @var{x}---the
  853. value whose hyperbolic cosine is @var{x}. If @var{x} is less than
  854. @code{1}, @code{acosh} signals a domain error.
  855. @end deftypefun
  856. @deftypefun double atanh (double @var{x})
  857. @deftypefunx float atanhf (float @var{x})
  858. @deftypefunx {long double} atanhl (long double @var{x})
  859. @deftypefunx _FloatN atanhfN (_Float@var{N} @var{x})
  860. @deftypefunx _FloatNx atanhfNx (_Float@var{N}x @var{x})
  861. @standards{ISO, math.h}
  862. @standardsx{atanhfN, TS 18661-3:2015, math.h}
  863. @standardsx{atanhfNx, TS 18661-3:2015, math.h}
  864. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  865. These functions return the inverse hyperbolic tangent of @var{x}---the
  866. value whose hyperbolic tangent is @var{x}. If the absolute value of
  867. @var{x} is greater than @code{1}, @code{atanh} signals a domain error;
  868. if it is equal to 1, @code{atanh} returns infinity.
  869. @end deftypefun
  870. @cindex inverse complex hyperbolic functions
  871. @deftypefun {complex double} casinh (complex double @var{z})
  872. @deftypefunx {complex float} casinhf (complex float @var{z})
  873. @deftypefunx {complex long double} casinhl (complex long double @var{z})
  874. @deftypefunx {complex _FloatN} casinhfN (complex _Float@var{N} @var{z})
  875. @deftypefunx {complex _FloatNx} casinhfNx (complex _Float@var{N}x @var{z})
  876. @standards{ISO, complex.h}
  877. @standardsx{casinhfN, TS 18661-3:2015, complex.h}
  878. @standardsx{casinhfNx, TS 18661-3:2015, complex.h}
  879. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  880. These functions return the inverse complex hyperbolic sine of
  881. @var{z}---the value whose complex hyperbolic sine is @var{z}.
  882. @end deftypefun
  883. @deftypefun {complex double} cacosh (complex double @var{z})
  884. @deftypefunx {complex float} cacoshf (complex float @var{z})
  885. @deftypefunx {complex long double} cacoshl (complex long double @var{z})
  886. @deftypefunx {complex _FloatN} cacoshfN (complex _Float@var{N} @var{z})
  887. @deftypefunx {complex _FloatNx} cacoshfNx (complex _Float@var{N}x @var{z})
  888. @standards{ISO, complex.h}
  889. @standardsx{cacoshfN, TS 18661-3:2015, complex.h}
  890. @standardsx{cacoshfNx, TS 18661-3:2015, complex.h}
  891. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  892. These functions return the inverse complex hyperbolic cosine of
  893. @var{z}---the value whose complex hyperbolic cosine is @var{z}. Unlike
  894. the real-valued functions, there are no restrictions on the value of @var{z}.
  895. @end deftypefun
  896. @deftypefun {complex double} catanh (complex double @var{z})
  897. @deftypefunx {complex float} catanhf (complex float @var{z})
  898. @deftypefunx {complex long double} catanhl (complex long double @var{z})
  899. @deftypefunx {complex _FloatN} catanhfN (complex _Float@var{N} @var{z})
  900. @deftypefunx {complex _FloatNx} catanhfNx (complex _Float@var{N}x @var{z})
  901. @standards{ISO, complex.h}
  902. @standardsx{catanhfN, TS 18661-3:2015, complex.h}
  903. @standardsx{catanhfNx, TS 18661-3:2015, complex.h}
  904. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  905. These functions return the inverse complex hyperbolic tangent of
  906. @var{z}---the value whose complex hyperbolic tangent is @var{z}. Unlike
  907. the real-valued functions, there are no restrictions on the value of
  908. @var{z}.
  909. @end deftypefun
  910. @node Special Functions
  911. @section Special Functions
  912. @cindex special functions
  913. @cindex Bessel functions
  914. @cindex gamma function
  915. These are some more exotic mathematical functions which are sometimes
  916. useful. Currently they only have real-valued versions.
  917. @deftypefun double erf (double @var{x})
  918. @deftypefunx float erff (float @var{x})
  919. @deftypefunx {long double} erfl (long double @var{x})
  920. @deftypefunx _FloatN erffN (_Float@var{N} @var{x})
  921. @deftypefunx _FloatNx erffNx (_Float@var{N}x @var{x})
  922. @standards{SVID, math.h}
  923. @standardsx{erffN, TS 18661-3:2015, math.h}
  924. @standardsx{erffNx, TS 18661-3:2015, math.h}
  925. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  926. @code{erf} returns the error function of @var{x}. The error
  927. function is defined as
  928. @tex
  929. $$\hbox{erf}(x) = {2\over\sqrt{\pi}}\cdot\int_0^x e^{-t^2} \hbox{d}t$$
  930. @end tex
  931. @ifnottex
  932. @smallexample
  933. erf (x) = 2/sqrt(pi) * integral from 0 to x of exp(-t^2) dt
  934. @end smallexample
  935. @end ifnottex
  936. @end deftypefun
  937. @deftypefun double erfc (double @var{x})
  938. @deftypefunx float erfcf (float @var{x})
  939. @deftypefunx {long double} erfcl (long double @var{x})
  940. @deftypefunx _FloatN erfcfN (_Float@var{N} @var{x})
  941. @deftypefunx _FloatNx erfcfNx (_Float@var{N}x @var{x})
  942. @standards{SVID, math.h}
  943. @standardsx{erfcfN, TS 18661-3:2015, math.h}
  944. @standardsx{erfcfNx, TS 18661-3:2015, math.h}
  945. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  946. @code{erfc} returns @code{1.0 - erf(@var{x})}, but computed in a
  947. fashion that avoids round-off error when @var{x} is large.
  948. @end deftypefun
  949. @deftypefun double lgamma (double @var{x})
  950. @deftypefunx float lgammaf (float @var{x})
  951. @deftypefunx {long double} lgammal (long double @var{x})
  952. @deftypefunx _FloatN lgammafN (_Float@var{N} @var{x})
  953. @deftypefunx _FloatNx lgammafNx (_Float@var{N}x @var{x})
  954. @standards{SVID, math.h}
  955. @standardsx{lgammafN, TS 18661-3:2015, math.h}
  956. @standardsx{lgammafNx, TS 18661-3:2015, math.h}
  957. @safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
  958. @code{lgamma} returns the natural logarithm of the absolute value of
  959. the gamma function of @var{x}. The gamma function is defined as
  960. @tex
  961. $$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
  962. @end tex
  963. @ifnottex
  964. @smallexample
  965. gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
  966. @end smallexample
  967. @end ifnottex
  968. @vindex signgam
  969. The sign of the gamma function is stored in the global variable
  970. @var{signgam}, which is declared in @file{math.h}. It is @code{1} if
  971. the intermediate result was positive or zero, or @code{-1} if it was
  972. negative.
  973. To compute the real gamma function you can use the @code{tgamma}
  974. function or you can compute the values as follows:
  975. @smallexample
  976. lgam = lgamma(x);
  977. gam = signgam*exp(lgam);
  978. @end smallexample
  979. The gamma function has singularities at the non-positive integers.
  980. @code{lgamma} will raise the zero divide exception if evaluated at a
  981. singularity.
  982. @end deftypefun
  983. @deftypefun double lgamma_r (double @var{x}, int *@var{signp})
  984. @deftypefunx float lgammaf_r (float @var{x}, int *@var{signp})
  985. @deftypefunx {long double} lgammal_r (long double @var{x}, int *@var{signp})
  986. @deftypefunx _FloatN lgammafN_r (_Float@var{N} @var{x}, int *@var{signp})
  987. @deftypefunx _FloatNx lgammafNx_r (_Float@var{N}x @var{x}, int *@var{signp})
  988. @standards{XPG, math.h}
  989. @standardsx{lgammafN_r, GNU, math.h}
  990. @standardsx{lgammafNx_r, GNU, math.h}
  991. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  992. @code{lgamma_r} is just like @code{lgamma}, but it stores the sign of
  993. the intermediate result in the variable pointed to by @var{signp}
  994. instead of in the @var{signgam} global. This means it is reentrant.
  995. The @code{lgammaf@var{N}_r} and @code{lgammaf@var{N}x_r} functions are
  996. GNU extensions.
  997. @end deftypefun
  998. @deftypefun double gamma (double @var{x})
  999. @deftypefunx float gammaf (float @var{x})
  1000. @deftypefunx {long double} gammal (long double @var{x})
  1001. @standards{SVID, math.h}
  1002. @safety{@prelim{}@mtunsafe{@mtasurace{:signgam}}@asunsafe{}@acsafe{}}
  1003. These functions exist for compatibility reasons. They are equivalent to
  1004. @code{lgamma} etc. It is better to use @code{lgamma} since for one the
  1005. name reflects better the actual computation, and moreover @code{lgamma} is
  1006. standardized in @w{ISO C99} while @code{gamma} is not.
  1007. @end deftypefun
  1008. @deftypefun double tgamma (double @var{x})
  1009. @deftypefunx float tgammaf (float @var{x})
  1010. @deftypefunx {long double} tgammal (long double @var{x})
  1011. @deftypefunx _FloatN tgammafN (_Float@var{N} @var{x})
  1012. @deftypefunx _FloatNx tgammafNx (_Float@var{N}x @var{x})
  1013. @standardsx{tgamma, XPG, math.h}
  1014. @standardsx{tgamma, ISO, math.h}
  1015. @standardsx{tgammaf, XPG, math.h}
  1016. @standardsx{tgammaf, ISO, math.h}
  1017. @standardsx{tgammal, XPG, math.h}
  1018. @standardsx{tgammal, ISO, math.h}
  1019. @standardsx{tgammafN, TS 18661-3:2015, math.h}
  1020. @standardsx{tgammafNx, TS 18661-3:2015, math.h}
  1021. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1022. @code{tgamma} applies the gamma function to @var{x}. The gamma
  1023. function is defined as
  1024. @tex
  1025. $$\Gamma(x) = \int_0^\infty t^{x-1} e^{-t} \hbox{d}t$$
  1026. @end tex
  1027. @ifnottex
  1028. @smallexample
  1029. gamma (x) = integral from 0 to @infinity{} of t^(x-1) e^-t dt
  1030. @end smallexample
  1031. @end ifnottex
  1032. This function was introduced in @w{ISO C99}. The @code{_Float@var{N}}
  1033. and @code{_Float@var{N}x} variants were introduced in @w{ISO/IEC TS
  1034. 18661-3}.
  1035. @end deftypefun
  1036. @deftypefun double j0 (double @var{x})
  1037. @deftypefunx float j0f (float @var{x})
  1038. @deftypefunx {long double} j0l (long double @var{x})
  1039. @deftypefunx _FloatN j0fN (_Float@var{N} @var{x})
  1040. @deftypefunx _FloatNx j0fNx (_Float@var{N}x @var{x})
  1041. @standards{SVID, math.h}
  1042. @standardsx{j0fN, GNU, math.h}
  1043. @standardsx{j0fNx, GNU, math.h}
  1044. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1045. @code{j0} returns the Bessel function of the first kind of order 0 of
  1046. @var{x}. It may signal underflow if @var{x} is too large.
  1047. The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
  1048. extensions.
  1049. @end deftypefun
  1050. @deftypefun double j1 (double @var{x})
  1051. @deftypefunx float j1f (float @var{x})
  1052. @deftypefunx {long double} j1l (long double @var{x})
  1053. @deftypefunx _FloatN j1fN (_Float@var{N} @var{x})
  1054. @deftypefunx _FloatNx j1fNx (_Float@var{N}x @var{x})
  1055. @standards{SVID, math.h}
  1056. @standardsx{j1fN, GNU, math.h}
  1057. @standardsx{j1fNx, GNU, math.h}
  1058. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1059. @code{j1} returns the Bessel function of the first kind of order 1 of
  1060. @var{x}. It may signal underflow if @var{x} is too large.
  1061. The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
  1062. extensions.
  1063. @end deftypefun
  1064. @deftypefun double jn (int @var{n}, double @var{x})
  1065. @deftypefunx float jnf (int @var{n}, float @var{x})
  1066. @deftypefunx {long double} jnl (int @var{n}, long double @var{x})
  1067. @deftypefunx _FloatN jnfN (int @var{n}, _Float@var{N} @var{x})
  1068. @deftypefunx _FloatNx jnfNx (int @var{n}, _Float@var{N}x @var{x})
  1069. @standards{SVID, math.h}
  1070. @standardsx{jnfN, GNU, math.h}
  1071. @standardsx{jnfNx, GNU, math.h}
  1072. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1073. @code{jn} returns the Bessel function of the first kind of order
  1074. @var{n} of @var{x}. It may signal underflow if @var{x} is too large.
  1075. The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
  1076. extensions.
  1077. @end deftypefun
  1078. @deftypefun double y0 (double @var{x})
  1079. @deftypefunx float y0f (float @var{x})
  1080. @deftypefunx {long double} y0l (long double @var{x})
  1081. @deftypefunx _FloatN y0fN (_Float@var{N} @var{x})
  1082. @deftypefunx _FloatNx y0fNx (_Float@var{N}x @var{x})
  1083. @standards{SVID, math.h}
  1084. @standardsx{y0fN, GNU, math.h}
  1085. @standardsx{y0fNx, GNU, math.h}
  1086. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1087. @code{y0} returns the Bessel function of the second kind of order 0 of
  1088. @var{x}. It may signal underflow if @var{x} is too large. If @var{x}
  1089. is negative, @code{y0} signals a domain error; if it is zero,
  1090. @code{y0} signals overflow and returns @math{-@infinity}.
  1091. The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
  1092. extensions.
  1093. @end deftypefun
  1094. @deftypefun double y1 (double @var{x})
  1095. @deftypefunx float y1f (float @var{x})
  1096. @deftypefunx {long double} y1l (long double @var{x})
  1097. @deftypefunx _FloatN y1fN (_Float@var{N} @var{x})
  1098. @deftypefunx _FloatNx y1fNx (_Float@var{N}x @var{x})
  1099. @standards{SVID, math.h}
  1100. @standardsx{y1fN, GNU, math.h}
  1101. @standardsx{y1fNx, GNU, math.h}
  1102. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1103. @code{y1} returns the Bessel function of the second kind of order 1 of
  1104. @var{x}. It may signal underflow if @var{x} is too large. If @var{x}
  1105. is negative, @code{y1} signals a domain error; if it is zero,
  1106. @code{y1} signals overflow and returns @math{-@infinity}.
  1107. The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
  1108. extensions.
  1109. @end deftypefun
  1110. @deftypefun double yn (int @var{n}, double @var{x})
  1111. @deftypefunx float ynf (int @var{n}, float @var{x})
  1112. @deftypefunx {long double} ynl (int @var{n}, long double @var{x})
  1113. @deftypefunx _FloatN ynfN (int @var{n}, _Float@var{N} @var{x})
  1114. @deftypefunx _FloatNx ynfNx (int @var{n}, _Float@var{N}x @var{x})
  1115. @standards{SVID, math.h}
  1116. @standardsx{ynfN, GNU, math.h}
  1117. @standardsx{ynfNx, GNU, math.h}
  1118. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1119. @code{yn} returns the Bessel function of the second kind of order @var{n} of
  1120. @var{x}. It may signal underflow if @var{x} is too large. If @var{x}
  1121. is negative, @code{yn} signals a domain error; if it is zero,
  1122. @code{yn} signals overflow and returns @math{-@infinity}.
  1123. The @code{_Float@var{N}} and @code{_Float@var{N}x} variants are GNU
  1124. extensions.
  1125. @end deftypefun
  1126. @node Errors in Math Functions
  1127. @section Known Maximum Errors in Math Functions
  1128. @cindex math errors
  1129. @cindex ulps
  1130. This section lists the known errors of the functions in the math
  1131. library. Errors are measured in ``units of the last place''. This is a
  1132. measure for the relative error. For a number @math{z} with the
  1133. representation @math{d.d@dots{}d@mul{}2^e} (we assume IEEE
  1134. floating-point numbers with base 2) the ULP is represented by
  1135. @tex
  1136. $${|d.d\dots d - (z/2^e)|}\over {2^{p-1}}$$
  1137. @end tex
  1138. @ifnottex
  1139. @smallexample
  1140. |d.d...d - (z / 2^e)| / 2^(p - 1)
  1141. @end smallexample
  1142. @end ifnottex
  1143. @noindent
  1144. where @math{p} is the number of bits in the mantissa of the
  1145. floating-point number representation. Ideally the error for all
  1146. functions is always less than 0.5ulps in round-to-nearest mode. Using
  1147. rounding bits this is also
  1148. possible and normally implemented for the basic operations. Except
  1149. for certain functions such as @code{sqrt}, @code{fma} and @code{rint}
  1150. whose results are fully specified by reference to corresponding IEEE
  1151. 754 floating-point operations, and conversions between strings and
  1152. floating point, @theglibc{} does not aim for correctly rounded results
  1153. for functions in the math library, and does not aim for correctness in
  1154. whether ``inexact'' exceptions are raised. Instead, the goals for
  1155. accuracy of functions without fully specified results are as follows;
  1156. some functions have bugs meaning they do not meet these goals in all
  1157. cases. In the future, @theglibc{} may provide some other correctly
  1158. rounding functions under the names such as @code{crsin} proposed for
  1159. an extension to ISO C.
  1160. @itemize @bullet
  1161. @item
  1162. Each function with a floating-point result behaves as if it computes
  1163. an infinite-precision result that is within a few ulp (in both real
  1164. and complex parts, for functions with complex results) of the
  1165. mathematically correct value of the function (interpreted together
  1166. with ISO C or POSIX semantics for the function in question) at the
  1167. exact value passed as the input. Exceptions are raised appropriately
  1168. for this value and in accordance with IEEE 754 / ISO C / POSIX
  1169. semantics, and it is then rounded according to the current rounding
  1170. direction to the result that is returned to the user. @code{errno}
  1171. may also be set (@pxref{Math Error Reporting}). (The ``inexact''
  1172. exception may be raised, or not raised, even if this is inconsistent
  1173. with the infinite-precision value.)
  1174. @item
  1175. For the IBM @code{long double} format, as used on PowerPC GNU/Linux,
  1176. the accuracy goal is weaker for input values not exactly representable
  1177. in 106 bits of precision; it is as if the input value is some value
  1178. within 0.5ulp of the value actually passed, where ``ulp'' is
  1179. interpreted in terms of a fixed-precision 106-bit mantissa, but not
  1180. necessarily the exact value actually passed with discontiguous
  1181. mantissa bits.
  1182. @item
  1183. For the IBM @code{long double} format, functions whose results are
  1184. fully specified by reference to corresponding IEEE 754 floating-point
  1185. operations have the same accuracy goals as other functions, but with
  1186. the error bound being the same as that for division (3ulp).
  1187. Furthermore, ``inexact'' and ``underflow'' exceptions may be raised
  1188. for all functions for any inputs, even where such exceptions are
  1189. inconsistent with the returned value, since the underlying
  1190. floating-point arithmetic has that property.
  1191. @item
  1192. Functions behave as if the infinite-precision result computed is zero,
  1193. infinity or NaN if and only if that is the mathematically correct
  1194. infinite-precision result. They behave as if the infinite-precision
  1195. result computed always has the same sign as the mathematically correct
  1196. result.
  1197. @item
  1198. If the mathematical result is more than a few ulp above the overflow
  1199. threshold for the current rounding direction, the value returned is
  1200. the appropriate overflow value for the current rounding direction,
  1201. with the overflow exception raised.
  1202. @item
  1203. If the mathematical result has magnitude well below half the least
  1204. subnormal magnitude, the returned value is either zero or the least
  1205. subnormal (in each case, with the correct sign), according to the
  1206. current rounding direction and with the underflow exception raised.
  1207. @item
  1208. Where the mathematical result underflows (before rounding) and is not
  1209. exactly representable as a floating-point value, the function does not
  1210. behave as if the computed infinite-precision result is an exact value
  1211. in the subnormal range. This means that the underflow exception is
  1212. raised other than possibly for cases where the mathematical result is
  1213. very close to the underflow threshold and the function behaves as if
  1214. it computes an infinite-precision result that does not underflow. (So
  1215. there may be spurious underflow exceptions in cases where the
  1216. underflowing result is exact, but not missing underflow exceptions in
  1217. cases where it is inexact.)
  1218. @item
  1219. @Theglibc{} does not aim for functions to satisfy other properties of
  1220. the underlying mathematical function, such as monotonicity, where not
  1221. implied by the above goals.
  1222. @item
  1223. All the above applies to both real and complex parts, for complex
  1224. functions.
  1225. @end itemize
  1226. Therefore many of the functions in the math library have errors. The
  1227. table lists the maximum error for each function which is exposed by one
  1228. of the existing tests in the test suite. The table tries to cover as much
  1229. as possible and list the actual maximum error (or at least a ballpark
  1230. figure) but this is often not achieved due to the large search space.
  1231. The table lists the ULP values for different architectures. Different
  1232. architectures have different results since their hardware support for
  1233. floating-point operations varies and also the existing hardware support
  1234. is different. Only the round-to-nearest rounding mode is covered by
  1235. this table, and vector versions of functions are not covered.
  1236. Functions not listed do not have known errors.
  1237. @page
  1238. @c This multitable does not fit on a single page
  1239. @include libm-err.texi
  1240. @node Pseudo-Random Numbers
  1241. @section Pseudo-Random Numbers
  1242. @cindex random numbers
  1243. @cindex pseudo-random numbers
  1244. @cindex seed (for random numbers)
  1245. This section describes the GNU facilities for generating a series of
  1246. pseudo-random numbers. The numbers generated are not truly random;
  1247. typically, they form a sequence that repeats periodically, with a period
  1248. so large that you can ignore it for ordinary purposes. The random
  1249. number generator works by remembering a @dfn{seed} value which it uses
  1250. to compute the next random number and also to compute a new seed.
  1251. Although the generated numbers look unpredictable within one run of a
  1252. program, the sequence of numbers is @emph{exactly the same} from one run
  1253. to the next. This is because the initial seed is always the same. This
  1254. is convenient when you are debugging a program, but it is unhelpful if
  1255. you want the program to behave unpredictably. If you want a different
  1256. pseudo-random series each time your program runs, you must specify a
  1257. different seed each time. For ordinary purposes, basing the seed on the
  1258. current time works well. For random numbers in cryptography,
  1259. @pxref{Unpredictable Bytes}.
  1260. You can obtain repeatable sequences of numbers on a particular machine type
  1261. by specifying the same initial seed value for the random number
  1262. generator. There is no standard meaning for a particular seed value;
  1263. the same seed, used in different C libraries or on different CPU types,
  1264. will give you different random numbers.
  1265. @Theglibc{} supports the standard @w{ISO C} random number functions
  1266. plus two other sets derived from BSD and SVID. The BSD and @w{ISO C}
  1267. functions provide identical, somewhat limited functionality. If only a
  1268. small number of random bits are required, we recommend you use the
  1269. @w{ISO C} interface, @code{rand} and @code{srand}. The SVID functions
  1270. provide a more flexible interface, which allows better random number
  1271. generator algorithms, provides more random bits (up to 48) per call, and
  1272. can provide random floating-point numbers. These functions are required
  1273. by the XPG standard and therefore will be present in all modern Unix
  1274. systems.
  1275. @menu
  1276. * ISO Random:: @code{rand} and friends.
  1277. * BSD Random:: @code{random} and friends.
  1278. * SVID Random:: @code{drand48} and friends.
  1279. @end menu
  1280. @node ISO Random
  1281. @subsection ISO C Random Number Functions
  1282. This section describes the random number functions that are part of
  1283. the @w{ISO C} standard.
  1284. To use these facilities, you should include the header file
  1285. @file{stdlib.h} in your program.
  1286. @pindex stdlib.h
  1287. @deftypevr Macro int RAND_MAX
  1288. @standards{ISO, stdlib.h}
  1289. The value of this macro is an integer constant representing the largest
  1290. value the @code{rand} function can return. In @theglibc{}, it is
  1291. @code{2147483647}, which is the largest signed integer representable in
  1292. 32 bits. In other libraries, it may be as low as @code{32767}.
  1293. @end deftypevr
  1294. @deftypefun int rand (void)
  1295. @standards{ISO, stdlib.h}
  1296. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1297. @c Just calls random.
  1298. The @code{rand} function returns the next pseudo-random number in the
  1299. series. The value ranges from @code{0} to @code{RAND_MAX}.
  1300. @end deftypefun
  1301. @deftypefun void srand (unsigned int @var{seed})
  1302. @standards{ISO, stdlib.h}
  1303. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1304. @c Alias to srandom.
  1305. This function establishes @var{seed} as the seed for a new series of
  1306. pseudo-random numbers. If you call @code{rand} before a seed has been
  1307. established with @code{srand}, it uses the value @code{1} as a default
  1308. seed.
  1309. To produce a different pseudo-random series each time your program is
  1310. run, do @code{srand (time (0))}.
  1311. @end deftypefun
  1312. POSIX.1 extended the C standard functions to support reproducible random
  1313. numbers in multi-threaded programs. However, the extension is badly
  1314. designed and unsuitable for serious work.
  1315. @deftypefun int rand_r (unsigned int *@var{seed})
  1316. @standards{POSIX.1, stdlib.h}
  1317. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1318. This function returns a random number in the range 0 to @code{RAND_MAX}
  1319. just as @code{rand} does. However, all its state is stored in the
  1320. @var{seed} argument. This means the RNG's state can only have as many
  1321. bits as the type @code{unsigned int} has. This is far too few to
  1322. provide a good RNG.
  1323. If your program requires a reentrant RNG, we recommend you use the
  1324. reentrant GNU extensions to the SVID random number generator. The
  1325. POSIX.1 interface should only be used when the GNU extensions are not
  1326. available.
  1327. @end deftypefun
  1328. @node BSD Random
  1329. @subsection BSD Random Number Functions
  1330. This section describes a set of random number generation functions that
  1331. are derived from BSD. There is no advantage to using these functions
  1332. with @theglibc{}; we support them for BSD compatibility only.
  1333. The prototypes for these functions are in @file{stdlib.h}.
  1334. @pindex stdlib.h
  1335. @deftypefun {long int} random (void)
  1336. @standards{BSD, stdlib.h}
  1337. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1338. @c Takes a lock and calls random_r with an automatic variable and the
  1339. @c global state, while holding a lock.
  1340. This function returns the next pseudo-random number in the sequence.
  1341. The value returned ranges from @code{0} to @code{2147483647}.
  1342. @strong{NB:} Temporarily this function was defined to return a
  1343. @code{int32_t} value to indicate that the return value always contains
  1344. 32 bits even if @code{long int} is wider. The standard demands it
  1345. differently. Users must always be aware of the 32-bit limitation,
  1346. though.
  1347. @end deftypefun
  1348. @deftypefun void srandom (unsigned int @var{seed})
  1349. @standards{BSD, stdlib.h}
  1350. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1351. @c Takes a lock and calls srandom_r with an automatic variable and a
  1352. @c static buffer. There's no MT-safety issue because the static buffer
  1353. @c is internally protected by a lock, although other threads may modify
  1354. @c the set state before it is used.
  1355. The @code{srandom} function sets the state of the random number
  1356. generator based on the integer @var{seed}. If you supply a @var{seed} value
  1357. of @code{1}, this will cause @code{random} to reproduce the default set
  1358. of random numbers.
  1359. To produce a different set of pseudo-random numbers each time your
  1360. program runs, do @code{srandom (time (0))}.
  1361. @end deftypefun
  1362. @deftypefun {char *} initstate (unsigned int @var{seed}, char *@var{state}, size_t @var{size})
  1363. @standards{BSD, stdlib.h}
  1364. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1365. The @code{initstate} function is used to initialize the random number
  1366. generator state. The argument @var{state} is an array of @var{size}
  1367. bytes, used to hold the state information. It is initialized based on
  1368. @var{seed}. The size must be between 8 and 256 bytes, and should be a
  1369. power of two. The bigger the @var{state} array, the better.
  1370. The return value is the previous value of the state information array.
  1371. You can use this value later as an argument to @code{setstate} to
  1372. restore that state.
  1373. @end deftypefun
  1374. @deftypefun {char *} setstate (char *@var{state})
  1375. @standards{BSD, stdlib.h}
  1376. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1377. The @code{setstate} function restores the random number state
  1378. information @var{state}. The argument must have been the result of
  1379. a previous call to @var{initstate} or @var{setstate}.
  1380. The return value is the previous value of the state information array.
  1381. You can use this value later as an argument to @code{setstate} to
  1382. restore that state.
  1383. If the function fails the return value is @code{NULL}.
  1384. @end deftypefun
  1385. The four functions described so far in this section all work on a state
  1386. which is shared by all threads. The state is not directly accessible to
  1387. the user and can only be modified by these functions. This makes it
  1388. hard to deal with situations where each thread should have its own
  1389. pseudo-random number generator.
  1390. @Theglibc{} contains four additional functions which contain the
  1391. state as an explicit parameter and therefore make it possible to handle
  1392. thread-local PRNGs. Besides this there is no difference. In fact, the
  1393. four functions already discussed are implemented internally using the
  1394. following interfaces.
  1395. The @file{stdlib.h} header contains a definition of the following type:
  1396. @deftp {Data Type} {struct random_data}
  1397. @standards{GNU, stdlib.h}
  1398. Objects of type @code{struct random_data} contain the information
  1399. necessary to represent the state of the PRNG. Although a complete
  1400. definition of the type is present the type should be treated as opaque.
  1401. @end deftp
  1402. The functions modifying the state follow exactly the already described
  1403. functions.
  1404. @deftypefun int random_r (struct random_data *restrict @var{buf}, int32_t *restrict @var{result})
  1405. @standards{GNU, stdlib.h}
  1406. @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
  1407. The @code{random_r} function behaves exactly like the @code{random}
  1408. function except that it uses and modifies the state in the object
  1409. pointed to by the first parameter instead of the global state.
  1410. @end deftypefun
  1411. @deftypefun int srandom_r (unsigned int @var{seed}, struct random_data *@var{buf})
  1412. @standards{GNU, stdlib.h}
  1413. @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
  1414. The @code{srandom_r} function behaves exactly like the @code{srandom}
  1415. function except that it uses and modifies the state in the object
  1416. pointed to by the second parameter instead of the global state.
  1417. @end deftypefun
  1418. @deftypefun int initstate_r (unsigned int @var{seed}, char *restrict @var{statebuf}, size_t @var{statelen}, struct random_data *restrict @var{buf})
  1419. @standards{GNU, stdlib.h}
  1420. @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
  1421. The @code{initstate_r} function behaves exactly like the @code{initstate}
  1422. function except that it uses and modifies the state in the object
  1423. pointed to by the fourth parameter instead of the global state.
  1424. @end deftypefun
  1425. @deftypefun int setstate_r (char *restrict @var{statebuf}, struct random_data *restrict @var{buf})
  1426. @standards{GNU, stdlib.h}
  1427. @safety{@prelim{}@mtsafe{@mtsrace{:buf}}@assafe{}@acunsafe{@acucorrupt{}}}
  1428. The @code{setstate_r} function behaves exactly like the @code{setstate}
  1429. function except that it uses and modifies the state in the object
  1430. pointed to by the first parameter instead of the global state.
  1431. @end deftypefun
  1432. @node SVID Random
  1433. @subsection SVID Random Number Function
  1434. The C library on SVID systems contains yet another kind of random number
  1435. generator functions. They use a state of 48 bits of data. The user can
  1436. choose among a collection of functions which return the random bits
  1437. in different forms.
  1438. Generally there are two kinds of function. The first uses a state of
  1439. the random number generator which is shared among several functions and
  1440. by all threads of the process. The second requires the user to handle
  1441. the state.
  1442. All functions have in common that they use the same congruential
  1443. formula with the same constants. The formula is
  1444. @smallexample
  1445. Y = (a * X + c) mod m
  1446. @end smallexample
  1447. @noindent
  1448. where @var{X} is the state of the generator at the beginning and
  1449. @var{Y} the state at the end. @code{a} and @code{c} are constants
  1450. determining the way the generator works. By default they are
  1451. @smallexample
  1452. a = 0x5DEECE66D = 25214903917
  1453. c = 0xb = 11
  1454. @end smallexample
  1455. @noindent
  1456. but they can also be changed by the user. @code{m} is of course 2^48
  1457. since the state consists of a 48-bit array.
  1458. The prototypes for these functions are in @file{stdlib.h}.
  1459. @pindex stdlib.h
  1460. @deftypefun double drand48 (void)
  1461. @standards{SVID, stdlib.h}
  1462. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1463. @c Uses of the static state buffer are not guarded by a lock (thus
  1464. @c @mtasurace:drand48), so they may be found or left at a
  1465. @c partially-updated state in case of calls from within signal handlers
  1466. @c or cancellation. None of this will break safety rules or invoke
  1467. @c undefined behavior, but it may affect randomness.
  1468. This function returns a @code{double} value in the range of @code{0.0}
  1469. to @code{1.0} (exclusive). The random bits are determined by the global
  1470. state of the random number generator in the C library.
  1471. Since the @code{double} type according to @w{IEEE 754} has a 52-bit
  1472. mantissa this means 4 bits are not initialized by the random number
  1473. generator. These are (of course) chosen to be the least significant
  1474. bits and they are initialized to @code{0}.
  1475. @end deftypefun
  1476. @deftypefun double erand48 (unsigned short int @var{xsubi}[3])
  1477. @standards{SVID, stdlib.h}
  1478. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1479. @c The static buffer is just initialized with default parameters, which
  1480. @c are later read to advance the state held in xsubi.
  1481. This function returns a @code{double} value in the range of @code{0.0}
  1482. to @code{1.0} (exclusive), similarly to @code{drand48}. The argument is
  1483. an array describing the state of the random number generator.
  1484. This function can be called subsequently since it updates the array to
  1485. guarantee random numbers. The array should have been initialized before
  1486. initial use to obtain reproducible results.
  1487. @end deftypefun
  1488. @deftypefun {long int} lrand48 (void)
  1489. @standards{SVID, stdlib.h}
  1490. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1491. The @code{lrand48} function returns an integer value in the range of
  1492. @code{0} to @code{2^31} (exclusive). Even if the size of the @code{long
  1493. int} type can take more than 32 bits, no higher numbers are returned.
  1494. The random bits are determined by the global state of the random number
  1495. generator in the C library.
  1496. @end deftypefun
  1497. @deftypefun {long int} nrand48 (unsigned short int @var{xsubi}[3])
  1498. @standards{SVID, stdlib.h}
  1499. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1500. This function is similar to the @code{lrand48} function in that it
  1501. returns a number in the range of @code{0} to @code{2^31} (exclusive) but
  1502. the state of the random number generator used to produce the random bits
  1503. is determined by the array provided as the parameter to the function.
  1504. The numbers in the array are updated afterwards so that subsequent calls
  1505. to this function yield different results (as is expected of a random
  1506. number generator). The array should have been initialized before the
  1507. first call to obtain reproducible results.
  1508. @end deftypefun
  1509. @deftypefun {long int} mrand48 (void)
  1510. @standards{SVID, stdlib.h}
  1511. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1512. The @code{mrand48} function is similar to @code{lrand48}. The only
  1513. difference is that the numbers returned are in the range @code{-2^31} to
  1514. @code{2^31} (exclusive).
  1515. @end deftypefun
  1516. @deftypefun {long int} jrand48 (unsigned short int @var{xsubi}[3])
  1517. @standards{SVID, stdlib.h}
  1518. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1519. The @code{jrand48} function is similar to @code{nrand48}. The only
  1520. difference is that the numbers returned are in the range @code{-2^31} to
  1521. @code{2^31} (exclusive). For the @code{xsubi} parameter the same
  1522. requirements are necessary.
  1523. @end deftypefun
  1524. The internal state of the random number generator can be initialized in
  1525. several ways. The methods differ in the completeness of the
  1526. information provided.
  1527. @deftypefun void srand48 (long int @var{seedval})
  1528. @standards{SVID, stdlib.h}
  1529. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1530. The @code{srand48} function sets the most significant 32 bits of the
  1531. internal state of the random number generator to the least
  1532. significant 32 bits of the @var{seedval} parameter. The lower 16 bits
  1533. are initialized to the value @code{0x330E}. Even if the @code{long
  1534. int} type contains more than 32 bits only the lower 32 bits are used.
  1535. Owing to this limitation, initialization of the state of this
  1536. function is not very useful. But it makes it easy to use a construct
  1537. like @code{srand48 (time (0))}.
  1538. A side-effect of this function is that the values @code{a} and @code{c}
  1539. from the internal state, which are used in the congruential formula,
  1540. are reset to the default values given above. This is of importance once
  1541. the user has called the @code{lcong48} function (see below).
  1542. @end deftypefun
  1543. @deftypefun {unsigned short int *} seed48 (unsigned short int @var{seed16v}[3])
  1544. @standards{SVID, stdlib.h}
  1545. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1546. The @code{seed48} function initializes all 48 bits of the state of the
  1547. internal random number generator from the contents of the parameter
  1548. @var{seed16v}. Here the lower 16 bits of the first element of
  1549. @var{seed16v} initialize the least significant 16 bits of the internal
  1550. state, the lower 16 bits of @code{@var{seed16v}[1]} initialize the mid-order
  1551. 16 bits of the state and the 16 lower bits of @code{@var{seed16v}[2]}
  1552. initialize the most significant 16 bits of the state.
  1553. Unlike @code{srand48} this function lets the user initialize all 48 bits
  1554. of the state.
  1555. The value returned by @code{seed48} is a pointer to an array containing
  1556. the values of the internal state before the change. This might be
  1557. useful to restart the random number generator at a certain state.
  1558. Otherwise the value can simply be ignored.
  1559. As for @code{srand48}, the values @code{a} and @code{c} from the
  1560. congruential formula are reset to the default values.
  1561. @end deftypefun
  1562. There is one more function to initialize the random number generator
  1563. which enables you to specify even more information by allowing you to
  1564. change the parameters in the congruential formula.
  1565. @deftypefun void lcong48 (unsigned short int @var{param}[7])
  1566. @standards{SVID, stdlib.h}
  1567. @safety{@prelim{}@mtunsafe{@mtasurace{:drand48}}@asunsafe{}@acunsafe{@acucorrupt{}}}
  1568. The @code{lcong48} function allows the user to change the complete state
  1569. of the random number generator. Unlike @code{srand48} and
  1570. @code{seed48}, this function also changes the constants in the
  1571. congruential formula.
  1572. From the seven elements in the array @var{param} the least significant
  1573. 16 bits of the entries @code{@var{param}[0]} to @code{@var{param}[2]}
  1574. determine the initial state, the least significant 16 bits of
  1575. @code{@var{param}[3]} to @code{@var{param}[5]} determine the 48 bit
  1576. constant @code{a} and @code{@var{param}[6]} determines the 16-bit value
  1577. @code{c}.
  1578. @end deftypefun
  1579. All the above functions have in common that they use the global
  1580. parameters for the congruential formula. In multi-threaded programs it
  1581. might sometimes be useful to have different parameters in different
  1582. threads. For this reason all the above functions have a counterpart
  1583. which works on a description of the random number generator in the
  1584. user-supplied buffer instead of the global state.
  1585. Please note that it is no problem if several threads use the global
  1586. state if all threads use the functions which take a pointer to an array
  1587. containing the state. The random numbers are computed following the
  1588. same loop but if the state in the array is different all threads will
  1589. obtain an individual random number generator.
  1590. The user-supplied buffer must be of type @code{struct drand48_data}.
  1591. This type should be regarded as opaque and not manipulated directly.
  1592. @deftypefun int drand48_r (struct drand48_data *@var{buffer}, double *@var{result})
  1593. @standards{GNU, stdlib.h}
  1594. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1595. This function is equivalent to the @code{drand48} function with the
  1596. difference that it does not modify the global random number generator
  1597. parameters but instead the parameters in the buffer supplied through the
  1598. pointer @var{buffer}. The random number is returned in the variable
  1599. pointed to by @var{result}.
  1600. The return value of the function indicates whether the call succeeded.
  1601. If the value is less than @code{0} an error occurred and @code{errno} is
  1602. set to indicate the problem.
  1603. This function is a GNU extension and should not be used in portable
  1604. programs.
  1605. @end deftypefun
  1606. @deftypefun int erand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, double *@var{result})
  1607. @standards{GNU, stdlib.h}
  1608. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1609. The @code{erand48_r} function works like @code{erand48}, but in addition
  1610. it takes an argument @var{buffer} which describes the random number
  1611. generator. The state of the random number generator is taken from the
  1612. @code{xsubi} array, the parameters for the congruential formula from the
  1613. global random number generator data. The random number is returned in
  1614. the variable pointed to by @var{result}.
  1615. The return value is non-negative if the call succeeded.
  1616. This function is a GNU extension and should not be used in portable
  1617. programs.
  1618. @end deftypefun
  1619. @deftypefun int lrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
  1620. @standards{GNU, stdlib.h}
  1621. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1622. This function is similar to @code{lrand48}, but in addition it takes a
  1623. pointer to a buffer describing the state of the random number generator
  1624. just like @code{drand48}.
  1625. If the return value of the function is non-negative the variable pointed
  1626. to by @var{result} contains the result. Otherwise an error occurred.
  1627. This function is a GNU extension and should not be used in portable
  1628. programs.
  1629. @end deftypefun
  1630. @deftypefun int nrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
  1631. @standards{GNU, stdlib.h}
  1632. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1633. The @code{nrand48_r} function works like @code{nrand48} in that it
  1634. produces a random number in the range @code{0} to @code{2^31}. But instead
  1635. of using the global parameters for the congruential formula it uses the
  1636. information from the buffer pointed to by @var{buffer}. The state is
  1637. described by the values in @var{xsubi}.
  1638. If the return value is non-negative the variable pointed to by
  1639. @var{result} contains the result.
  1640. This function is a GNU extension and should not be used in portable
  1641. programs.
  1642. @end deftypefun
  1643. @deftypefun int mrand48_r (struct drand48_data *@var{buffer}, long int *@var{result})
  1644. @standards{GNU, stdlib.h}
  1645. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1646. This function is similar to @code{mrand48} but like the other reentrant
  1647. functions it uses the random number generator described by the value in
  1648. the buffer pointed to by @var{buffer}.
  1649. If the return value is non-negative the variable pointed to by
  1650. @var{result} contains the result.
  1651. This function is a GNU extension and should not be used in portable
  1652. programs.
  1653. @end deftypefun
  1654. @deftypefun int jrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
  1655. @standards{GNU, stdlib.h}
  1656. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1657. The @code{jrand48_r} function is similar to @code{jrand48}. Like the
  1658. other reentrant functions of this function family it uses the
  1659. congruential formula parameters from the buffer pointed to by
  1660. @var{buffer}.
  1661. If the return value is non-negative the variable pointed to by
  1662. @var{result} contains the result.
  1663. This function is a GNU extension and should not be used in portable
  1664. programs.
  1665. @end deftypefun
  1666. Before any of the above functions are used the buffer of type
  1667. @code{struct drand48_data} should be initialized. The easiest way to do
  1668. this is to fill the whole buffer with null bytes, e.g. by
  1669. @smallexample
  1670. memset (buffer, '\0', sizeof (struct drand48_data));
  1671. @end smallexample
  1672. @noindent
  1673. Using any of the reentrant functions of this family now will
  1674. automatically initialize the random number generator to the default
  1675. values for the state and the parameters of the congruential formula.
  1676. The other possibility is to use any of the functions which explicitly
  1677. initialize the buffer. Though it might be obvious how to initialize the
  1678. buffer from looking at the parameter to the function, it is highly
  1679. recommended to use these functions since the result might not always be
  1680. what you expect.
  1681. @deftypefun int srand48_r (long int @var{seedval}, struct drand48_data *@var{buffer})
  1682. @standards{GNU, stdlib.h}
  1683. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1684. The description of the random number generator represented by the
  1685. information in @var{buffer} is initialized similarly to what the function
  1686. @code{srand48} does. The state is initialized from the parameter
  1687. @var{seedval} and the parameters for the congruential formula are
  1688. initialized to their default values.
  1689. If the return value is non-negative the function call succeeded.
  1690. This function is a GNU extension and should not be used in portable
  1691. programs.
  1692. @end deftypefun
  1693. @deftypefun int seed48_r (unsigned short int @var{seed16v}[3], struct drand48_data *@var{buffer})
  1694. @standards{GNU, stdlib.h}
  1695. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1696. This function is similar to @code{srand48_r} but like @code{seed48} it
  1697. initializes all 48 bits of the state from the parameter @var{seed16v}.
  1698. If the return value is non-negative the function call succeeded. It
  1699. does not return a pointer to the previous state of the random number
  1700. generator like the @code{seed48} function does. If the user wants to
  1701. preserve the state for a later re-run s/he can copy the whole buffer
  1702. pointed to by @var{buffer}.
  1703. This function is a GNU extension and should not be used in portable
  1704. programs.
  1705. @end deftypefun
  1706. @deftypefun int lcong48_r (unsigned short int @var{param}[7], struct drand48_data *@var{buffer})
  1707. @standards{GNU, stdlib.h}
  1708. @safety{@prelim{}@mtsafe{@mtsrace{:buffer}}@assafe{}@acunsafe{@acucorrupt{}}}
  1709. This function initializes all aspects of the random number generator
  1710. described in @var{buffer} with the data in @var{param}. Here it is
  1711. especially true that the function does more than just copying the
  1712. contents of @var{param} and @var{buffer}. More work is required and
  1713. therefore it is important to use this function rather than initializing
  1714. the random number generator directly.
  1715. If the return value is non-negative the function call succeeded.
  1716. This function is a GNU extension and should not be used in portable
  1717. programs.
  1718. @end deftypefun
  1719. @node FP Function Optimizations
  1720. @section Is Fast Code or Small Code preferred?
  1721. @cindex Optimization
  1722. If an application uses many floating point functions it is often the case
  1723. that the cost of the function calls themselves is not negligible.
  1724. Modern processors can often execute the operations themselves
  1725. very fast, but the function call disrupts the instruction pipeline.
  1726. For this reason @theglibc{} provides optimizations for many of the
  1727. frequently-used math functions. When GNU CC is used and the user
  1728. activates the optimizer, several new inline functions and macros are
  1729. defined. These new functions and macros have the same names as the
  1730. library functions and so are used instead of the latter. In the case of
  1731. inline functions the compiler will decide whether it is reasonable to
  1732. use them, and this decision is usually correct.
  1733. This means that no calls to the library functions may be necessary, and
  1734. can increase the speed of generated code significantly. The drawback is
  1735. that code size will increase, and the increase is not always negligible.
  1736. There are two kinds of inline functions: those that give the same result
  1737. as the library functions and others that might not set @code{errno} and
  1738. might have a reduced precision and/or argument range in comparison with
  1739. the library functions. The latter inline functions are only available
  1740. if the flag @code{-ffast-math} is given to GNU CC.
  1741. In cases where the inline functions and macros are not wanted the symbol
  1742. @code{__NO_MATH_INLINES} should be defined before any system header is
  1743. included. This will ensure that only library functions are used. Of
  1744. course, it can be determined for each file in the project whether
  1745. giving this option is preferable or not.
  1746. Not all hardware implements the entire @w{IEEE 754} standard, and even
  1747. if it does there may be a substantial performance penalty for using some
  1748. of its features. For example, enabling traps on some processors forces
  1749. the FPU to run un-pipelined, which can more than double calculation time.
  1750. @c ***Add explanation of -lieee, -mieee.