arith.texi 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337
  1. @node Arithmetic, Bit Manipulation, Mathematics, Top
  2. @c %MENU% Low level arithmetic functions
  3. @chapter Arithmetic Functions
  4. This chapter contains information about functions for doing basic
  5. arithmetic operations, such as splitting a float into its integer and
  6. fractional parts or retrieving the imaginary part of a complex value.
  7. These functions are declared in the header files @file{math.h} and
  8. @file{complex.h}.
  9. @menu
  10. * Integers:: Basic integer types and concepts
  11. * Integer Division:: Integer division with guaranteed rounding.
  12. * Floating Point Numbers:: Basic concepts. IEEE 754.
  13. * Floating Point Classes:: The five kinds of floating-point number.
  14. * Floating Point Errors:: When something goes wrong in a calculation.
  15. * Rounding:: Controlling how results are rounded.
  16. * Control Functions:: Saving and restoring the FPU's state.
  17. * Arithmetic Functions:: Fundamental operations provided by the library.
  18. * Complex Numbers:: The types. Writing complex constants.
  19. * Operations on Complex:: Projection, conjugation, decomposition.
  20. * Parsing of Numbers:: Converting strings to numbers.
  21. * Printing of Floats:: Converting floating-point numbers to strings.
  22. * System V Number Conversion:: An archaic way to convert numbers to strings.
  23. @end menu
  24. @node Integers
  25. @section Integers
  26. @cindex integer
  27. The C language defines several integer data types: integer, short integer,
  28. long integer, and character, all in both signed and unsigned varieties.
  29. The GNU C compiler extends the language to contain long long integers
  30. as well.
  31. @cindex signedness
  32. The C integer types were intended to allow code to be portable among
  33. machines with different inherent data sizes (word sizes), so each type
  34. may have different ranges on different machines. The problem with
  35. this is that a program often needs to be written for a particular range
  36. of integers, and sometimes must be written for a particular size of
  37. storage, regardless of what machine the program runs on.
  38. To address this problem, @theglibc{} contains C type definitions
  39. you can use to declare integers that meet your exact needs. Because the
  40. @glibcadj{} header files are customized to a specific machine, your
  41. program source code doesn't have to be.
  42. These @code{typedef}s are in @file{stdint.h}.
  43. @pindex stdint.h
  44. If you require that an integer be represented in exactly N bits, use one
  45. of the following types, with the obvious mapping to bit size and signedness:
  46. @itemize @bullet
  47. @item int8_t
  48. @item int16_t
  49. @item int32_t
  50. @item int64_t
  51. @item uint8_t
  52. @item uint16_t
  53. @item uint32_t
  54. @item uint64_t
  55. @end itemize
  56. If your C compiler and target machine do not allow integers of a certain
  57. size, the corresponding above type does not exist.
  58. If you don't need a specific storage size, but want the smallest data
  59. structure with @emph{at least} N bits, use one of these:
  60. @itemize @bullet
  61. @item int_least8_t
  62. @item int_least16_t
  63. @item int_least32_t
  64. @item int_least64_t
  65. @item uint_least8_t
  66. @item uint_least16_t
  67. @item uint_least32_t
  68. @item uint_least64_t
  69. @end itemize
  70. If you don't need a specific storage size, but want the data structure
  71. that allows the fastest access while having at least N bits (and
  72. among data structures with the same access speed, the smallest one), use
  73. one of these:
  74. @itemize @bullet
  75. @item int_fast8_t
  76. @item int_fast16_t
  77. @item int_fast32_t
  78. @item int_fast64_t
  79. @item uint_fast8_t
  80. @item uint_fast16_t
  81. @item uint_fast32_t
  82. @item uint_fast64_t
  83. @end itemize
  84. If you want an integer with the widest range possible on the platform on
  85. which it is being used, use one of the following. If you use these,
  86. you should write code that takes into account the variable size and range
  87. of the integer.
  88. @itemize @bullet
  89. @item intmax_t
  90. @item uintmax_t
  91. @end itemize
  92. @Theglibc{} also provides macros that tell you the maximum and
  93. minimum possible values for each integer data type. The macro names
  94. follow these examples: @code{INT32_MAX}, @code{UINT8_MAX},
  95. @code{INT_FAST32_MIN}, @code{INT_LEAST64_MIN}, @code{UINTMAX_MAX},
  96. @code{INTMAX_MAX}, @code{INTMAX_MIN}. Note that there are no macros for
  97. unsigned integer minima. These are always zero. Similarly, there
  98. are macros such as @code{INTMAX_WIDTH} for the width of these types.
  99. Those macros for integer type widths come from TS 18661-1:2014.
  100. @cindex maximum possible integer
  101. @cindex minimum possible integer
  102. There are similar macros for use with C's built in integer types which
  103. should come with your C compiler. These are described in @ref{Data Type
  104. Measurements}.
  105. Don't forget you can use the C @code{sizeof} function with any of these
  106. data types to get the number of bytes of storage each uses.
  107. @node Integer Division
  108. @section Integer Division
  109. @cindex integer division functions
  110. This section describes functions for performing integer division. These
  111. functions are redundant when GNU CC is used, because in GNU C the
  112. @samp{/} operator always rounds towards zero. But in other C
  113. implementations, @samp{/} may round differently with negative arguments.
  114. @code{div} and @code{ldiv} are useful because they specify how to round
  115. the quotient: towards zero. The remainder has the same sign as the
  116. numerator.
  117. These functions are specified to return a result @var{r} such that the value
  118. @code{@var{r}.quot*@var{denominator} + @var{r}.rem} equals
  119. @var{numerator}.
  120. @pindex stdlib.h
  121. To use these facilities, you should include the header file
  122. @file{stdlib.h} in your program.
  123. @deftp {Data Type} div_t
  124. @standards{ISO, stdlib.h}
  125. This is a structure type used to hold the result returned by the @code{div}
  126. function. It has the following members:
  127. @table @code
  128. @item int quot
  129. The quotient from the division.
  130. @item int rem
  131. The remainder from the division.
  132. @end table
  133. @end deftp
  134. @deftypefun div_t div (int @var{numerator}, int @var{denominator})
  135. @standards{ISO, stdlib.h}
  136. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  137. @c Functions in this section are pure, and thus safe.
  138. The function @code{div} computes the quotient and remainder from
  139. the division of @var{numerator} by @var{denominator}, returning the
  140. result in a structure of type @code{div_t}.
  141. If the result cannot be represented (as in a division by zero), the
  142. behavior is undefined.
  143. Here is an example, albeit not a very useful one.
  144. @smallexample
  145. div_t result;
  146. result = div (20, -6);
  147. @end smallexample
  148. @noindent
  149. Now @code{result.quot} is @code{-3} and @code{result.rem} is @code{2}.
  150. @end deftypefun
  151. @deftp {Data Type} ldiv_t
  152. @standards{ISO, stdlib.h}
  153. This is a structure type used to hold the result returned by the @code{ldiv}
  154. function. It has the following members:
  155. @table @code
  156. @item long int quot
  157. The quotient from the division.
  158. @item long int rem
  159. The remainder from the division.
  160. @end table
  161. (This is identical to @code{div_t} except that the components are of
  162. type @code{long int} rather than @code{int}.)
  163. @end deftp
  164. @deftypefun ldiv_t ldiv (long int @var{numerator}, long int @var{denominator})
  165. @standards{ISO, stdlib.h}
  166. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  167. The @code{ldiv} function is similar to @code{div}, except that the
  168. arguments are of type @code{long int} and the result is returned as a
  169. structure of type @code{ldiv_t}.
  170. @end deftypefun
  171. @deftp {Data Type} lldiv_t
  172. @standards{ISO, stdlib.h}
  173. This is a structure type used to hold the result returned by the @code{lldiv}
  174. function. It has the following members:
  175. @table @code
  176. @item long long int quot
  177. The quotient from the division.
  178. @item long long int rem
  179. The remainder from the division.
  180. @end table
  181. (This is identical to @code{div_t} except that the components are of
  182. type @code{long long int} rather than @code{int}.)
  183. @end deftp
  184. @deftypefun lldiv_t lldiv (long long int @var{numerator}, long long int @var{denominator})
  185. @standards{ISO, stdlib.h}
  186. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  187. The @code{lldiv} function is like the @code{div} function, but the
  188. arguments are of type @code{long long int} and the result is returned as
  189. a structure of type @code{lldiv_t}.
  190. The @code{lldiv} function was added in @w{ISO C99}.
  191. @end deftypefun
  192. @deftp {Data Type} imaxdiv_t
  193. @standards{ISO, inttypes.h}
  194. This is a structure type used to hold the result returned by the @code{imaxdiv}
  195. function. It has the following members:
  196. @table @code
  197. @item intmax_t quot
  198. The quotient from the division.
  199. @item intmax_t rem
  200. The remainder from the division.
  201. @end table
  202. (This is identical to @code{div_t} except that the components are of
  203. type @code{intmax_t} rather than @code{int}.)
  204. See @ref{Integers} for a description of the @code{intmax_t} type.
  205. @end deftp
  206. @deftypefun imaxdiv_t imaxdiv (intmax_t @var{numerator}, intmax_t @var{denominator})
  207. @standards{ISO, inttypes.h}
  208. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  209. The @code{imaxdiv} function is like the @code{div} function, but the
  210. arguments are of type @code{intmax_t} and the result is returned as
  211. a structure of type @code{imaxdiv_t}.
  212. See @ref{Integers} for a description of the @code{intmax_t} type.
  213. The @code{imaxdiv} function was added in @w{ISO C99}.
  214. @end deftypefun
  215. @node Floating Point Numbers
  216. @section Floating Point Numbers
  217. @cindex floating point
  218. @cindex IEEE 754
  219. @cindex IEEE floating point
  220. Most computer hardware has support for two different kinds of numbers:
  221. integers (@math{@dots{}-3, -2, -1, 0, 1, 2, 3@dots{}}) and
  222. floating-point numbers. Floating-point numbers have three parts: the
  223. @dfn{mantissa}, the @dfn{exponent}, and the @dfn{sign bit}. The real
  224. number represented by a floating-point value is given by
  225. @tex
  226. $(s \mathrel? -1 \mathrel: 1) \cdot 2^e \cdot M$
  227. @end tex
  228. @ifnottex
  229. @math{(s ? -1 : 1) @mul{} 2^e @mul{} M}
  230. @end ifnottex
  231. where @math{s} is the sign bit, @math{e} the exponent, and @math{M}
  232. the mantissa. @xref{Floating Point Concepts}, for details. (It is
  233. possible to have a different @dfn{base} for the exponent, but all modern
  234. hardware uses @math{2}.)
  235. Floating-point numbers can represent a finite subset of the real
  236. numbers. While this subset is large enough for most purposes, it is
  237. important to remember that the only reals that can be represented
  238. exactly are rational numbers that have a terminating binary expansion
  239. shorter than the width of the mantissa. Even simple fractions such as
  240. @math{1/5} can only be approximated by floating point.
  241. Mathematical operations and functions frequently need to produce values
  242. that are not representable. Often these values can be approximated
  243. closely enough for practical purposes, but sometimes they can't.
  244. Historically there was no way to tell when the results of a calculation
  245. were inaccurate. Modern computers implement the @w{IEEE 754} standard
  246. for numerical computations, which defines a framework for indicating to
  247. the program when the results of calculation are not trustworthy. This
  248. framework consists of a set of @dfn{exceptions} that indicate why a
  249. result could not be represented, and the special values @dfn{infinity}
  250. and @dfn{not a number} (NaN).
  251. @node Floating Point Classes
  252. @section Floating-Point Number Classification Functions
  253. @cindex floating-point classes
  254. @cindex classes, floating-point
  255. @pindex math.h
  256. @w{ISO C99} defines macros that let you determine what sort of
  257. floating-point number a variable holds.
  258. @deftypefn {Macro} int fpclassify (@emph{float-type} @var{x})
  259. @standards{ISO, math.h}
  260. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  261. This is a generic macro which works on all floating-point types and
  262. which returns a value of type @code{int}. The possible values are:
  263. @vtable @code
  264. @item FP_NAN
  265. @standards{C99, math.h}
  266. The floating-point number @var{x} is ``Not a Number'' (@pxref{Infinity
  267. and NaN})
  268. @item FP_INFINITE
  269. @standards{C99, math.h}
  270. The value of @var{x} is either plus or minus infinity (@pxref{Infinity
  271. and NaN})
  272. @item FP_ZERO
  273. @standards{C99, math.h}
  274. The value of @var{x} is zero. In floating-point formats like @w{IEEE
  275. 754}, where zero can be signed, this value is also returned if
  276. @var{x} is negative zero.
  277. @item FP_SUBNORMAL
  278. @standards{C99, math.h}
  279. Numbers whose absolute value is too small to be represented in the
  280. normal format are represented in an alternate, @dfn{denormalized} format
  281. (@pxref{Floating Point Concepts}). This format is less precise but can
  282. represent values closer to zero. @code{fpclassify} returns this value
  283. for values of @var{x} in this alternate format.
  284. @item FP_NORMAL
  285. @standards{C99, math.h}
  286. This value is returned for all other values of @var{x}. It indicates
  287. that there is nothing special about the number.
  288. @end vtable
  289. @end deftypefn
  290. @code{fpclassify} is most useful if more than one property of a number
  291. must be tested. There are more specific macros which only test one
  292. property at a time. Generally these macros execute faster than
  293. @code{fpclassify}, since there is special hardware support for them.
  294. You should therefore use the specific macros whenever possible.
  295. @deftypefn {Macro} int iscanonical (@emph{float-type} @var{x})
  296. @standards{ISO, math.h}
  297. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  298. In some floating-point formats, some values have canonical (preferred)
  299. and noncanonical encodings (for IEEE interchange binary formats, all
  300. encodings are canonical). This macro returns a nonzero value if
  301. @var{x} has a canonical encoding. It is from TS 18661-1:2014.
  302. Note that some formats have multiple encodings of a value which are
  303. all equally canonical; @code{iscanonical} returns a nonzero value for
  304. all such encodings. Also, formats may have encodings that do not
  305. correspond to any valid value of the type. In ISO C terms these are
  306. @dfn{trap representations}; in @theglibc{}, @code{iscanonical} returns
  307. zero for such encodings.
  308. @end deftypefn
  309. @deftypefn {Macro} int isfinite (@emph{float-type} @var{x})
  310. @standards{ISO, math.h}
  311. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  312. This macro returns a nonzero value if @var{x} is finite: not plus or
  313. minus infinity, and not NaN. It is equivalent to
  314. @smallexample
  315. (fpclassify (x) != FP_NAN && fpclassify (x) != FP_INFINITE)
  316. @end smallexample
  317. @code{isfinite} is implemented as a macro which accepts any
  318. floating-point type.
  319. @end deftypefn
  320. @deftypefn {Macro} int isnormal (@emph{float-type} @var{x})
  321. @standards{ISO, math.h}
  322. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  323. This macro returns a nonzero value if @var{x} is finite and normalized.
  324. It is equivalent to
  325. @smallexample
  326. (fpclassify (x) == FP_NORMAL)
  327. @end smallexample
  328. @end deftypefn
  329. @deftypefn {Macro} int isnan (@emph{float-type} @var{x})
  330. @standards{ISO, math.h}
  331. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  332. This macro returns a nonzero value if @var{x} is NaN. It is equivalent
  333. to
  334. @smallexample
  335. (fpclassify (x) == FP_NAN)
  336. @end smallexample
  337. @end deftypefn
  338. @deftypefn {Macro} int issignaling (@emph{float-type} @var{x})
  339. @standards{ISO, math.h}
  340. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  341. This macro returns a nonzero value if @var{x} is a signaling NaN
  342. (sNaN). It is from TS 18661-1:2014.
  343. @end deftypefn
  344. @deftypefn {Macro} int issubnormal (@emph{float-type} @var{x})
  345. @standards{ISO, math.h}
  346. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  347. This macro returns a nonzero value if @var{x} is subnormal. It is
  348. from TS 18661-1:2014.
  349. @end deftypefn
  350. @deftypefn {Macro} int iszero (@emph{float-type} @var{x})
  351. @standards{ISO, math.h}
  352. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  353. This macro returns a nonzero value if @var{x} is zero. It is from TS
  354. 18661-1:2014.
  355. @end deftypefn
  356. Another set of floating-point classification functions was provided by
  357. BSD. @Theglibc{} also supports these functions; however, we
  358. recommend that you use the ISO C99 macros in new code. Those are standard
  359. and will be available more widely. Also, since they are macros, you do
  360. not have to worry about the type of their argument.
  361. @deftypefun int isinf (double @var{x})
  362. @deftypefunx int isinff (float @var{x})
  363. @deftypefunx int isinfl (long double @var{x})
  364. @standards{BSD, math.h}
  365. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  366. This function returns @code{-1} if @var{x} represents negative infinity,
  367. @code{1} if @var{x} represents positive infinity, and @code{0} otherwise.
  368. @end deftypefun
  369. @deftypefun int isnan (double @var{x})
  370. @deftypefunx int isnanf (float @var{x})
  371. @deftypefunx int isnanl (long double @var{x})
  372. @standards{BSD, math.h}
  373. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  374. This function returns a nonzero value if @var{x} is a ``not a number''
  375. value, and zero otherwise.
  376. @strong{NB:} The @code{isnan} macro defined by @w{ISO C99} overrides
  377. the BSD function. This is normally not a problem, because the two
  378. routines behave identically. However, if you really need to get the BSD
  379. function for some reason, you can write
  380. @smallexample
  381. (isnan) (x)
  382. @end smallexample
  383. @end deftypefun
  384. @deftypefun int finite (double @var{x})
  385. @deftypefunx int finitef (float @var{x})
  386. @deftypefunx int finitel (long double @var{x})
  387. @standards{BSD, math.h}
  388. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  389. This function returns a nonzero value if @var{x} is neither infinite nor
  390. a ``not a number'' value, and zero otherwise.
  391. @end deftypefun
  392. @strong{Portability Note:} The functions listed in this section are BSD
  393. extensions.
  394. @node Floating Point Errors
  395. @section Errors in Floating-Point Calculations
  396. @menu
  397. * FP Exceptions:: IEEE 754 math exceptions and how to detect them.
  398. * Infinity and NaN:: Special values returned by calculations.
  399. * Status bit operations:: Checking for exceptions after the fact.
  400. * Math Error Reporting:: How the math functions report errors.
  401. @end menu
  402. @node FP Exceptions
  403. @subsection FP Exceptions
  404. @cindex exception
  405. @cindex signal
  406. @cindex zero divide
  407. @cindex division by zero
  408. @cindex inexact exception
  409. @cindex invalid exception
  410. @cindex overflow exception
  411. @cindex underflow exception
  412. The @w{IEEE 754} standard defines five @dfn{exceptions} that can occur
  413. during a calculation. Each corresponds to a particular sort of error,
  414. such as overflow.
  415. When exceptions occur (when exceptions are @dfn{raised}, in the language
  416. of the standard), one of two things can happen. By default the
  417. exception is simply noted in the floating-point @dfn{status word}, and
  418. the program continues as if nothing had happened. The operation
  419. produces a default value, which depends on the exception (see the table
  420. below). Your program can check the status word to find out which
  421. exceptions happened.
  422. Alternatively, you can enable @dfn{traps} for exceptions. In that case,
  423. when an exception is raised, your program will receive the @code{SIGFPE}
  424. signal. The default action for this signal is to terminate the
  425. program. @xref{Signal Handling}, for how you can change the effect of
  426. the signal.
  427. @noindent
  428. The exceptions defined in @w{IEEE 754} are:
  429. @table @samp
  430. @item Invalid Operation
  431. This exception is raised if the given operands are invalid for the
  432. operation to be performed. Examples are
  433. (see @w{IEEE 754}, @w{section 7}):
  434. @enumerate
  435. @item
  436. Addition or subtraction: @math{@infinity{} - @infinity{}}. (But
  437. @math{@infinity{} + @infinity{} = @infinity{}}).
  438. @item
  439. Multiplication: @math{0 @mul{} @infinity{}}.
  440. @item
  441. Division: @math{0/0} or @math{@infinity{}/@infinity{}}.
  442. @item
  443. Remainder: @math{x} REM @math{y}, where @math{y} is zero or @math{x} is
  444. infinite.
  445. @item
  446. Square root if the operand is less than zero. More generally, any
  447. mathematical function evaluated outside its domain produces this
  448. exception.
  449. @item
  450. Conversion of a floating-point number to an integer or decimal
  451. string, when the number cannot be represented in the target format (due
  452. to overflow, infinity, or NaN).
  453. @item
  454. Conversion of an unrecognizable input string.
  455. @item
  456. Comparison via predicates involving @math{<} or @math{>}, when one or
  457. other of the operands is NaN. You can prevent this exception by using
  458. the unordered comparison functions instead; see @ref{FP Comparison Functions}.
  459. @end enumerate
  460. If the exception does not trap, the result of the operation is NaN.
  461. @item Division by Zero
  462. This exception is raised when a finite nonzero number is divided
  463. by zero. If no trap occurs the result is either @math{+@infinity{}} or
  464. @math{-@infinity{}}, depending on the signs of the operands.
  465. @item Overflow
  466. This exception is raised whenever the result cannot be represented
  467. as a finite value in the precision format of the destination. If no trap
  468. occurs the result depends on the sign of the intermediate result and the
  469. current rounding mode (@w{IEEE 754}, @w{section 7.3}):
  470. @enumerate
  471. @item
  472. Round to nearest carries all overflows to @math{@infinity{}}
  473. with the sign of the intermediate result.
  474. @item
  475. Round toward @math{0} carries all overflows to the largest representable
  476. finite number with the sign of the intermediate result.
  477. @item
  478. Round toward @math{-@infinity{}} carries positive overflows to the
  479. largest representable finite number and negative overflows to
  480. @math{-@infinity{}}.
  481. @item
  482. Round toward @math{@infinity{}} carries negative overflows to the
  483. most negative representable finite number and positive overflows
  484. to @math{@infinity{}}.
  485. @end enumerate
  486. Whenever the overflow exception is raised, the inexact exception is also
  487. raised.
  488. @item Underflow
  489. The underflow exception is raised when an intermediate result is too
  490. small to be calculated accurately, or if the operation's result rounded
  491. to the destination precision is too small to be normalized.
  492. When no trap is installed for the underflow exception, underflow is
  493. signaled (via the underflow flag) only when both tininess and loss of
  494. accuracy have been detected. If no trap handler is installed the
  495. operation continues with an imprecise small value, or zero if the
  496. destination precision cannot hold the small exact result.
  497. @item Inexact
  498. This exception is signalled if a rounded result is not exact (such as
  499. when calculating the square root of two) or a result overflows without
  500. an overflow trap.
  501. @end table
  502. @node Infinity and NaN
  503. @subsection Infinity and NaN
  504. @cindex infinity
  505. @cindex not a number
  506. @cindex NaN
  507. @w{IEEE 754} floating point numbers can represent positive or negative
  508. infinity, and @dfn{NaN} (not a number). These three values arise from
  509. calculations whose result is undefined or cannot be represented
  510. accurately. You can also deliberately set a floating-point variable to
  511. any of them, which is sometimes useful. Some examples of calculations
  512. that produce infinity or NaN:
  513. @ifnottex
  514. @smallexample
  515. @math{1/0 = @infinity{}}
  516. @math{log (0) = -@infinity{}}
  517. @math{sqrt (-1) = NaN}
  518. @end smallexample
  519. @end ifnottex
  520. @tex
  521. $${1\over0} = \infty$$
  522. $$\log 0 = -\infty$$
  523. $$\sqrt{-1} = \hbox{NaN}$$
  524. @end tex
  525. When a calculation produces any of these values, an exception also
  526. occurs; see @ref{FP Exceptions}.
  527. The basic operations and math functions all accept infinity and NaN and
  528. produce sensible output. Infinities propagate through calculations as
  529. one would expect: for example, @math{2 + @infinity{} = @infinity{}},
  530. @math{4/@infinity{} = 0}, atan @math{(@infinity{}) = @pi{}/2}. NaN, on
  531. the other hand, infects any calculation that involves it. Unless the
  532. calculation would produce the same result no matter what real value
  533. replaced NaN, the result is NaN.
  534. In comparison operations, positive infinity is larger than all values
  535. except itself and NaN, and negative infinity is smaller than all values
  536. except itself and NaN. NaN is @dfn{unordered}: it is not equal to,
  537. greater than, or less than anything, @emph{including itself}. @code{x ==
  538. x} is false if the value of @code{x} is NaN. You can use this to test
  539. whether a value is NaN or not, but the recommended way to test for NaN
  540. is with the @code{isnan} function (@pxref{Floating Point Classes}). In
  541. addition, @code{<}, @code{>}, @code{<=}, and @code{>=} will raise an
  542. exception when applied to NaNs.
  543. @file{math.h} defines macros that allow you to explicitly set a variable
  544. to infinity or NaN.
  545. @deftypevr Macro float INFINITY
  546. @standards{ISO, math.h}
  547. An expression representing positive infinity. It is equal to the value
  548. produced by mathematical operations like @code{1.0 / 0.0}.
  549. @code{-INFINITY} represents negative infinity.
  550. You can test whether a floating-point value is infinite by comparing it
  551. to this macro. However, this is not recommended; you should use the
  552. @code{isfinite} macro instead. @xref{Floating Point Classes}.
  553. This macro was introduced in the @w{ISO C99} standard.
  554. @end deftypevr
  555. @deftypevr Macro float NAN
  556. @standards{GNU, math.h}
  557. An expression representing a value which is ``not a number''. This
  558. macro is a GNU extension, available only on machines that support the
  559. ``not a number'' value---that is to say, on all machines that support
  560. IEEE floating point.
  561. You can use @samp{#ifdef NAN} to test whether the machine supports
  562. NaN. (Of course, you must arrange for GNU extensions to be visible,
  563. such as by defining @code{_GNU_SOURCE}, and then you must include
  564. @file{math.h}.)
  565. @end deftypevr
  566. @deftypevr Macro float SNANF
  567. @deftypevrx Macro double SNAN
  568. @deftypevrx Macro {long double} SNANL
  569. @deftypevrx Macro _FloatN SNANFN
  570. @deftypevrx Macro _FloatNx SNANFNx
  571. @standards{TS 18661-1:2014, math.h}
  572. @standardsx{SNANFN, TS 18661-3:2015, math.h}
  573. @standardsx{SNANFNx, TS 18661-3:2015, math.h}
  574. These macros, defined by TS 18661-1:2014 and TS 18661-3:2015, are
  575. constant expressions for signaling NaNs.
  576. @end deftypevr
  577. @deftypevr Macro int FE_SNANS_ALWAYS_SIGNAL
  578. @standards{ISO, fenv.h}
  579. This macro, defined by TS 18661-1:2014, is defined to @code{1} in
  580. @file{fenv.h} to indicate that functions and operations with signaling
  581. NaN inputs and floating-point results always raise the invalid
  582. exception and return a quiet NaN, even in cases (such as @code{fmax},
  583. @code{hypot} and @code{pow}) where a quiet NaN input can produce a
  584. non-NaN result. Because some compiler optimizations may not handle
  585. signaling NaNs correctly, this macro is only defined if compiler
  586. support for signaling NaNs is enabled. That support can be enabled
  587. with the GCC option @option{-fsignaling-nans}.
  588. @end deftypevr
  589. @w{IEEE 754} also allows for another unusual value: negative zero. This
  590. value is produced when you divide a positive number by negative
  591. infinity, or when a negative result is smaller than the limits of
  592. representation.
  593. @node Status bit operations
  594. @subsection Examining the FPU status word
  595. @w{ISO C99} defines functions to query and manipulate the
  596. floating-point status word. You can use these functions to check for
  597. untrapped exceptions when it's convenient, rather than worrying about
  598. them in the middle of a calculation.
  599. These constants represent the various @w{IEEE 754} exceptions. Not all
  600. FPUs report all the different exceptions. Each constant is defined if
  601. and only if the FPU you are compiling for supports that exception, so
  602. you can test for FPU support with @samp{#ifdef}. They are defined in
  603. @file{fenv.h}.
  604. @vtable @code
  605. @item FE_INEXACT
  606. @standards{ISO, fenv.h}
  607. The inexact exception.
  608. @item FE_DIVBYZERO
  609. @standards{ISO, fenv.h}
  610. The divide by zero exception.
  611. @item FE_UNDERFLOW
  612. @standards{ISO, fenv.h}
  613. The underflow exception.
  614. @item FE_OVERFLOW
  615. @standards{ISO, fenv.h}
  616. The overflow exception.
  617. @item FE_INVALID
  618. @standards{ISO, fenv.h}
  619. The invalid exception.
  620. @end vtable
  621. The macro @code{FE_ALL_EXCEPT} is the bitwise OR of all exception macros
  622. which are supported by the FP implementation.
  623. These functions allow you to clear exception flags, test for exceptions,
  624. and save and restore the set of exceptions flagged.
  625. @deftypefun int feclearexcept (int @var{excepts})
  626. @standards{ISO, fenv.h}
  627. @safety{@prelim{}@mtsafe{}@assafe{@assposix{}}@acsafe{@acsposix{}}}
  628. @c The other functions in this section that modify FP status register
  629. @c mostly do so with non-atomic load-modify-store sequences, but since
  630. @c the register is thread-specific, this should be fine, and safe for
  631. @c cancellation. As long as the FP environment is restored before the
  632. @c signal handler returns control to the interrupted thread (like any
  633. @c kernel should do), the functions are also safe for use in signal
  634. @c handlers.
  635. This function clears all of the supported exception flags indicated by
  636. @var{excepts}.
  637. The function returns zero in case the operation was successful, a
  638. non-zero value otherwise.
  639. @end deftypefun
  640. @deftypefun int feraiseexcept (int @var{excepts})
  641. @standards{ISO, fenv.h}
  642. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  643. This function raises the supported exceptions indicated by
  644. @var{excepts}. If more than one exception bit in @var{excepts} is set
  645. the order in which the exceptions are raised is undefined except that
  646. overflow (@code{FE_OVERFLOW}) or underflow (@code{FE_UNDERFLOW}) are
  647. raised before inexact (@code{FE_INEXACT}). Whether for overflow or
  648. underflow the inexact exception is also raised is also implementation
  649. dependent.
  650. The function returns zero in case the operation was successful, a
  651. non-zero value otherwise.
  652. @end deftypefun
  653. @deftypefun int fesetexcept (int @var{excepts})
  654. @standards{ISO, fenv.h}
  655. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  656. This function sets the supported exception flags indicated by
  657. @var{excepts}, like @code{feraiseexcept}, but without causing enabled
  658. traps to be taken. @code{fesetexcept} is from TS 18661-1:2014.
  659. The function returns zero in case the operation was successful, a
  660. non-zero value otherwise.
  661. @end deftypefun
  662. @deftypefun int fetestexcept (int @var{excepts})
  663. @standards{ISO, fenv.h}
  664. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  665. Test whether the exception flags indicated by the parameter @var{except}
  666. are currently set. If any of them are, a nonzero value is returned
  667. which specifies which exceptions are set. Otherwise the result is zero.
  668. @end deftypefun
  669. To understand these functions, imagine that the status word is an
  670. integer variable named @var{status}. @code{feclearexcept} is then
  671. equivalent to @samp{status &= ~excepts} and @code{fetestexcept} is
  672. equivalent to @samp{(status & excepts)}. The actual implementation may
  673. be very different, of course.
  674. Exception flags are only cleared when the program explicitly requests it,
  675. by calling @code{feclearexcept}. If you want to check for exceptions
  676. from a set of calculations, you should clear all the flags first. Here
  677. is a simple example of the way to use @code{fetestexcept}:
  678. @smallexample
  679. @{
  680. double f;
  681. int raised;
  682. feclearexcept (FE_ALL_EXCEPT);
  683. f = compute ();
  684. raised = fetestexcept (FE_OVERFLOW | FE_INVALID);
  685. if (raised & FE_OVERFLOW) @{ /* @dots{} */ @}
  686. if (raised & FE_INVALID) @{ /* @dots{} */ @}
  687. /* @dots{} */
  688. @}
  689. @end smallexample
  690. You cannot explicitly set bits in the status word. You can, however,
  691. save the entire status word and restore it later. This is done with the
  692. following functions:
  693. @deftypefun int fegetexceptflag (fexcept_t *@var{flagp}, int @var{excepts})
  694. @standards{ISO, fenv.h}
  695. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  696. This function stores in the variable pointed to by @var{flagp} an
  697. implementation-defined value representing the current setting of the
  698. exception flags indicated by @var{excepts}.
  699. The function returns zero in case the operation was successful, a
  700. non-zero value otherwise.
  701. @end deftypefun
  702. @deftypefun int fesetexceptflag (const fexcept_t *@var{flagp}, int @var{excepts})
  703. @standards{ISO, fenv.h}
  704. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  705. This function restores the flags for the exceptions indicated by
  706. @var{excepts} to the values stored in the variable pointed to by
  707. @var{flagp}.
  708. The function returns zero in case the operation was successful, a
  709. non-zero value otherwise.
  710. @end deftypefun
  711. Note that the value stored in @code{fexcept_t} bears no resemblance to
  712. the bit mask returned by @code{fetestexcept}. The type may not even be
  713. an integer. Do not attempt to modify an @code{fexcept_t} variable.
  714. @deftypefun int fetestexceptflag (const fexcept_t *@var{flagp}, int @var{excepts})
  715. @standards{ISO, fenv.h}
  716. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  717. Test whether the exception flags indicated by the parameter
  718. @var{excepts} are set in the variable pointed to by @var{flagp}. If
  719. any of them are, a nonzero value is returned which specifies which
  720. exceptions are set. Otherwise the result is zero.
  721. @code{fetestexceptflag} is from TS 18661-1:2014.
  722. @end deftypefun
  723. @node Math Error Reporting
  724. @subsection Error Reporting by Mathematical Functions
  725. @cindex errors, mathematical
  726. @cindex domain error
  727. @cindex range error
  728. Many of the math functions are defined only over a subset of the real or
  729. complex numbers. Even if they are mathematically defined, their result
  730. may be larger or smaller than the range representable by their return
  731. type without loss of accuracy. These are known as @dfn{domain errors},
  732. @dfn{overflows}, and
  733. @dfn{underflows}, respectively. Math functions do several things when
  734. one of these errors occurs. In this manual we will refer to the
  735. complete response as @dfn{signalling} a domain error, overflow, or
  736. underflow.
  737. When a math function suffers a domain error, it raises the invalid
  738. exception and returns NaN. It also sets @code{errno} to @code{EDOM};
  739. this is for compatibility with old systems that do not support @w{IEEE
  740. 754} exception handling. Likewise, when overflow occurs, math
  741. functions raise the overflow exception and, in the default rounding
  742. mode, return @math{@infinity{}} or @math{-@infinity{}} as appropriate
  743. (in other rounding modes, the largest finite value of the appropriate
  744. sign is returned when appropriate for that rounding mode). They also
  745. set @code{errno} to @code{ERANGE} if returning @math{@infinity{}} or
  746. @math{-@infinity{}}; @code{errno} may or may not be set to
  747. @code{ERANGE} when a finite value is returned on overflow. When
  748. underflow occurs, the underflow exception is raised, and zero
  749. (appropriately signed) or a subnormal value, as appropriate for the
  750. mathematical result of the function and the rounding mode, is
  751. returned. @code{errno} may be set to @code{ERANGE}, but this is not
  752. guaranteed; it is intended that @theglibc{} should set it when the
  753. underflow is to an appropriately signed zero, but not necessarily for
  754. other underflows.
  755. When a math function has an argument that is a signaling NaN,
  756. @theglibc{} does not consider this a domain error, so @code{errno} is
  757. unchanged, but the invalid exception is still raised (except for a few
  758. functions that are specified to handle signaling NaNs differently).
  759. Some of the math functions are defined mathematically to result in a
  760. complex value over parts of their domains. The most familiar example of
  761. this is taking the square root of a negative number. The complex math
  762. functions, such as @code{csqrt}, will return the appropriate complex value
  763. in this case. The real-valued functions, such as @code{sqrt}, will
  764. signal a domain error.
  765. Some older hardware does not support infinities. On that hardware,
  766. overflows instead return a particular very large number (usually the
  767. largest representable number). @file{math.h} defines macros you can use
  768. to test for overflow on both old and new hardware.
  769. @deftypevr Macro double HUGE_VAL
  770. @deftypevrx Macro float HUGE_VALF
  771. @deftypevrx Macro {long double} HUGE_VALL
  772. @deftypevrx Macro _FloatN HUGE_VAL_FN
  773. @deftypevrx Macro _FloatNx HUGE_VAL_FNx
  774. @standards{ISO, math.h}
  775. @standardsx{HUGE_VAL_FN, TS 18661-3:2015, math.h}
  776. @standardsx{HUGE_VAL_FNx, TS 18661-3:2015, math.h}
  777. An expression representing a particular very large number. On machines
  778. that use @w{IEEE 754} floating point format, @code{HUGE_VAL} is infinity.
  779. On other machines, it's typically the largest positive number that can
  780. be represented.
  781. Mathematical functions return the appropriately typed version of
  782. @code{HUGE_VAL} or @code{@minus{}HUGE_VAL} when the result is too large
  783. to be represented.
  784. @end deftypevr
  785. @node Rounding
  786. @section Rounding Modes
  787. Floating-point calculations are carried out internally with extra
  788. precision, and then rounded to fit into the destination type. This
  789. ensures that results are as precise as the input data. @w{IEEE 754}
  790. defines four possible rounding modes:
  791. @table @asis
  792. @item Round to nearest.
  793. This is the default mode. It should be used unless there is a specific
  794. need for one of the others. In this mode results are rounded to the
  795. nearest representable value. If the result is midway between two
  796. representable values, the even representable is chosen. @dfn{Even} here
  797. means the lowest-order bit is zero. This rounding mode prevents
  798. statistical bias and guarantees numeric stability: round-off errors in a
  799. lengthy calculation will remain smaller than half of @code{FLT_EPSILON}.
  800. @c @item Round toward @math{+@infinity{}}
  801. @item Round toward plus Infinity.
  802. All results are rounded to the smallest representable value
  803. which is greater than the result.
  804. @c @item Round toward @math{-@infinity{}}
  805. @item Round toward minus Infinity.
  806. All results are rounded to the largest representable value which is less
  807. than the result.
  808. @item Round toward zero.
  809. All results are rounded to the largest representable value whose
  810. magnitude is less than that of the result. In other words, if the
  811. result is negative it is rounded up; if it is positive, it is rounded
  812. down.
  813. @end table
  814. @noindent
  815. @file{fenv.h} defines constants which you can use to refer to the
  816. various rounding modes. Each one will be defined if and only if the FPU
  817. supports the corresponding rounding mode.
  818. @vtable @code
  819. @item FE_TONEAREST
  820. @standards{ISO, fenv.h}
  821. Round to nearest.
  822. @item FE_UPWARD
  823. @standards{ISO, fenv.h}
  824. Round toward @math{+@infinity{}}.
  825. @item FE_DOWNWARD
  826. @standards{ISO, fenv.h}
  827. Round toward @math{-@infinity{}}.
  828. @item FE_TOWARDZERO
  829. @standards{ISO, fenv.h}
  830. Round toward zero.
  831. @end vtable
  832. Underflow is an unusual case. Normally, @w{IEEE 754} floating point
  833. numbers are always normalized (@pxref{Floating Point Concepts}).
  834. Numbers smaller than @math{2^r} (where @math{r} is the minimum exponent,
  835. @code{FLT_MIN_RADIX-1} for @var{float}) cannot be represented as
  836. normalized numbers. Rounding all such numbers to zero or @math{2^r}
  837. would cause some algorithms to fail at 0. Therefore, they are left in
  838. denormalized form. That produces loss of precision, since some bits of
  839. the mantissa are stolen to indicate the decimal point.
  840. If a result is too small to be represented as a denormalized number, it
  841. is rounded to zero. However, the sign of the result is preserved; if
  842. the calculation was negative, the result is @dfn{negative zero}.
  843. Negative zero can also result from some operations on infinity, such as
  844. @math{4/-@infinity{}}.
  845. At any time, one of the above four rounding modes is selected. You can
  846. find out which one with this function:
  847. @deftypefun int fegetround (void)
  848. @standards{ISO, fenv.h}
  849. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  850. Returns the currently selected rounding mode, represented by one of the
  851. values of the defined rounding mode macros.
  852. @end deftypefun
  853. @noindent
  854. To change the rounding mode, use this function:
  855. @deftypefun int fesetround (int @var{round})
  856. @standards{ISO, fenv.h}
  857. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  858. Changes the currently selected rounding mode to @var{round}. If
  859. @var{round} does not correspond to one of the supported rounding modes
  860. nothing is changed. @code{fesetround} returns zero if it changed the
  861. rounding mode, or a nonzero value if the mode is not supported.
  862. @end deftypefun
  863. You should avoid changing the rounding mode if possible. It can be an
  864. expensive operation; also, some hardware requires you to compile your
  865. program differently for it to work. The resulting code may run slower.
  866. See your compiler documentation for details.
  867. @c This section used to claim that functions existed to round one number
  868. @c in a specific fashion. I can't find any functions in the library
  869. @c that do that. -zw
  870. @node Control Functions
  871. @section Floating-Point Control Functions
  872. @w{IEEE 754} floating-point implementations allow the programmer to
  873. decide whether traps will occur for each of the exceptions, by setting
  874. bits in the @dfn{control word}. In C, traps result in the program
  875. receiving the @code{SIGFPE} signal; see @ref{Signal Handling}.
  876. @strong{NB:} @w{IEEE 754} says that trap handlers are given details of
  877. the exceptional situation, and can set the result value. C signals do
  878. not provide any mechanism to pass this information back and forth.
  879. Trapping exceptions in C is therefore not very useful.
  880. It is sometimes necessary to save the state of the floating-point unit
  881. while you perform some calculation. The library provides functions
  882. which save and restore the exception flags, the set of exceptions that
  883. generate traps, and the rounding mode. This information is known as the
  884. @dfn{floating-point environment}.
  885. The functions to save and restore the floating-point environment all use
  886. a variable of type @code{fenv_t} to store information. This type is
  887. defined in @file{fenv.h}. Its size and contents are
  888. implementation-defined. You should not attempt to manipulate a variable
  889. of this type directly.
  890. To save the state of the FPU, use one of these functions:
  891. @deftypefun int fegetenv (fenv_t *@var{envp})
  892. @standards{ISO, fenv.h}
  893. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  894. Store the floating-point environment in the variable pointed to by
  895. @var{envp}.
  896. The function returns zero in case the operation was successful, a
  897. non-zero value otherwise.
  898. @end deftypefun
  899. @deftypefun int feholdexcept (fenv_t *@var{envp})
  900. @standards{ISO, fenv.h}
  901. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  902. Store the current floating-point environment in the object pointed to by
  903. @var{envp}. Then clear all exception flags, and set the FPU to trap no
  904. exceptions. Not all FPUs support trapping no exceptions; if
  905. @code{feholdexcept} cannot set this mode, it returns nonzero value. If it
  906. succeeds, it returns zero.
  907. @end deftypefun
  908. The functions which restore the floating-point environment can take these
  909. kinds of arguments:
  910. @itemize @bullet
  911. @item
  912. Pointers to @code{fenv_t} objects, which were initialized previously by a
  913. call to @code{fegetenv} or @code{feholdexcept}.
  914. @item
  915. @vindex FE_DFL_ENV
  916. The special macro @code{FE_DFL_ENV} which represents the floating-point
  917. environment as it was available at program start.
  918. @item
  919. Implementation defined macros with names starting with @code{FE_} and
  920. having type @code{fenv_t *}.
  921. @vindex FE_NOMASK_ENV
  922. If possible, @theglibc{} defines a macro @code{FE_NOMASK_ENV}
  923. which represents an environment where every exception raised causes a
  924. trap to occur. You can test for this macro using @code{#ifdef}. It is
  925. only defined if @code{_GNU_SOURCE} is defined.
  926. Some platforms might define other predefined environments.
  927. @end itemize
  928. @noindent
  929. To set the floating-point environment, you can use either of these
  930. functions:
  931. @deftypefun int fesetenv (const fenv_t *@var{envp})
  932. @standards{ISO, fenv.h}
  933. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  934. Set the floating-point environment to that described by @var{envp}.
  935. The function returns zero in case the operation was successful, a
  936. non-zero value otherwise.
  937. @end deftypefun
  938. @deftypefun int feupdateenv (const fenv_t *@var{envp})
  939. @standards{ISO, fenv.h}
  940. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  941. Like @code{fesetenv}, this function sets the floating-point environment
  942. to that described by @var{envp}. However, if any exceptions were
  943. flagged in the status word before @code{feupdateenv} was called, they
  944. remain flagged after the call. In other words, after @code{feupdateenv}
  945. is called, the status word is the bitwise OR of the previous status word
  946. and the one saved in @var{envp}.
  947. The function returns zero in case the operation was successful, a
  948. non-zero value otherwise.
  949. @end deftypefun
  950. @noindent
  951. TS 18661-1:2014 defines additional functions to save and restore
  952. floating-point control modes (such as the rounding mode and whether
  953. traps are enabled) while leaving other status (such as raised flags)
  954. unchanged.
  955. @vindex FE_DFL_MODE
  956. The special macro @code{FE_DFL_MODE} may be passed to
  957. @code{fesetmode}. It represents the floating-point control modes at
  958. program start.
  959. @deftypefun int fegetmode (femode_t *@var{modep})
  960. @standards{ISO, fenv.h}
  961. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  962. Store the floating-point control modes in the variable pointed to by
  963. @var{modep}.
  964. The function returns zero in case the operation was successful, a
  965. non-zero value otherwise.
  966. @end deftypefun
  967. @deftypefun int fesetmode (const femode_t *@var{modep})
  968. @standards{ISO, fenv.h}
  969. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  970. Set the floating-point control modes to those described by
  971. @var{modep}.
  972. The function returns zero in case the operation was successful, a
  973. non-zero value otherwise.
  974. @end deftypefun
  975. @noindent
  976. To control for individual exceptions if raising them causes a trap to
  977. occur, you can use the following two functions.
  978. @strong{Portability Note:} These functions are all GNU extensions.
  979. @deftypefun int feenableexcept (int @var{excepts})
  980. @standards{GNU, fenv.h}
  981. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  982. This function enables traps for each of the exceptions as indicated by
  983. the parameter @var{excepts}. The individual exceptions are described in
  984. @ref{Status bit operations}. Only the specified exceptions are
  985. enabled, the status of the other exceptions is not changed.
  986. The function returns the previous enabled exceptions in case the
  987. operation was successful, @code{-1} otherwise.
  988. Note: Enabling traps for an exception for which the exception flag is
  989. currently already set (@pxref{Status bit operations}) has unspecified
  990. consequences: it may or may not trigger a trap immediately.
  991. @c It triggers a trap immediately on powerpc*, at the next floating-
  992. @c instruction on i386, and not at all on the other CPUs.
  993. @end deftypefun
  994. @deftypefun int fedisableexcept (int @var{excepts})
  995. @standards{GNU, fenv.h}
  996. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  997. This function disables traps for each of the exceptions as indicated by
  998. the parameter @var{excepts}. The individual exceptions are described in
  999. @ref{Status bit operations}. Only the specified exceptions are
  1000. disabled, the status of the other exceptions is not changed.
  1001. The function returns the previous enabled exceptions in case the
  1002. operation was successful, @code{-1} otherwise.
  1003. @end deftypefun
  1004. @deftypefun int fegetexcept (void)
  1005. @standards{GNU, fenv.h}
  1006. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1007. The function returns a bitmask of all currently enabled exceptions. It
  1008. returns @code{-1} in case of failure.
  1009. @end deftypefun
  1010. @node Arithmetic Functions
  1011. @section Arithmetic Functions
  1012. The C library provides functions to do basic operations on
  1013. floating-point numbers. These include absolute value, maximum and minimum,
  1014. normalization, bit twiddling, rounding, and a few others.
  1015. @menu
  1016. * Absolute Value:: Absolute values of integers and floats.
  1017. * Normalization Functions:: Extracting exponents and putting them back.
  1018. * Rounding Functions:: Rounding floats to integers.
  1019. * Remainder Functions:: Remainders on division, precisely defined.
  1020. * FP Bit Twiddling:: Sign bit adjustment. Adding epsilon.
  1021. * FP Comparison Functions:: Comparisons without risk of exceptions.
  1022. * Misc FP Arithmetic:: Max, min, positive difference, multiply-add.
  1023. @end menu
  1024. @node Absolute Value
  1025. @subsection Absolute Value
  1026. @cindex absolute value functions
  1027. These functions are provided for obtaining the @dfn{absolute value} (or
  1028. @dfn{magnitude}) of a number. The absolute value of a real number
  1029. @var{x} is @var{x} if @var{x} is positive, @minus{}@var{x} if @var{x} is
  1030. negative. For a complex number @var{z}, whose real part is @var{x} and
  1031. whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt
  1032. (@var{x}*@var{x} + @var{y}*@var{y})}}.
  1033. @pindex math.h
  1034. @pindex stdlib.h
  1035. Prototypes for @code{abs}, @code{labs}, @code{llabs},
  1036. @code{uabs}, @code{ulabs} and @code{ullabs} are in @file{stdlib.h};
  1037. @code{imaxabs} and @code{umaxabs} are declared in @file{inttypes.h};
  1038. the @code{fabs} functions are declared in @file{math.h};
  1039. the @code{cabs} functions are declared in @file{complex.h}.
  1040. @deftypefun int abs (int @var{number})
  1041. @deftypefunx {long int} labs (long int @var{number})
  1042. @deftypefunx {long long int} llabs (long long int @var{number})
  1043. @deftypefunx {unsigned int} uabs (int @var{number})
  1044. @deftypefunx {unsigned long int} ulabs (long int @var{number})
  1045. @deftypefunx {unsigned long long int} ullabs (long long int @var{number})
  1046. @deftypefunx intmax_t imaxabs (intmax_t @var{number})
  1047. @deftypefunx uintmax_t umaxabs (intmax_t @var{number})
  1048. @standards{ISO, stdlib.h}
  1049. @standardsx{imaxabs, ISO, inttypes.h}
  1050. @standardsx{umaxabs, ISO, inttypes.h}
  1051. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1052. These functions return the absolute value of @var{number}.
  1053. Most computers use a two's complement integer representation, in which
  1054. the absolute value of @code{INT_MIN} (the smallest possible @code{int})
  1055. cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined.
  1056. Using @code{uabs} avoids this.
  1057. @code{llabs} and @code{imaxdiv} are new to @w{ISO C99}.
  1058. @code{uabs}, @code{ulabs}, @code{ullabs} and @code{umaxabs} are new to @w{ISO C2Y}.
  1059. See @ref{Integers} for a description of the @code{intmax_t} type.
  1060. @end deftypefun
  1061. @deftypefun double fabs (double @var{number})
  1062. @deftypefunx float fabsf (float @var{number})
  1063. @deftypefunx {long double} fabsl (long double @var{number})
  1064. @deftypefunx _FloatN fabsfN (_Float@var{N} @var{number})
  1065. @deftypefunx _FloatNx fabsfNx (_Float@var{N}x @var{number})
  1066. @standards{ISO, math.h}
  1067. @standardsx{fabsfN, TS 18661-3:2015, math.h}
  1068. @standardsx{fabsfNx, TS 18661-3:2015, math.h}
  1069. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1070. This function returns the absolute value of the floating-point number
  1071. @var{number}.
  1072. @end deftypefun
  1073. @deftypefun double cabs (complex double @var{z})
  1074. @deftypefunx float cabsf (complex float @var{z})
  1075. @deftypefunx {long double} cabsl (complex long double @var{z})
  1076. @deftypefunx _FloatN cabsfN (complex _Float@var{N} @var{z})
  1077. @deftypefunx _FloatNx cabsfNx (complex _Float@var{N}x @var{z})
  1078. @standards{ISO, complex.h}
  1079. @standardsx{cabsfN, TS 18661-3:2015, complex.h}
  1080. @standardsx{cabsfNx, TS 18661-3:2015, complex.h}
  1081. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1082. These functions return the absolute value of the complex number @var{z}
  1083. (@pxref{Complex Numbers}). The absolute value of a complex number is:
  1084. @smallexample
  1085. sqrt (creal (@var{z}) * creal (@var{z}) + cimag (@var{z}) * cimag (@var{z}))
  1086. @end smallexample
  1087. This function should always be used instead of the direct formula
  1088. because it takes special care to avoid losing precision. It may also
  1089. take advantage of hardware support for this operation. See @code{hypot}
  1090. in @ref{Exponents and Logarithms}.
  1091. @end deftypefun
  1092. @node Normalization Functions
  1093. @subsection Normalization Functions
  1094. @cindex normalization functions (floating-point)
  1095. The functions described in this section are primarily provided as a way
  1096. to efficiently perform certain low-level manipulations on floating point
  1097. numbers that are represented internally using a binary radix;
  1098. see @ref{Floating Point Concepts}. These functions are required to
  1099. have equivalent behavior even if the representation does not use a radix
  1100. of 2, but of course they are unlikely to be particularly efficient in
  1101. those cases.
  1102. @pindex math.h
  1103. All these functions are declared in @file{math.h}.
  1104. @deftypefun double frexp (double @var{value}, int *@var{exponent})
  1105. @deftypefunx float frexpf (float @var{value}, int *@var{exponent})
  1106. @deftypefunx {long double} frexpl (long double @var{value}, int *@var{exponent})
  1107. @deftypefunx _FloatN frexpfN (_Float@var{N} @var{value}, int *@var{exponent})
  1108. @deftypefunx _FloatNx frexpfNx (_Float@var{N}x @var{value}, int *@var{exponent})
  1109. @standards{ISO, math.h}
  1110. @standardsx{frexpfN, TS 18661-3:2015, math.h}
  1111. @standardsx{frexpfNx, TS 18661-3:2015, math.h}
  1112. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1113. These functions are used to split the number @var{value}
  1114. into a normalized fraction and an exponent.
  1115. If the argument @var{value} is not zero, the return value is @var{value}
  1116. times a power of two, and its magnitude is always in the range 1/2
  1117. (inclusive) to 1 (exclusive). The corresponding exponent is stored in
  1118. @code{*@var{exponent}}; the return value multiplied by 2 raised to this
  1119. exponent equals the original number @var{value}.
  1120. For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and
  1121. stores @code{4} in @code{exponent}.
  1122. If @var{value} is zero, then the return value is zero and
  1123. zero is stored in @code{*@var{exponent}}.
  1124. @end deftypefun
  1125. @deftypefun double ldexp (double @var{value}, int @var{exponent})
  1126. @deftypefunx float ldexpf (float @var{value}, int @var{exponent})
  1127. @deftypefunx {long double} ldexpl (long double @var{value}, int @var{exponent})
  1128. @deftypefunx _FloatN ldexpfN (_Float@var{N} @var{value}, int @var{exponent})
  1129. @deftypefunx _FloatNx ldexpfNx (_Float@var{N}x @var{value}, int @var{exponent})
  1130. @standards{ISO, math.h}
  1131. @standardsx{ldexpfN, TS 18661-3:2015, math.h}
  1132. @standardsx{ldexpfNx, TS 18661-3:2015, math.h}
  1133. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1134. These functions return the result of multiplying the floating-point
  1135. number @var{value} by 2 raised to the power @var{exponent}. (It can
  1136. be used to reassemble floating-point numbers that were taken apart
  1137. by @code{frexp}.)
  1138. For example, @code{ldexp (0.8, 4)} returns @code{12.8}.
  1139. @end deftypefun
  1140. The following functions, which come from BSD, provide facilities
  1141. equivalent to those of @code{ldexp} and @code{frexp}. See also the
  1142. @w{ISO C} function @code{logb} which originally also appeared in BSD.
  1143. The @code{_Float@var{N}} and @code{_Float@var{N}} variants of the
  1144. following functions come from TS 18661-3:2015.
  1145. @deftypefun double scalb (double @var{value}, double @var{exponent})
  1146. @deftypefunx float scalbf (float @var{value}, float @var{exponent})
  1147. @deftypefunx {long double} scalbl (long double @var{value}, long double @var{exponent})
  1148. @standards{BSD, math.h}
  1149. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1150. The @code{scalb} function is the BSD name for @code{ldexp}.
  1151. @end deftypefun
  1152. @deftypefun double scalbn (double @var{x}, int @var{n})
  1153. @deftypefunx float scalbnf (float @var{x}, int @var{n})
  1154. @deftypefunx {long double} scalbnl (long double @var{x}, int @var{n})
  1155. @deftypefunx _FloatN scalbnfN (_Float@var{N} @var{x}, int @var{n})
  1156. @deftypefunx _FloatNx scalbnfNx (_Float@var{N}x @var{x}, int @var{n})
  1157. @standards{BSD, math.h}
  1158. @standardsx{scalbnfN, TS 18661-3:2015, math.h}
  1159. @standardsx{scalbnfNx, TS 18661-3:2015, math.h}
  1160. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1161. @code{scalbn} is identical to @code{scalb}, except that the exponent
  1162. @var{n} is an @code{int} instead of a floating-point number.
  1163. @end deftypefun
  1164. @deftypefun double scalbln (double @var{x}, long int @var{n})
  1165. @deftypefunx float scalblnf (float @var{x}, long int @var{n})
  1166. @deftypefunx {long double} scalblnl (long double @var{x}, long int @var{n})
  1167. @deftypefunx _FloatN scalblnfN (_Float@var{N} @var{x}, long int @var{n})
  1168. @deftypefunx _FloatNx scalblnfNx (_Float@var{N}x @var{x}, long int @var{n})
  1169. @standards{BSD, math.h}
  1170. @standardsx{scalblnfN, TS 18661-3:2015, math.h}
  1171. @standardsx{scalblnfNx, TS 18661-3:2015, math.h}
  1172. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1173. @code{scalbln} is identical to @code{scalb}, except that the exponent
  1174. @var{n} is a @code{long int} instead of a floating-point number.
  1175. @end deftypefun
  1176. @deftypefun double significand (double @var{x})
  1177. @deftypefunx float significandf (float @var{x})
  1178. @deftypefunx {long double} significandl (long double @var{x})
  1179. @standards{BSD, math.h}
  1180. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1181. @code{significand} returns the mantissa of @var{x} scaled to the range
  1182. @math{[1, @code{FLT_RADIX})}.
  1183. It is equivalent to @w{@code{scalb (@var{x}, (double) -ilogb (@var{x}))}}.
  1184. This function exists mainly for use in certain standardized tests
  1185. of @w{IEEE 754} conformance.
  1186. @end deftypefun
  1187. @node Rounding Functions
  1188. @subsection Rounding Functions
  1189. @cindex converting floats to integers
  1190. @pindex math.h
  1191. The functions listed here perform operations such as rounding and
  1192. truncation of floating-point values. Some of these functions convert
  1193. floating point numbers to integer values. They are all declared in
  1194. @file{math.h}.
  1195. You can also convert floating-point numbers to integers simply by
  1196. casting them to @code{int}. This discards the fractional part,
  1197. effectively rounding towards zero. However, this only works if the
  1198. result can actually be represented as an @code{int}---for very large
  1199. numbers, this is impossible. The functions listed here return the
  1200. result as a @code{double} instead to get around this problem.
  1201. The @code{fromfp} functions use the following macros, from TS
  1202. 18661-1:2014, to specify the direction of rounding. These correspond
  1203. to the rounding directions defined in IEEE 754-2008.
  1204. @vtable @code
  1205. @item FP_INT_UPWARD
  1206. @standards{ISO, math.h}
  1207. Round toward @math{+@infinity{}}.
  1208. @item FP_INT_DOWNWARD
  1209. @standards{ISO, math.h}
  1210. Round toward @math{-@infinity{}}.
  1211. @item FP_INT_TOWARDZERO
  1212. @standards{ISO, math.h}
  1213. Round toward zero.
  1214. @item FP_INT_TONEARESTFROMZERO
  1215. @standards{ISO, math.h}
  1216. Round to nearest, ties round away from zero.
  1217. @item FP_INT_TONEAREST
  1218. @standards{ISO, math.h}
  1219. Round to nearest, ties round to even.
  1220. @end vtable
  1221. @deftypefun double ceil (double @var{x})
  1222. @deftypefunx float ceilf (float @var{x})
  1223. @deftypefunx {long double} ceill (long double @var{x})
  1224. @deftypefunx _FloatN ceilfN (_Float@var{N} @var{x})
  1225. @deftypefunx _FloatNx ceilfNx (_Float@var{N}x @var{x})
  1226. @standards{ISO, math.h}
  1227. @standardsx{ceilfN, TS 18661-3:2015, math.h}
  1228. @standardsx{ceilfNx, TS 18661-3:2015, math.h}
  1229. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1230. These functions round @var{x} upwards to the nearest integer,
  1231. returning that value as a @code{double}. Thus, @code{ceil (1.5)}
  1232. is @code{2.0}.
  1233. @end deftypefun
  1234. @deftypefun double floor (double @var{x})
  1235. @deftypefunx float floorf (float @var{x})
  1236. @deftypefunx {long double} floorl (long double @var{x})
  1237. @deftypefunx _FloatN floorfN (_Float@var{N} @var{x})
  1238. @deftypefunx _FloatNx floorfNx (_Float@var{N}x @var{x})
  1239. @standards{ISO, math.h}
  1240. @standardsx{floorfN, TS 18661-3:2015, math.h}
  1241. @standardsx{floorfNx, TS 18661-3:2015, math.h}
  1242. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1243. These functions round @var{x} downwards to the nearest
  1244. integer, returning that value as a @code{double}. Thus, @code{floor
  1245. (1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}.
  1246. @end deftypefun
  1247. @deftypefun double trunc (double @var{x})
  1248. @deftypefunx float truncf (float @var{x})
  1249. @deftypefunx {long double} truncl (long double @var{x})
  1250. @deftypefunx _FloatN truncfN (_Float@var{N} @var{x})
  1251. @deftypefunx _FloatNx truncfNx (_Float@var{N}x @var{x})
  1252. @standards{ISO, math.h}
  1253. @standardsx{truncfN, TS 18661-3:2015, math.h}
  1254. @standardsx{truncfNx, TS 18661-3:2015, math.h}
  1255. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1256. The @code{trunc} functions round @var{x} towards zero to the nearest
  1257. integer (returned in floating-point format). Thus, @code{trunc (1.5)}
  1258. is @code{1.0} and @code{trunc (-1.5)} is @code{-1.0}.
  1259. @end deftypefun
  1260. @deftypefun double rint (double @var{x})
  1261. @deftypefunx float rintf (float @var{x})
  1262. @deftypefunx {long double} rintl (long double @var{x})
  1263. @deftypefunx _FloatN rintfN (_Float@var{N} @var{x})
  1264. @deftypefunx _FloatNx rintfNx (_Float@var{N}x @var{x})
  1265. @standards{ISO, math.h}
  1266. @standardsx{rintfN, TS 18661-3:2015, math.h}
  1267. @standardsx{rintfNx, TS 18661-3:2015, math.h}
  1268. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1269. These functions round @var{x} to an integer value according to the
  1270. current rounding mode. @xref{Floating Point Parameters}, for
  1271. information about the various rounding modes. The default
  1272. rounding mode is to round to the nearest integer; some machines
  1273. support other modes, but round-to-nearest is always used unless
  1274. you explicitly select another.
  1275. If @var{x} was not initially an integer, these functions raise the
  1276. inexact exception.
  1277. @end deftypefun
  1278. @deftypefun double nearbyint (double @var{x})
  1279. @deftypefunx float nearbyintf (float @var{x})
  1280. @deftypefunx {long double} nearbyintl (long double @var{x})
  1281. @deftypefunx _FloatN nearbyintfN (_Float@var{N} @var{x})
  1282. @deftypefunx _FloatNx nearbyintfNx (_Float@var{N}x @var{x})
  1283. @standards{ISO, math.h}
  1284. @standardsx{nearbyintfN, TS 18661-3:2015, math.h}
  1285. @standardsx{nearbyintfNx, TS 18661-3:2015, math.h}
  1286. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1287. These functions return the same value as the @code{rint} functions, but
  1288. do not raise the inexact exception if @var{x} is not an integer.
  1289. @end deftypefun
  1290. @deftypefun double round (double @var{x})
  1291. @deftypefunx float roundf (float @var{x})
  1292. @deftypefunx {long double} roundl (long double @var{x})
  1293. @deftypefunx _FloatN roundfN (_Float@var{N} @var{x})
  1294. @deftypefunx _FloatNx roundfNx (_Float@var{N}x @var{x})
  1295. @standards{ISO, math.h}
  1296. @standardsx{roundfN, TS 18661-3:2015, math.h}
  1297. @standardsx{roundfNx, TS 18661-3:2015, math.h}
  1298. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1299. These functions are similar to @code{rint}, but they round halfway
  1300. cases away from zero instead of to the nearest integer (or other
  1301. current rounding mode).
  1302. @end deftypefun
  1303. @deftypefun double roundeven (double @var{x})
  1304. @deftypefunx float roundevenf (float @var{x})
  1305. @deftypefunx {long double} roundevenl (long double @var{x})
  1306. @deftypefunx _FloatN roundevenfN (_Float@var{N} @var{x})
  1307. @deftypefunx _FloatNx roundevenfNx (_Float@var{N}x @var{x})
  1308. @standards{ISO, math.h}
  1309. @standardsx{roundevenfN, TS 18661-3:2015, math.h}
  1310. @standardsx{roundevenfNx, TS 18661-3:2015, math.h}
  1311. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1312. These functions, from TS 18661-1:2014 and TS 18661-3:2015, are similar
  1313. to @code{round}, but they round halfway cases to even instead of away
  1314. from zero.
  1315. @end deftypefun
  1316. @deftypefun {long int} lrint (double @var{x})
  1317. @deftypefunx {long int} lrintf (float @var{x})
  1318. @deftypefunx {long int} lrintl (long double @var{x})
  1319. @deftypefunx {long int} lrintfN (_Float@var{N} @var{x})
  1320. @deftypefunx {long int} lrintfNx (_Float@var{N}x @var{x})
  1321. @standards{ISO, math.h}
  1322. @standardsx{lrintfN, TS 18661-3:2015, math.h}
  1323. @standardsx{lrintfNx, TS 18661-3:2015, math.h}
  1324. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1325. These functions are just like @code{rint}, but they return a
  1326. @code{long int} instead of a floating-point number.
  1327. @end deftypefun
  1328. @deftypefun {long long int} llrint (double @var{x})
  1329. @deftypefunx {long long int} llrintf (float @var{x})
  1330. @deftypefunx {long long int} llrintl (long double @var{x})
  1331. @deftypefunx {long long int} llrintfN (_Float@var{N} @var{x})
  1332. @deftypefunx {long long int} llrintfNx (_Float@var{N}x @var{x})
  1333. @standards{ISO, math.h}
  1334. @standardsx{llrintfN, TS 18661-3:2015, math.h}
  1335. @standardsx{llrintfNx, TS 18661-3:2015, math.h}
  1336. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1337. These functions are just like @code{rint}, but they return a
  1338. @code{long long int} instead of a floating-point number.
  1339. @end deftypefun
  1340. @deftypefun {long int} lround (double @var{x})
  1341. @deftypefunx {long int} lroundf (float @var{x})
  1342. @deftypefunx {long int} lroundl (long double @var{x})
  1343. @deftypefunx {long int} lroundfN (_Float@var{N} @var{x})
  1344. @deftypefunx {long int} lroundfNx (_Float@var{N}x @var{x})
  1345. @standards{ISO, math.h}
  1346. @standardsx{lroundfN, TS 18661-3:2015, math.h}
  1347. @standardsx{lroundfNx, TS 18661-3:2015, math.h}
  1348. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1349. These functions are just like @code{round}, but they return a
  1350. @code{long int} instead of a floating-point number.
  1351. @end deftypefun
  1352. @deftypefun {long long int} llround (double @var{x})
  1353. @deftypefunx {long long int} llroundf (float @var{x})
  1354. @deftypefunx {long long int} llroundl (long double @var{x})
  1355. @deftypefunx {long long int} llroundfN (_Float@var{N} @var{x})
  1356. @deftypefunx {long long int} llroundfNx (_Float@var{N}x @var{x})
  1357. @standards{ISO, math.h}
  1358. @standardsx{llroundfN, TS 18661-3:2015, math.h}
  1359. @standardsx{llroundfNx, TS 18661-3:2015, math.h}
  1360. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1361. These functions are just like @code{round}, but they return a
  1362. @code{long long int} instead of a floating-point number.
  1363. @end deftypefun
  1364. @deftypefun double fromfp (double @var{x}, int @var{round}, unsigned int @var{width})
  1365. @deftypefunx float fromfpf (float @var{x}, int @var{round}, unsigned int @var{width})
  1366. @deftypefunx {long double} fromfpl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1367. @deftypefunx _FloatN fromfpfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1368. @deftypefunx _FloatNx fromfpfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1369. @deftypefunx double ufromfp (double @var{x}, int @var{round}, unsigned int @var{width})
  1370. @deftypefunx float ufromfpf (float @var{x}, int @var{round}, unsigned int @var{width})
  1371. @deftypefunx {long double} ufromfpl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1372. @deftypefunx _FloatN ufromfpfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1373. @deftypefunx _FloatNx ufromfpfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1374. @deftypefunx double fromfpx (double @var{x}, int @var{round}, unsigned int @var{width})
  1375. @deftypefunx float fromfpxf (float @var{x}, int @var{round}, unsigned int @var{width})
  1376. @deftypefunx {long double} fromfpxl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1377. @deftypefunx _FloatN fromfpxfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1378. @deftypefunx _FloatNx fromfpxfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1379. @deftypefunx double ufromfpx (double @var{x}, int @var{round}, unsigned int @var{width})
  1380. @deftypefunx float ufromfpxf (float @var{x}, int @var{round}, unsigned int @var{width})
  1381. @deftypefunx {long double} ufromfpxl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1382. @deftypefunx _FloatN ufromfpxfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1383. @deftypefunx _FloatNx ufromfpxfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1384. @standards{ISO, math.h}
  1385. @standardsx{fromfpfN, TS 18661-3:2015, math.h}
  1386. @standardsx{fromfpfNx, TS 18661-3:2015, math.h}
  1387. @standardsx{ufromfpfN, TS 18661-3:2015, math.h}
  1388. @standardsx{ufromfpfNx, TS 18661-3:2015, math.h}
  1389. @standardsx{fromfpxfN, TS 18661-3:2015, math.h}
  1390. @standardsx{fromfpxfNx, TS 18661-3:2015, math.h}
  1391. @standardsx{ufromfpxfN, TS 18661-3:2015, math.h}
  1392. @standardsx{ufromfpxfNx, TS 18661-3:2015, math.h}
  1393. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1394. These functions, from TS 18661-1:2014 and TS 18661-3:2015, convert a
  1395. floating-point number to an integer according to the rounding
  1396. direction @var{round} (one of the @code{FP_INT_*} macros). If the
  1397. integer is outside the range of a two's complement signed or unsigned
  1398. (depending on the return type of the function) integer of width
  1399. @var{width} bits, or if @var{x} is infinite or NaN, or if @var{width}
  1400. is zero, a domain error occurs and a NaN is returned. The functions
  1401. with an @samp{x} in their names raise the inexact exception when a
  1402. domain error does not occur and the argument is not an integer; the
  1403. other functions do not raise the inexact exception.
  1404. Following ISO C23, in @theglibc{} these functions return their result
  1405. in the floating-point argument type. In TS 18661-1:2014 and TS
  1406. 18661-3:2015, they were originally defined to return @code{intmax_t}
  1407. and @code{uintmax_t}.
  1408. @end deftypefun
  1409. @deftypefun double modf (double @var{value}, double *@var{integer-part})
  1410. @deftypefunx float modff (float @var{value}, float *@var{integer-part})
  1411. @deftypefunx {long double} modfl (long double @var{value}, long double *@var{integer-part})
  1412. @deftypefunx _FloatN modffN (_Float@var{N} @var{value}, _Float@var{N} *@var{integer-part})
  1413. @deftypefunx _FloatNx modffNx (_Float@var{N}x @var{value}, _Float@var{N}x *@var{integer-part})
  1414. @standards{ISO, math.h}
  1415. @standardsx{modffN, TS 18661-3:2015, math.h}
  1416. @standardsx{modffNx, TS 18661-3:2015, math.h}
  1417. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1418. These functions break the argument @var{value} into an integer part and a
  1419. fractional part (between @code{-1} and @code{1}, exclusive). Their sum
  1420. equals @var{value}. Each of the parts has the same sign as @var{value},
  1421. and the integer part is always rounded toward zero.
  1422. @code{modf} stores the integer part in @code{*@var{integer-part}}, and
  1423. returns the fractional part. For example, @code{modf (2.5, &intpart)}
  1424. returns @code{0.5} and stores @code{2.0} into @code{intpart}.
  1425. @end deftypefun
  1426. @node Remainder Functions
  1427. @subsection Remainder Functions
  1428. The functions in this section compute the remainder on division of two
  1429. floating-point numbers. Each is a little different; pick the one that
  1430. suits your problem.
  1431. @deftypefun double fmod (double @var{numerator}, double @var{denominator})
  1432. @deftypefunx float fmodf (float @var{numerator}, float @var{denominator})
  1433. @deftypefunx {long double} fmodl (long double @var{numerator}, long double @var{denominator})
  1434. @deftypefunx _FloatN fmodfN (_Float@var{N} @var{numerator}, _Float@var{N} @var{denominator})
  1435. @deftypefunx _FloatNx fmodfNx (_Float@var{N}x @var{numerator}, _Float@var{N}x @var{denominator})
  1436. @standards{ISO, math.h}
  1437. @standardsx{fmodfN, TS 18661-3:2015, math.h}
  1438. @standardsx{fmodfNx, TS 18661-3:2015, math.h}
  1439. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1440. These functions compute the remainder from the division of
  1441. @var{numerator} by @var{denominator}. Specifically, the return value is
  1442. @code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n}
  1443. is the quotient of @var{numerator} divided by @var{denominator}, rounded
  1444. towards zero to an integer. Thus, @w{@code{fmod (6.5, 2.3)}} returns
  1445. @code{1.9}, which is @code{6.5} minus @code{4.6}.
  1446. The result has the same sign as the @var{numerator} and has magnitude
  1447. less than the magnitude of the @var{denominator}.
  1448. If @var{denominator} is zero, @code{fmod} signals a domain error.
  1449. @end deftypefun
  1450. @deftypefun double remainder (double @var{numerator}, double @var{denominator})
  1451. @deftypefunx float remainderf (float @var{numerator}, float @var{denominator})
  1452. @deftypefunx {long double} remainderl (long double @var{numerator}, long double @var{denominator})
  1453. @deftypefunx _FloatN remainderfN (_Float@var{N} @var{numerator}, _Float@var{N} @var{denominator})
  1454. @deftypefunx _FloatNx remainderfNx (_Float@var{N}x @var{numerator}, _Float@var{N}x @var{denominator})
  1455. @standards{ISO, math.h}
  1456. @standardsx{remainderfN, TS 18661-3:2015, math.h}
  1457. @standardsx{remainderfNx, TS 18661-3:2015, math.h}
  1458. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1459. These functions are like @code{fmod} except that they round the
  1460. internal quotient @var{n} to the nearest integer instead of towards zero
  1461. to an integer. For example, @code{remainder (6.5, 2.3)} returns
  1462. @code{-0.4}, which is @code{6.5} minus @code{6.9}.
  1463. The absolute value of the result is less than or equal to half the
  1464. absolute value of the @var{denominator}. The difference between
  1465. @code{fmod (@var{numerator}, @var{denominator})} and @code{remainder
  1466. (@var{numerator}, @var{denominator})} is always either
  1467. @var{denominator}, minus @var{denominator}, or zero.
  1468. If @var{denominator} is zero, @code{remainder} signals a domain error.
  1469. @end deftypefun
  1470. @deftypefun double drem (double @var{numerator}, double @var{denominator})
  1471. @deftypefunx float dremf (float @var{numerator}, float @var{denominator})
  1472. @deftypefunx {long double} dreml (long double @var{numerator}, long double @var{denominator})
  1473. @standards{BSD, math.h}
  1474. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1475. This function is another name for @code{remainder}.
  1476. @end deftypefun
  1477. @node FP Bit Twiddling
  1478. @subsection Setting and modifying single bits of FP values
  1479. @cindex FP arithmetic
  1480. There are some operations that are too complicated or expensive to
  1481. perform by hand on floating-point numbers. @w{ISO C99} defines
  1482. functions to do these operations, which mostly involve changing single
  1483. bits.
  1484. @deftypefun double copysign (double @var{x}, double @var{y})
  1485. @deftypefunx float copysignf (float @var{x}, float @var{y})
  1486. @deftypefunx {long double} copysignl (long double @var{x}, long double @var{y})
  1487. @deftypefunx _FloatN copysignfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1488. @deftypefunx _FloatNx copysignfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1489. @standards{ISO, math.h}
  1490. @standardsx{copysignfN, TS 18661-3:2015, math.h}
  1491. @standardsx{copysignfNx, TS 18661-3:2015, math.h}
  1492. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1493. These functions return @var{x} but with the sign of @var{y}. They work
  1494. even if @var{x} or @var{y} are NaN or zero. Both of these can carry a
  1495. sign (although not all implementations support it) and this is one of
  1496. the few operations that can tell the difference.
  1497. @code{copysign} never raises an exception.
  1498. @c except signalling NaNs
  1499. This function is defined in @w{IEC 559} (and the appendix with
  1500. recommended functions in @w{IEEE 754}/@w{IEEE 854}).
  1501. @end deftypefun
  1502. @deftypefun int signbit (@emph{float-type} @var{x})
  1503. @standards{ISO, math.h}
  1504. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1505. @code{signbit} is a generic macro which can work on all floating-point
  1506. types. It returns a nonzero value if the value of @var{x} has its sign
  1507. bit set.
  1508. This is not the same as @code{x < 0.0}, because @w{IEEE 754} floating
  1509. point allows zero to be signed. The comparison @code{-0.0 < 0.0} is
  1510. false, but @code{signbit (-0.0)} will return a nonzero value.
  1511. @end deftypefun
  1512. @deftypefun double nextafter (double @var{x}, double @var{y})
  1513. @deftypefunx float nextafterf (float @var{x}, float @var{y})
  1514. @deftypefunx {long double} nextafterl (long double @var{x}, long double @var{y})
  1515. @deftypefunx _FloatN nextafterfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1516. @deftypefunx _FloatNx nextafterfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1517. @standards{ISO, math.h}
  1518. @standardsx{nextafterfN, TS 18661-3:2015, math.h}
  1519. @standardsx{nextafterfNx, TS 18661-3:2015, math.h}
  1520. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1521. The @code{nextafter} function returns the next representable neighbor of
  1522. @var{x} in the direction towards @var{y}. The size of the step between
  1523. @var{x} and the result depends on the type of the result. If
  1524. @math{@var{x} = @var{y}} the function simply returns @var{y}. If either
  1525. value is @code{NaN}, @code{NaN} is returned. Otherwise
  1526. a value corresponding to the value of the least significant bit in the
  1527. mantissa is added or subtracted, depending on the direction.
  1528. @code{nextafter} will signal overflow or underflow if the result goes
  1529. outside of the range of normalized numbers.
  1530. This function is defined in @w{IEC 559} (and the appendix with
  1531. recommended functions in @w{IEEE 754}/@w{IEEE 854}).
  1532. @end deftypefun
  1533. @deftypefun double nexttoward (double @var{x}, long double @var{y})
  1534. @deftypefunx float nexttowardf (float @var{x}, long double @var{y})
  1535. @deftypefunx {long double} nexttowardl (long double @var{x}, long double @var{y})
  1536. @standards{ISO, math.h}
  1537. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1538. These functions are identical to the corresponding versions of
  1539. @code{nextafter} except that their second argument is a @code{long
  1540. double}.
  1541. @end deftypefun
  1542. @deftypefun double nextup (double @var{x})
  1543. @deftypefunx float nextupf (float @var{x})
  1544. @deftypefunx {long double} nextupl (long double @var{x})
  1545. @deftypefunx _FloatN nextupfN (_Float@var{N} @var{x})
  1546. @deftypefunx _FloatNx nextupfNx (_Float@var{N}x @var{x})
  1547. @standards{ISO, math.h}
  1548. @standardsx{nextupfN, TS 18661-3:2015, math.h}
  1549. @standardsx{nextupfNx, TS 18661-3:2015, math.h}
  1550. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1551. The @code{nextup} function returns the next representable neighbor of @var{x}
  1552. in the direction of positive infinity. If @var{x} is the smallest negative
  1553. subnormal number in the type of @var{x} the function returns @code{-0}. If
  1554. @math{@var{x} = @code{0}} the function returns the smallest positive subnormal
  1555. number in the type of @var{x}. If @var{x} is NaN, NaN is returned.
  1556. If @var{x} is @math{+@infinity{}}, @math{+@infinity{}} is returned.
  1557. @code{nextup} is from TS 18661-1:2014 and TS 18661-3:2015.
  1558. @code{nextup} never raises an exception except for signaling NaNs.
  1559. @end deftypefun
  1560. @deftypefun double nextdown (double @var{x})
  1561. @deftypefunx float nextdownf (float @var{x})
  1562. @deftypefunx {long double} nextdownl (long double @var{x})
  1563. @deftypefunx _FloatN nextdownfN (_Float@var{N} @var{x})
  1564. @deftypefunx _FloatNx nextdownfNx (_Float@var{N}x @var{x})
  1565. @standards{ISO, math.h}
  1566. @standardsx{nextdownfN, TS 18661-3:2015, math.h}
  1567. @standardsx{nextdownfNx, TS 18661-3:2015, math.h}
  1568. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1569. The @code{nextdown} function returns the next representable neighbor of @var{x}
  1570. in the direction of negative infinity. If @var{x} is the smallest positive
  1571. subnormal number in the type of @var{x} the function returns @code{+0}. If
  1572. @math{@var{x} = @code{0}} the function returns the smallest negative subnormal
  1573. number in the type of @var{x}. If @var{x} is NaN, NaN is returned.
  1574. If @var{x} is @math{-@infinity{}}, @math{-@infinity{}} is returned.
  1575. @code{nextdown} is from TS 18661-1:2014 and TS 18661-3:2015.
  1576. @code{nextdown} never raises an exception except for signaling NaNs.
  1577. @end deftypefun
  1578. @cindex NaN
  1579. @deftypefun double nan (const char *@var{tagp})
  1580. @deftypefunx float nanf (const char *@var{tagp})
  1581. @deftypefunx {long double} nanl (const char *@var{tagp})
  1582. @deftypefunx _FloatN nanfN (const char *@var{tagp})
  1583. @deftypefunx _FloatNx nanfNx (const char *@var{tagp})
  1584. @standards{ISO, math.h}
  1585. @standardsx{nanfN, TS 18661-3:2015, math.h}
  1586. @standardsx{nanfNx, TS 18661-3:2015, math.h}
  1587. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  1588. @c The unsafe-but-ruled-safe locale use comes from strtod.
  1589. The @code{nan} function returns a representation of NaN, provided that
  1590. NaN is supported by the target platform.
  1591. @code{nan ("@var{n-char-sequence}")} is equivalent to
  1592. @code{strtod ("NAN(@var{n-char-sequence})")}.
  1593. The argument @var{tagp} is used in an unspecified manner. On @w{IEEE
  1594. 754} systems, there are many representations of NaN, and @var{tagp}
  1595. selects one. On other systems it may do nothing.
  1596. @end deftypefun
  1597. @deftypefun int canonicalize (double *@var{cx}, const double *@var{x})
  1598. @deftypefunx int canonicalizef (float *@var{cx}, const float *@var{x})
  1599. @deftypefunx int canonicalizel (long double *@var{cx}, const long double *@var{x})
  1600. @deftypefunx int canonicalizefN (_Float@var{N} *@var{cx}, const _Float@var{N} *@var{x})
  1601. @deftypefunx int canonicalizefNx (_Float@var{N}x *@var{cx}, const _Float@var{N}x *@var{x})
  1602. @standards{ISO, math.h}
  1603. @standardsx{canonicalizefN, TS 18661-3:2015, math.h}
  1604. @standardsx{canonicalizefNx, TS 18661-3:2015, math.h}
  1605. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1606. In some floating-point formats, some values have canonical (preferred)
  1607. and noncanonical encodings (for IEEE interchange binary formats, all
  1608. encodings are canonical). These functions, defined by TS
  1609. 18661-1:2014 and TS 18661-3:2015, attempt to produce a canonical version
  1610. of the floating-point value pointed to by @var{x}; if that value is a
  1611. signaling NaN, they raise the invalid exception and produce a quiet
  1612. NaN. If a canonical value is produced, it is stored in the object
  1613. pointed to by @var{cx}, and these functions return zero. Otherwise
  1614. (if a canonical value could not be produced because the object pointed
  1615. to by @var{x} is not a valid representation of any floating-point
  1616. value), the object pointed to by @var{cx} is unchanged and a nonzero
  1617. value is returned.
  1618. Note that some formats have multiple encodings of a value which are
  1619. all equally canonical; when such an encoding is used as an input to
  1620. this function, any such encoding of the same value (or of the
  1621. corresponding quiet NaN, if that value is a signaling NaN) may be
  1622. produced as output.
  1623. @end deftypefun
  1624. @deftypefun double getpayload (const double *@var{x})
  1625. @deftypefunx float getpayloadf (const float *@var{x})
  1626. @deftypefunx {long double} getpayloadl (const long double *@var{x})
  1627. @deftypefunx _FloatN getpayloadfN (const _Float@var{N} *@var{x})
  1628. @deftypefunx _FloatNx getpayloadfNx (const _Float@var{N}x *@var{x})
  1629. @standards{ISO, math.h}
  1630. @standardsx{getpayloadfN, TS 18661-3:2015, math.h}
  1631. @standardsx{getpayloadfNx, TS 18661-3:2015, math.h}
  1632. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1633. IEEE 754 defines the @dfn{payload} of a NaN to be an integer value
  1634. encoded in the representation of the NaN. Payloads are typically
  1635. propagated from NaN inputs to the result of a floating-point
  1636. operation. These functions, defined by TS 18661-1:2014 and TS
  1637. 18661-3:2015, return the payload of the NaN pointed to by @var{x}
  1638. (returned as a positive integer, or positive zero, represented as a
  1639. floating-point number); if @var{x} is not a NaN, they return
  1640. @minus{}1. They raise no floating-point exceptions even for signaling
  1641. NaNs. (The return value of @minus{}1 for an argument that is not a
  1642. NaN is specified in C23; the value was unspecified in TS 18661.)
  1643. @end deftypefun
  1644. @deftypefun int setpayload (double *@var{x}, double @var{payload})
  1645. @deftypefunx int setpayloadf (float *@var{x}, float @var{payload})
  1646. @deftypefunx int setpayloadl (long double *@var{x}, long double @var{payload})
  1647. @deftypefunx int setpayloadfN (_Float@var{N} *@var{x}, _Float@var{N} @var{payload})
  1648. @deftypefunx int setpayloadfNx (_Float@var{N}x *@var{x}, _Float@var{N}x @var{payload})
  1649. @standards{ISO, math.h}
  1650. @standardsx{setpayloadfN, TS 18661-3:2015, math.h}
  1651. @standardsx{setpayloadfNx, TS 18661-3:2015, math.h}
  1652. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1653. These functions, defined by TS 18661-1:2014 and TS 18661-3:2015, set the
  1654. object pointed to by @var{x} to a quiet NaN with payload @var{payload}
  1655. and a zero sign bit and return zero. If @var{payload} is not a
  1656. positive-signed integer that is a valid payload for a quiet NaN of the
  1657. given type, the object pointed to by @var{x} is set to positive zero and
  1658. a nonzero value is returned. They raise no floating-point exceptions.
  1659. @end deftypefun
  1660. @deftypefun int setpayloadsig (double *@var{x}, double @var{payload})
  1661. @deftypefunx int setpayloadsigf (float *@var{x}, float @var{payload})
  1662. @deftypefunx int setpayloadsigl (long double *@var{x}, long double @var{payload})
  1663. @deftypefunx int setpayloadsigfN (_Float@var{N} *@var{x}, _Float@var{N} @var{payload})
  1664. @deftypefunx int setpayloadsigfNx (_Float@var{N}x *@var{x}, _Float@var{N}x @var{payload})
  1665. @standards{ISO, math.h}
  1666. @standardsx{setpayloadsigfN, TS 18661-3:2015, math.h}
  1667. @standardsx{setpayloadsigfNx, TS 18661-3:2015, math.h}
  1668. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1669. These functions, defined by TS 18661-1:2014 and TS 18661-3:2015, set the
  1670. object pointed to by @var{x} to a signaling NaN with payload
  1671. @var{payload} and a zero sign bit and return zero. If @var{payload} is
  1672. not a positive-signed integer that is a valid payload for a signaling
  1673. NaN of the given type, the object pointed to by @var{x} is set to
  1674. positive zero and a nonzero value is returned. They raise no
  1675. floating-point exceptions.
  1676. @end deftypefun
  1677. @node FP Comparison Functions
  1678. @subsection Floating-Point Comparison Functions
  1679. @cindex unordered comparison
  1680. The standard C comparison operators provoke exceptions when one or other
  1681. of the operands is NaN. For example,
  1682. @smallexample
  1683. int v = a < 1.0;
  1684. @end smallexample
  1685. @noindent
  1686. will raise an exception if @var{a} is NaN. (This does @emph{not}
  1687. happen with @code{==} and @code{!=}; those merely return false and true,
  1688. respectively, when NaN is examined.) Frequently this exception is
  1689. undesirable. @w{ISO C99} therefore defines comparison functions that
  1690. do not raise exceptions when NaN is examined. All of the functions are
  1691. implemented as macros which allow their arguments to be of any
  1692. floating-point type. The macros are guaranteed to evaluate their
  1693. arguments only once. TS 18661-1:2014 adds such a macro for an
  1694. equality comparison that @emph{does} raise an exception for a NaN
  1695. argument; it also adds functions that provide a total ordering on all
  1696. floating-point values, including NaNs, without raising any exceptions
  1697. even for signaling NaNs.
  1698. @deftypefn Macro int isgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1699. @standards{ISO, math.h}
  1700. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1701. This macro determines whether the argument @var{x} is greater than
  1702. @var{y}. It is equivalent to @code{(@var{x}) > (@var{y})}, but no
  1703. exception is raised if @var{x} or @var{y} are NaN.
  1704. @end deftypefn
  1705. @deftypefn Macro int isgreaterequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1706. @standards{ISO, math.h}
  1707. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1708. This macro determines whether the argument @var{x} is greater than or
  1709. equal to @var{y}. It is equivalent to @code{(@var{x}) >= (@var{y})}, but no
  1710. exception is raised if @var{x} or @var{y} are NaN.
  1711. @end deftypefn
  1712. @deftypefn Macro int isless (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1713. @standards{ISO, math.h}
  1714. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1715. This macro determines whether the argument @var{x} is less than @var{y}.
  1716. It is equivalent to @code{(@var{x}) < (@var{y})}, but no exception is
  1717. raised if @var{x} or @var{y} are NaN.
  1718. @end deftypefn
  1719. @deftypefn Macro int islessequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1720. @standards{ISO, math.h}
  1721. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1722. This macro determines whether the argument @var{x} is less than or equal
  1723. to @var{y}. It is equivalent to @code{(@var{x}) <= (@var{y})}, but no
  1724. exception is raised if @var{x} or @var{y} are NaN.
  1725. @end deftypefn
  1726. @deftypefn Macro int islessgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1727. @standards{ISO, math.h}
  1728. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1729. This macro determines whether the argument @var{x} is less or greater
  1730. than @var{y}. It is equivalent to @code{(@var{x}) < (@var{y}) ||
  1731. (@var{x}) > (@var{y})} (although it only evaluates @var{x} and @var{y}
  1732. once), but no exception is raised if @var{x} or @var{y} are NaN.
  1733. This macro is not equivalent to @code{@var{x} != @var{y}}, because that
  1734. expression is true if @var{x} or @var{y} are NaN.
  1735. @end deftypefn
  1736. @deftypefn Macro int isunordered (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1737. @standards{ISO, math.h}
  1738. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1739. This macro determines whether its arguments are unordered. In other
  1740. words, it is true if @var{x} or @var{y} are NaN, and false otherwise.
  1741. @end deftypefn
  1742. @deftypefn Macro int iseqsig (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1743. @standards{ISO, math.h}
  1744. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1745. This macro determines whether its arguments are equal. It is
  1746. equivalent to @code{(@var{x}) == (@var{y})}, but it raises the invalid
  1747. exception and sets @code{errno} to @code{EDOM} if either argument is a
  1748. NaN.
  1749. @end deftypefn
  1750. @deftypefun int totalorder (const double *@var{x}, const double *@var{y})
  1751. @deftypefunx int totalorderf (const float *@var{x}, const float *@var{y})
  1752. @deftypefunx int totalorderl (const long double *@var{x}, const long double *@var{y})
  1753. @deftypefunx int totalorderfN (const _Float@var{N} *@var{x}, const _Float@var{N} *@var{y})
  1754. @deftypefunx int totalorderfNx (const _Float@var{N}x *@var{x}, const _Float@var{N}x *@var{y})
  1755. @standards{TS 18661-1:2014, math.h}
  1756. @standardsx{totalorderfN, TS 18661-3:2015, math.h}
  1757. @standardsx{totalorderfNx, TS 18661-3:2015, math.h}
  1758. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1759. These functions determine whether the total order relationship,
  1760. defined in IEEE 754-2008, is true for @code{*@var{x}} and
  1761. @code{*@var{y}}, returning
  1762. nonzero if it is true and zero if it is false. No exceptions are
  1763. raised even for signaling NaNs. The relationship is true if they are
  1764. the same floating-point value (including sign for zero and NaNs, and
  1765. payload for NaNs), or if @code{*@var{x}} comes before @code{*@var{y}}
  1766. in the following
  1767. order: negative quiet NaNs, in order of decreasing payload; negative
  1768. signaling NaNs, in order of decreasing payload; negative infinity;
  1769. finite numbers, in ascending order, with negative zero before positive
  1770. zero; positive infinity; positive signaling NaNs, in order of
  1771. increasing payload; positive quiet NaNs, in order of increasing
  1772. payload.
  1773. @end deftypefun
  1774. @deftypefun int totalordermag (const double *@var{x}, const double *@var{y})
  1775. @deftypefunx int totalordermagf (const float *@var{x}, const float *@var{y})
  1776. @deftypefunx int totalordermagl (const long double *@var{x}, const long double *@var{y})
  1777. @deftypefunx int totalordermagfN (const _Float@var{N} *@var{x}, const _Float@var{N} *@var{y})
  1778. @deftypefunx int totalordermagfNx (const _Float@var{N}x *@var{x}, const _Float@var{N}x *@var{y})
  1779. @standards{TS 18661-1:2014, math.h}
  1780. @standardsx{totalordermagfN, TS 18661-3:2015, math.h}
  1781. @standardsx{totalordermagfNx, TS 18661-3:2015, math.h}
  1782. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1783. These functions determine whether the total order relationship,
  1784. defined in IEEE 754-2008, is true for the absolute values of @code{*@var{x}}
  1785. and @code{*@var{y}}, returning nonzero if it is true and zero if it is false.
  1786. No exceptions are raised even for signaling NaNs.
  1787. @end deftypefun
  1788. Not all machines provide hardware support for these operations. On
  1789. machines that don't, the macros can be very slow. Therefore, you should
  1790. not use these functions when NaN is not a concern.
  1791. @strong{NB:} There are no macros @code{isequal} or @code{isunequal}.
  1792. They are unnecessary, because the @code{==} and @code{!=} operators do
  1793. @emph{not} throw an exception if one or both of the operands are NaN.
  1794. @node Misc FP Arithmetic
  1795. @subsection Miscellaneous FP arithmetic functions
  1796. @cindex minimum
  1797. @cindex maximum
  1798. @cindex positive difference
  1799. @cindex multiply-add
  1800. The functions in this section perform miscellaneous but common
  1801. operations that are awkward to express with C operators. On some
  1802. processors these functions can use special machine instructions to
  1803. perform these operations faster than the equivalent C code.
  1804. @deftypefun double fmin (double @var{x}, double @var{y})
  1805. @deftypefunx float fminf (float @var{x}, float @var{y})
  1806. @deftypefunx {long double} fminl (long double @var{x}, long double @var{y})
  1807. @deftypefunx _FloatN fminfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1808. @deftypefunx _FloatNx fminfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1809. @standards{ISO, math.h}
  1810. @standardsx{fminfN, TS 18661-3:2015, math.h}
  1811. @standardsx{fminfNx, TS 18661-3:2015, math.h}
  1812. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1813. The @code{fmin} function returns the lesser of the two values @var{x}
  1814. and @var{y}. It is similar to the expression
  1815. @smallexample
  1816. ((x) < (y) ? (x) : (y))
  1817. @end smallexample
  1818. except that @var{x} and @var{y} are only evaluated once.
  1819. If an argument is a quiet NaN, the other argument is returned. If both arguments
  1820. are NaN, or either is a signaling NaN, NaN is returned.
  1821. @end deftypefun
  1822. @deftypefun double fmax (double @var{x}, double @var{y})
  1823. @deftypefunx float fmaxf (float @var{x}, float @var{y})
  1824. @deftypefunx {long double} fmaxl (long double @var{x}, long double @var{y})
  1825. @deftypefunx _FloatN fmaxfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1826. @deftypefunx _FloatNx fmaxfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1827. @standards{ISO, math.h}
  1828. @standardsx{fmaxfN, TS 18661-3:2015, math.h}
  1829. @standardsx{fmaxfNx, TS 18661-3:2015, math.h}
  1830. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1831. The @code{fmax} function returns the greater of the two values @var{x}
  1832. and @var{y}.
  1833. If an argument is a quiet NaN, the other argument is returned. If both arguments
  1834. are NaN, or either is a signaling NaN, NaN is returned.
  1835. @end deftypefun
  1836. @deftypefun double fminimum (double @var{x}, double @var{y})
  1837. @deftypefunx float fminimumf (float @var{x}, float @var{y})
  1838. @deftypefunx {long double} fminimuml (long double @var{x}, long double @var{y})
  1839. @deftypefunx _FloatN fminimumfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1840. @deftypefunx _FloatNx fminimumfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1841. @standards{C23, math.h}
  1842. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1843. The @code{fminimum} function returns the lesser of the two values @var{x}
  1844. and @var{y}. Unlike @code{fmin}, if either argument is a NaN, NaN is returned.
  1845. Positive zero is treated as greater than negative zero.
  1846. @end deftypefun
  1847. @deftypefun double fmaximum (double @var{x}, double @var{y})
  1848. @deftypefunx float fmaximumf (float @var{x}, float @var{y})
  1849. @deftypefunx {long double} fmaximuml (long double @var{x}, long double @var{y})
  1850. @deftypefunx _FloatN fmaximumfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1851. @deftypefunx _FloatNx fmaximumfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1852. @standards{C23, math.h}
  1853. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1854. The @code{fmaximum} function returns the greater of the two values @var{x}
  1855. and @var{y}. Unlike @code{fmax}, if either argument is a NaN, NaN is returned.
  1856. Positive zero is treated as greater than negative zero.
  1857. @end deftypefun
  1858. @deftypefun double fminimum_num (double @var{x}, double @var{y})
  1859. @deftypefunx float fminimum_numf (float @var{x}, float @var{y})
  1860. @deftypefunx {long double} fminimum_numl (long double @var{x}, long double @var{y})
  1861. @deftypefunx _FloatN fminimum_numfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1862. @deftypefunx _FloatNx fminimum_numfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1863. @standards{C23, math.h}
  1864. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1865. The @code{fminimum_num} function returns the lesser of the two values
  1866. @var{x} and @var{y}. If one argument is a number and the other is a
  1867. NaN, even a signaling NaN, the number is returned. Positive zero is
  1868. treated as greater than negative zero.
  1869. @end deftypefun
  1870. @deftypefun double fmaximum_num (double @var{x}, double @var{y})
  1871. @deftypefunx float fmaximum_numf (float @var{x}, float @var{y})
  1872. @deftypefunx {long double} fmaximum_numl (long double @var{x}, long double @var{y})
  1873. @deftypefunx _FloatN fmaximum_numfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1874. @deftypefunx _FloatNx fmaximum_numfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1875. @standards{C23, math.h}
  1876. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1877. The @code{fmaximum_num} function returns the greater of the two values
  1878. @var{x} and @var{y}. If one argument is a number and the other is a
  1879. NaN, even a signaling NaN, the number is returned. Positive zero is
  1880. treated as greater than negative zero.
  1881. @end deftypefun
  1882. @deftypefun double fminmag (double @var{x}, double @var{y})
  1883. @deftypefunx float fminmagf (float @var{x}, float @var{y})
  1884. @deftypefunx {long double} fminmagl (long double @var{x}, long double @var{y})
  1885. @deftypefunx _FloatN fminmagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1886. @deftypefunx _FloatNx fminmagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1887. @standards{ISO, math.h}
  1888. @standardsx{fminmagfN, TS 18661-3:2015, math.h}
  1889. @standardsx{fminmagfNx, TS 18661-3:2015, math.h}
  1890. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1891. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  1892. whichever of the two values @var{x} and @var{y} has the smaller absolute
  1893. value. If both have the same absolute value, or either is NaN, they
  1894. behave the same as the @code{fmin} functions.
  1895. @end deftypefun
  1896. @deftypefun double fmaxmag (double @var{x}, double @var{y})
  1897. @deftypefunx float fmaxmagf (float @var{x}, float @var{y})
  1898. @deftypefunx {long double} fmaxmagl (long double @var{x}, long double @var{y})
  1899. @deftypefunx _FloatN fmaxmagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1900. @deftypefunx _FloatNx fmaxmagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1901. @standards{ISO, math.h}
  1902. @standardsx{fmaxmagfN, TS 18661-3:2015, math.h}
  1903. @standardsx{fmaxmagfNx, TS 18661-3:2015, math.h}
  1904. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1905. These functions, from TS 18661-1:2014, return whichever of the two
  1906. values @var{x} and @var{y} has the greater absolute value. If both
  1907. have the same absolute value, or either is NaN, they behave the same
  1908. as the @code{fmax} functions.
  1909. @end deftypefun
  1910. @deftypefun double fminimum_mag (double @var{x}, double @var{y})
  1911. @deftypefunx float fminimum_magf (float @var{x}, float @var{y})
  1912. @deftypefunx {long double} fminimum_magl (long double @var{x}, long double @var{y})
  1913. @deftypefunx _FloatN fminimum_magfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1914. @deftypefunx _FloatNx fminimum_magfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1915. @standards{C23, math.h}
  1916. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1917. These functions return whichever of the two values @var{x} and @var{y}
  1918. has the smaller absolute value. If both have the same absolute value,
  1919. or either is NaN, they behave the same as the @code{fminimum}
  1920. functions.
  1921. @end deftypefun
  1922. @deftypefun double fmaximum_mag (double @var{x}, double @var{y})
  1923. @deftypefunx float fmaximum_magf (float @var{x}, float @var{y})
  1924. @deftypefunx {long double} fmaximum_magl (long double @var{x}, long double @var{y})
  1925. @deftypefunx _FloatN fmaximum_magfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1926. @deftypefunx _FloatNx fmaximum_magfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1927. @standards{C23, math.h}
  1928. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1929. These functions return whichever of the two values @var{x} and @var{y}
  1930. has the greater absolute value. If both have the same absolute value,
  1931. or either is NaN, they behave the same as the @code{fmaximum}
  1932. functions.
  1933. @end deftypefun
  1934. @deftypefun double fminimum_mag_num (double @var{x}, double @var{y})
  1935. @deftypefunx float fminimum_mag_numf (float @var{x}, float @var{y})
  1936. @deftypefunx {long double} fminimum_mag_numl (long double @var{x}, long double @var{y})
  1937. @deftypefunx _FloatN fminimum_mag_numfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1938. @deftypefunx _FloatNx fminimum_mag_numfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1939. @standards{C23, math.h}
  1940. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1941. These functions return whichever of the two values @var{x} and @var{y}
  1942. has the smaller absolute value. If both have the same absolute value,
  1943. or either is NaN, they behave the same as the @code{fminimum_num}
  1944. functions.
  1945. @end deftypefun
  1946. @deftypefun double fmaximum_mag_num (double @var{x}, double @var{y})
  1947. @deftypefunx float fmaximum_mag_numf (float @var{x}, float @var{y})
  1948. @deftypefunx {long double} fmaximum_mag_numl (long double @var{x}, long double @var{y})
  1949. @deftypefunx _FloatN fmaximum_mag_numfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1950. @deftypefunx _FloatNx fmaximum_mag_numfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1951. @standards{C23, math.h}
  1952. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1953. These functions return whichever of the two values @var{x} and @var{y}
  1954. has the greater absolute value. If both have the same absolute value,
  1955. or either is NaN, they behave the same as the @code{fmaximum_num}
  1956. functions.
  1957. @end deftypefun
  1958. @deftypefun double fdim (double @var{x}, double @var{y})
  1959. @deftypefunx float fdimf (float @var{x}, float @var{y})
  1960. @deftypefunx {long double} fdiml (long double @var{x}, long double @var{y})
  1961. @deftypefunx _FloatN fdimfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1962. @deftypefunx _FloatNx fdimfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1963. @standards{ISO, math.h}
  1964. @standardsx{fdimfN, TS 18661-3:2015, math.h}
  1965. @standardsx{fdimfNx, TS 18661-3:2015, math.h}
  1966. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1967. The @code{fdim} function returns the positive difference between
  1968. @var{x} and @var{y}. The positive difference is @math{@var{x} -
  1969. @var{y}} if @var{x} is greater than @var{y}, and @math{0} otherwise.
  1970. If @var{x}, @var{y}, or both are NaN, NaN is returned.
  1971. @end deftypefun
  1972. @deftypefun double fma (double @var{x}, double @var{y}, double @var{z})
  1973. @deftypefunx float fmaf (float @var{x}, float @var{y}, float @var{z})
  1974. @deftypefunx {long double} fmal (long double @var{x}, long double @var{y}, long double @var{z})
  1975. @deftypefunx _FloatN fmafN (_Float@var{N} @var{x}, _Float@var{N} @var{y}, _Float@var{N} @var{z})
  1976. @deftypefunx _FloatNx fmafNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y}, _Float@var{N}x @var{z})
  1977. @standards{ISO, math.h}
  1978. @standardsx{fmafN, TS 18661-3:2015, math.h}
  1979. @standardsx{fmafNx, TS 18661-3:2015, math.h}
  1980. @cindex butterfly
  1981. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1982. The @code{fma} function performs floating-point multiply-add. This is
  1983. the operation @math{(@var{x} @mul{} @var{y}) + @var{z}}, but the
  1984. intermediate result is not rounded to the destination type. This can
  1985. sometimes improve the precision of a calculation.
  1986. This function was introduced because some processors have a special
  1987. instruction to perform multiply-add. The C compiler cannot use it
  1988. directly, because the expression @samp{x*y + z} is defined to round the
  1989. intermediate result. @code{fma} lets you choose when you want to round
  1990. only once.
  1991. @vindex FP_FAST_FMA
  1992. On processors which do not implement multiply-add in hardware,
  1993. @code{fma} can be very slow since it must avoid intermediate rounding.
  1994. @file{math.h} defines the symbols @code{FP_FAST_FMA},
  1995. @code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} when the corresponding
  1996. version of @code{fma} is no slower than the expression @samp{x*y + z}.
  1997. In @theglibc{}, this always means the operation is implemented in
  1998. hardware.
  1999. @end deftypefun
  2000. @deftypefun float fadd (double @var{x}, double @var{y})
  2001. @deftypefunx float faddl (long double @var{x}, long double @var{y})
  2002. @deftypefunx double daddl (long double @var{x}, long double @var{y})
  2003. @deftypefunx _FloatM fMaddfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2004. @deftypefunx _FloatM fMaddfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2005. @deftypefunx _FloatMx fMxaddfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2006. @deftypefunx _FloatMx fMxaddfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2007. @standards{TS 18661-1:2014, math.h}
  2008. @standardsx{fMaddfN, TS 18661-3:2015, math.h}
  2009. @standardsx{fMaddfNx, TS 18661-3:2015, math.h}
  2010. @standardsx{fMxaddfN, TS 18661-3:2015, math.h}
  2011. @standardsx{fMxaddfNx, TS 18661-3:2015, math.h}
  2012. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2013. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  2014. @math{@var{x} + @var{y}}, rounded once to the return type of the
  2015. function without any intermediate rounding to the type of the
  2016. arguments.
  2017. @end deftypefun
  2018. @deftypefun float fsub (double @var{x}, double @var{y})
  2019. @deftypefunx float fsubl (long double @var{x}, long double @var{y})
  2020. @deftypefunx double dsubl (long double @var{x}, long double @var{y})
  2021. @deftypefunx _FloatM fMsubfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2022. @deftypefunx _FloatM fMsubfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2023. @deftypefunx _FloatMx fMxsubfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2024. @deftypefunx _FloatMx fMxsubfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2025. @standards{TS 18661-1:2014, math.h}
  2026. @standardsx{fMsubfN, TS 18661-3:2015, math.h}
  2027. @standardsx{fMsubfNx, TS 18661-3:2015, math.h}
  2028. @standardsx{fMxsubfN, TS 18661-3:2015, math.h}
  2029. @standardsx{fMxsubfNx, TS 18661-3:2015, math.h}
  2030. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2031. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  2032. @math{@var{x} - @var{y}}, rounded once to the return type of the
  2033. function without any intermediate rounding to the type of the
  2034. arguments.
  2035. @end deftypefun
  2036. @deftypefun float fmul (double @var{x}, double @var{y})
  2037. @deftypefunx float fmull (long double @var{x}, long double @var{y})
  2038. @deftypefunx double dmull (long double @var{x}, long double @var{y})
  2039. @deftypefunx _FloatM fMmulfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2040. @deftypefunx _FloatM fMmulfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2041. @deftypefunx _FloatMx fMxmulfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2042. @deftypefunx _FloatMx fMxmulfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2043. @standards{TS 18661-1:2014, math.h}
  2044. @standardsx{fMmulfN, TS 18661-3:2015, math.h}
  2045. @standardsx{fMmulfNx, TS 18661-3:2015, math.h}
  2046. @standardsx{fMxmulfN, TS 18661-3:2015, math.h}
  2047. @standardsx{fMxmulfNx, TS 18661-3:2015, math.h}
  2048. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2049. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  2050. @math{@var{x} * @var{y}}, rounded once to the return type of the
  2051. function without any intermediate rounding to the type of the
  2052. arguments.
  2053. @end deftypefun
  2054. @deftypefun float fdiv (double @var{x}, double @var{y})
  2055. @deftypefunx float fdivl (long double @var{x}, long double @var{y})
  2056. @deftypefunx double ddivl (long double @var{x}, long double @var{y})
  2057. @deftypefunx _FloatM fMdivfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2058. @deftypefunx _FloatM fMdivfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2059. @deftypefunx _FloatMx fMxdivfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  2060. @deftypefunx _FloatMx fMxdivfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  2061. @standards{TS 18661-1:2014, math.h}
  2062. @standardsx{fMdivfN, TS 18661-3:2015, math.h}
  2063. @standardsx{fMdivfNx, TS 18661-3:2015, math.h}
  2064. @standardsx{fMxdivfN, TS 18661-3:2015, math.h}
  2065. @standardsx{fMxdivfNx, TS 18661-3:2015, math.h}
  2066. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2067. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  2068. @math{@var{x} / @var{y}}, rounded once to the return type of the
  2069. function without any intermediate rounding to the type of the
  2070. arguments.
  2071. @end deftypefun
  2072. @deftypefun float fsqrt (double @var{x})
  2073. @deftypefunx float fsqrtl (long double @var{x})
  2074. @deftypefunx double dsqrtl (long double @var{x})
  2075. @deftypefunx _FloatM fMsqrtfN (_Float@var{N} @var{x})
  2076. @deftypefunx _FloatM fMsqrtfNx (_Float@var{N}x @var{x})
  2077. @deftypefunx _FloatMx fMxsqrtfN (_Float@var{N} @var{x})
  2078. @deftypefunx _FloatMx fMxsqrtfNx (_Float@var{N}x @var{x})
  2079. @standards{TS 18661-1:2014, math.h}
  2080. @standardsx{fMsqrtfN, TS 18661-3:2015, math.h}
  2081. @standardsx{fMsqrtfNx, TS 18661-3:2015, math.h}
  2082. @standardsx{fMxsqrtfN, TS 18661-3:2015, math.h}
  2083. @standardsx{fMxsqrtfNx, TS 18661-3:2015, math.h}
  2084. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2085. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return the
  2086. square root of @var{x}, rounded once to the return type of the
  2087. function without any intermediate rounding to the type of the
  2088. arguments.
  2089. @end deftypefun
  2090. @deftypefun float ffma (double @var{x}, double @var{y}, double @var{z})
  2091. @deftypefunx float ffmal (long double @var{x}, long double @var{y}, long double @var{z})
  2092. @deftypefunx double dfmal (long double @var{x}, long double @var{y}, long double @var{z})
  2093. @deftypefunx _FloatM fMfmafN (_Float@var{N} @var{x}, _Float@var{N} @var{y}, _Float@var{N} @var{z})
  2094. @deftypefunx _FloatM fMfmafNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y}, _Float@var{N}x @var{z})
  2095. @deftypefunx _FloatMx fMxfmafN (_Float@var{N} @var{x}, _Float@var{N} @var{y}, _Float@var{N} @var{z})
  2096. @deftypefunx _FloatMx fMxfmafNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y}, _Float@var{N}x @var{z})
  2097. @standards{TS 18661-1:2014, math.h}
  2098. @standardsx{fMfmafN, TS 18661-3:2015, math.h}
  2099. @standardsx{fMfmafNx, TS 18661-3:2015, math.h}
  2100. @standardsx{fMxfmafN, TS 18661-3:2015, math.h}
  2101. @standardsx{fMxfmafNx, TS 18661-3:2015, math.h}
  2102. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2103. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  2104. @math{(@var{x} @mul{} @var{y}) + @var{z}}, rounded once to the return
  2105. type of the function without any intermediate rounding to the type of
  2106. the arguments and without any intermediate rounding of result of the
  2107. multiplication.
  2108. @end deftypefun
  2109. @node Complex Numbers
  2110. @section Complex Numbers
  2111. @pindex complex.h
  2112. @cindex complex numbers
  2113. @w{ISO C99} introduces support for complex numbers in C. This is done
  2114. with a new type qualifier, @code{complex}. It is a keyword if and only
  2115. if @file{complex.h} has been included. There are three complex types,
  2116. corresponding to the three real types: @code{float complex},
  2117. @code{double complex}, and @code{long double complex}.
  2118. Likewise, on machines that have support for @code{_Float@var{N}} or
  2119. @code{_Float@var{N}x} enabled, the complex types @code{_Float@var{N}
  2120. complex} and @code{_Float@var{N}x complex} are also available if
  2121. @file{complex.h} has been included; @pxref{Mathematics}.
  2122. To construct complex numbers you need a way to indicate the imaginary
  2123. part of a number. There is no standard notation for an imaginary
  2124. floating point constant. Instead, @file{complex.h} defines two macros
  2125. that can be used to create complex numbers.
  2126. @deftypevr Macro {const float complex} _Complex_I
  2127. @standards{C99, complex.h}
  2128. This macro is a representation of the complex number ``@math{0+1i}''.
  2129. Multiplying a real floating-point value by @code{_Complex_I} gives a
  2130. complex number whose value is purely imaginary. You can use this to
  2131. construct complex constants:
  2132. @smallexample
  2133. @math{3.0 + 4.0i} = @code{3.0 + 4.0 * _Complex_I}
  2134. @end smallexample
  2135. Note that @code{_Complex_I * _Complex_I} has the value @code{-1}, but
  2136. the type of that value is @code{complex}.
  2137. @end deftypevr
  2138. @c Put this back in when gcc supports _Imaginary_I. It's too confusing.
  2139. @ignore
  2140. @noindent
  2141. Without an optimizing compiler this is more expensive than the use of
  2142. @code{_Imaginary_I} but with is better than nothing. You can avoid all
  2143. the hassles if you use the @code{I} macro below if the name is not
  2144. problem.
  2145. @deftypevr Macro {const float imaginary} _Imaginary_I
  2146. This macro is a representation of the value ``@math{1i}''. I.e., it is
  2147. the value for which
  2148. @smallexample
  2149. _Imaginary_I * _Imaginary_I = -1
  2150. @end smallexample
  2151. @noindent
  2152. The result is not of type @code{float imaginary} but instead @code{float}.
  2153. One can use it to easily construct complex number like in
  2154. @smallexample
  2155. 3.0 - _Imaginary_I * 4.0
  2156. @end smallexample
  2157. @noindent
  2158. which results in the complex number with a real part of 3.0 and a
  2159. imaginary part -4.0.
  2160. @end deftypevr
  2161. @end ignore
  2162. @noindent
  2163. @code{_Complex_I} is a bit of a mouthful. @file{complex.h} also defines
  2164. a shorter name for the same constant.
  2165. @deftypevr Macro {const float complex} I
  2166. @standards{C99, complex.h}
  2167. This macro has exactly the same value as @code{_Complex_I}. Most of the
  2168. time it is preferable. However, it causes problems if you want to use
  2169. the identifier @code{I} for something else. You can safely write
  2170. @smallexample
  2171. #include <complex.h>
  2172. #undef I
  2173. @end smallexample
  2174. @noindent
  2175. if you need @code{I} for your own purposes. (In that case we recommend
  2176. you also define some other short name for @code{_Complex_I}, such as
  2177. @code{J}.)
  2178. @ignore
  2179. If the implementation does not support the @code{imaginary} types
  2180. @code{I} is defined as @code{_Complex_I} which is the second best
  2181. solution. It still can be used in the same way but requires a most
  2182. clever compiler to get the same results.
  2183. @end ignore
  2184. @end deftypevr
  2185. @node Operations on Complex
  2186. @section Projections, Conjugates, and Decomposing of Complex Numbers
  2187. @cindex project complex numbers
  2188. @cindex conjugate complex numbers
  2189. @cindex decompose complex numbers
  2190. @pindex complex.h
  2191. @w{ISO C99} also defines functions that perform basic operations on
  2192. complex numbers, such as decomposition and conjugation. The prototypes
  2193. for all these functions are in @file{complex.h}. All functions are
  2194. available in three variants, one for each of the three complex types.
  2195. @deftypefun double creal (complex double @var{z})
  2196. @deftypefunx float crealf (complex float @var{z})
  2197. @deftypefunx {long double} creall (complex long double @var{z})
  2198. @deftypefunx _FloatN crealfN (complex _Float@var{N} @var{z})
  2199. @deftypefunx _FloatNx crealfNx (complex _Float@var{N}x @var{z})
  2200. @standards{ISO, complex.h}
  2201. @standardsx{crealfN, TS 18661-3:2015, complex.h}
  2202. @standardsx{crealfNx, TS 18661-3:2015, complex.h}
  2203. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2204. These functions return the real part of the complex number @var{z}.
  2205. @end deftypefun
  2206. @deftypefun double cimag (complex double @var{z})
  2207. @deftypefunx float cimagf (complex float @var{z})
  2208. @deftypefunx {long double} cimagl (complex long double @var{z})
  2209. @deftypefunx _FloatN cimagfN (complex _Float@var{N} @var{z})
  2210. @deftypefunx _FloatNx cimagfNx (complex _Float@var{N}x @var{z})
  2211. @standards{ISO, complex.h}
  2212. @standardsx{cimagfN, TS 18661-3:2015, complex.h}
  2213. @standardsx{cimagfNx, TS 18661-3:2015, complex.h}
  2214. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2215. These functions return the imaginary part of the complex number @var{z}.
  2216. @end deftypefun
  2217. @deftypefun {complex double} conj (complex double @var{z})
  2218. @deftypefunx {complex float} conjf (complex float @var{z})
  2219. @deftypefunx {complex long double} conjl (complex long double @var{z})
  2220. @deftypefunx {complex _FloatN} conjfN (complex _Float@var{N} @var{z})
  2221. @deftypefunx {complex _FloatNx} conjfNx (complex _Float@var{N}x @var{z})
  2222. @standards{ISO, complex.h}
  2223. @standardsx{conjfN, TS 18661-3:2015, complex.h}
  2224. @standardsx{conjfNx, TS 18661-3:2015, complex.h}
  2225. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2226. These functions return the conjugate value of the complex number
  2227. @var{z}. The conjugate of a complex number has the same real part and a
  2228. negated imaginary part. In other words, @samp{conj(a + bi) = a + -bi}.
  2229. @end deftypefun
  2230. @deftypefun double carg (complex double @var{z})
  2231. @deftypefunx float cargf (complex float @var{z})
  2232. @deftypefunx {long double} cargl (complex long double @var{z})
  2233. @deftypefunx _FloatN cargfN (complex _Float@var{N} @var{z})
  2234. @deftypefunx _FloatNx cargfNx (complex _Float@var{N}x @var{z})
  2235. @standards{ISO, complex.h}
  2236. @standardsx{cargfN, TS 18661-3:2015, complex.h}
  2237. @standardsx{cargfNx, TS 18661-3:2015, complex.h}
  2238. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2239. These functions return the argument of the complex number @var{z}.
  2240. The argument of a complex number is the angle in the complex plane
  2241. between the positive real axis and a line passing through zero and the
  2242. number. This angle is measured in the usual fashion and ranges from
  2243. @math{-@pi{}} to @math{@pi{}}.
  2244. @code{carg} has a branch cut along the negative real axis.
  2245. @end deftypefun
  2246. @deftypefun {complex double} cproj (complex double @var{z})
  2247. @deftypefunx {complex float} cprojf (complex float @var{z})
  2248. @deftypefunx {complex long double} cprojl (complex long double @var{z})
  2249. @deftypefunx {complex _FloatN} cprojfN (complex _Float@var{N} @var{z})
  2250. @deftypefunx {complex _FloatNx} cprojfNx (complex _Float@var{N}x @var{z})
  2251. @standards{ISO, complex.h}
  2252. @standardsx{cprojfN, TS 18661-3:2015, complex.h}
  2253. @standardsx{cprojfNx, TS 18661-3:2015, complex.h}
  2254. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2255. These functions return the projection of the complex value @var{z} onto
  2256. the Riemann sphere. Values with an infinite imaginary part are projected
  2257. to positive infinity on the real axis, even if the real part is NaN. If
  2258. the real part is infinite, the result is equivalent to
  2259. @smallexample
  2260. INFINITY + I * copysign (0.0, cimag (z))
  2261. @end smallexample
  2262. @end deftypefun
  2263. @node Parsing of Numbers
  2264. @section Parsing of Numbers
  2265. @cindex parsing numbers (in formatted input)
  2266. @cindex converting strings to numbers
  2267. @cindex number syntax, parsing
  2268. @cindex syntax, for reading numbers
  2269. This section describes functions for ``reading'' integer and
  2270. floating-point numbers from a string. It may be more convenient in some
  2271. cases to use @code{sscanf} or one of the related functions; see
  2272. @ref{Formatted Input}. But often you can make a program more robust by
  2273. finding the tokens in the string by hand, then converting the numbers
  2274. one by one.
  2275. @menu
  2276. * Parsing of Integers:: Functions for conversion of integer values.
  2277. * Parsing of Floats:: Functions for conversion of floating-point
  2278. values.
  2279. @end menu
  2280. @node Parsing of Integers
  2281. @subsection Parsing of Integers
  2282. @pindex stdlib.h
  2283. @pindex wchar.h
  2284. The @samp{str} functions are declared in @file{stdlib.h} and those
  2285. beginning with @samp{wcs} are declared in @file{wchar.h}. One might
  2286. wonder about the use of @code{restrict} in the prototypes of the
  2287. functions in this section. It is seemingly useless but the @w{ISO C}
  2288. standard uses it (for the functions defined there) so we have to do it
  2289. as well.
  2290. @deftypefun {long int} strtol (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2291. @standards{ISO, stdlib.h}
  2292. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2293. @c strtol uses the thread-local pointer to the locale in effect, and
  2294. @c strtol_l loads the LC_NUMERIC locale data from it early on and once,
  2295. @c but if the locale is the global locale, and another thread calls
  2296. @c setlocale in a way that modifies the pointer to the LC_CTYPE locale
  2297. @c category, the behavior of e.g. IS*, TOUPPER will vary throughout the
  2298. @c execution of the function, because they re-read the locale data from
  2299. @c the given locale pointer. We solved this by documenting setlocale as
  2300. @c MT-Unsafe.
  2301. The @code{strtol} (``string-to-long'') function converts the initial
  2302. part of @var{string} to a signed integer, which is returned as a value
  2303. of type @code{long int}.
  2304. This function attempts to decompose @var{string} as follows:
  2305. @itemize @bullet
  2306. @item
  2307. A (possibly empty) sequence of whitespace characters. Which characters
  2308. are whitespace is determined by the @code{isspace} function
  2309. (@pxref{Classification of Characters}). These are discarded.
  2310. @item
  2311. An optional plus or minus sign (@samp{+} or @samp{-}).
  2312. @item
  2313. A nonempty sequence of digits in the radix specified by @var{base}.
  2314. If @var{base} is zero, decimal radix is assumed unless the series of
  2315. digits begins with @samp{0} (specifying octal radix), or @samp{0x} or
  2316. @samp{0X} (specifying hexadecimal radix), or @samp{0b} or @samp{0B}
  2317. (specifying binary radix; only supported when C23 features are
  2318. enabled); in other words, the same syntax used for integer constants in C.
  2319. Otherwise @var{base} must have a value between @code{2} and @code{36}.
  2320. If @var{base} is @code{16}, the digits may optionally be preceded by
  2321. @samp{0x} or @samp{0X}. If @var{base} is @code{2}, and C23 features
  2322. are enabled, the digits may optionally be preceded by
  2323. @samp{0b} or @samp{0B}. If base has no legal value the value returned
  2324. is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}.
  2325. @item
  2326. Any remaining characters in the string. If @var{tailptr} is not a null
  2327. pointer, @code{strtol} stores a pointer to this tail in
  2328. @code{*@var{tailptr}}.
  2329. @end itemize
  2330. If the string is empty, contains only whitespace, or does not contain an
  2331. initial substring that has the expected syntax for an integer in the
  2332. specified @var{base}, no conversion is performed. In this case,
  2333. @code{strtol} returns a value of zero and the value stored in
  2334. @code{*@var{tailptr}} is the value of @var{string}.
  2335. In a locale other than the standard @code{"C"} locale, this function
  2336. may recognize additional implementation-dependent syntax.
  2337. If the string has valid syntax for an integer but the value is not
  2338. representable because of overflow, @code{strtol} returns either
  2339. @code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as
  2340. appropriate for the sign of the value. It also sets @code{errno}
  2341. to @code{ERANGE} to indicate there was overflow.
  2342. You should not check for errors by examining the return value of
  2343. @code{strtol}, because the string might be a valid representation of
  2344. @code{0l}, @code{LONG_MAX}, or @code{LONG_MIN}. Instead, check whether
  2345. @var{tailptr} points to what you expect after the number
  2346. (e.g. @code{'\0'} if the string should end after the number). You also
  2347. need to clear @code{errno} before the call and check it afterward, in
  2348. case there was overflow.
  2349. There is an example at the end of this section.
  2350. @end deftypefun
  2351. @deftypefun {long int} wcstol (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2352. @standards{ISO, wchar.h}
  2353. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2354. The @code{wcstol} function is equivalent to the @code{strtol} function
  2355. in nearly all aspects but handles wide character strings.
  2356. The @code{wcstol} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2357. @end deftypefun
  2358. @deftypefun {unsigned long int} strtoul (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2359. @standards{ISO, stdlib.h}
  2360. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2361. The @code{strtoul} (``string-to-unsigned-long'') function is like
  2362. @code{strtol} except it converts to an @code{unsigned long int} value.
  2363. The syntax is the same as described above for @code{strtol}. The value
  2364. returned on overflow is @code{ULONG_MAX} (@pxref{Range of Type}).
  2365. If @var{string} depicts a negative number, @code{strtoul} acts the same
  2366. as @var{strtol} but casts the result to an unsigned integer. That means
  2367. for example that @code{strtoul} on @code{"-1"} returns @code{ULONG_MAX}
  2368. and an input more negative than @code{LONG_MIN} returns
  2369. (@code{ULONG_MAX} + 1) / 2.
  2370. @code{strtoul} sets @code{errno} to @code{EINVAL} if @var{base} is out of
  2371. range, or @code{ERANGE} on overflow.
  2372. @end deftypefun
  2373. @deftypefun {unsigned long int} wcstoul (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2374. @standards{ISO, wchar.h}
  2375. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2376. The @code{wcstoul} function is equivalent to the @code{strtoul} function
  2377. in nearly all aspects but handles wide character strings.
  2378. The @code{wcstoul} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2379. @end deftypefun
  2380. @deftypefun {long long int} strtoll (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2381. @standards{ISO, stdlib.h}
  2382. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2383. The @code{strtoll} function is like @code{strtol} except that it returns
  2384. a @code{long long int} value, and accepts numbers with a correspondingly
  2385. larger range.
  2386. If the string has valid syntax for an integer but the value is not
  2387. representable because of overflow, @code{strtoll} returns either
  2388. @code{LLONG_MAX} or @code{LLONG_MIN} (@pxref{Range of Type}), as
  2389. appropriate for the sign of the value. It also sets @code{errno} to
  2390. @code{ERANGE} to indicate there was overflow.
  2391. The @code{strtoll} function was introduced in @w{ISO C99}.
  2392. @end deftypefun
  2393. @deftypefun {long long int} wcstoll (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2394. @standards{ISO, wchar.h}
  2395. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2396. The @code{wcstoll} function is equivalent to the @code{strtoll} function
  2397. in nearly all aspects but handles wide character strings.
  2398. The @code{wcstoll} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2399. @end deftypefun
  2400. @deftypefun {long long int} strtoq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2401. @standards{BSD, stdlib.h}
  2402. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2403. @code{strtoq} (``string-to-quad-word'') is the BSD name for @code{strtoll}.
  2404. @end deftypefun
  2405. @deftypefun {long long int} wcstoq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2406. @standards{GNU, wchar.h}
  2407. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2408. The @code{wcstoq} function is equivalent to the @code{strtoq} function
  2409. in nearly all aspects but handles wide character strings.
  2410. The @code{wcstoq} function is a GNU extension.
  2411. @end deftypefun
  2412. @deftypefun {unsigned long long int} strtoull (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2413. @standards{ISO, stdlib.h}
  2414. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2415. The @code{strtoull} function is related to @code{strtoll} the same way
  2416. @code{strtoul} is related to @code{strtol}.
  2417. The @code{strtoull} function was introduced in @w{ISO C99}.
  2418. @end deftypefun
  2419. @deftypefun {unsigned long long int} wcstoull (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2420. @standards{ISO, wchar.h}
  2421. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2422. The @code{wcstoull} function is equivalent to the @code{strtoull} function
  2423. in nearly all aspects but handles wide character strings.
  2424. The @code{wcstoull} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2425. @end deftypefun
  2426. @deftypefun {unsigned long long int} strtouq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2427. @standards{BSD, stdlib.h}
  2428. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2429. @code{strtouq} is the BSD name for @code{strtoull}.
  2430. @end deftypefun
  2431. @deftypefun {unsigned long long int} wcstouq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2432. @standards{GNU, wchar.h}
  2433. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2434. The @code{wcstouq} function is equivalent to the @code{strtouq} function
  2435. in nearly all aspects but handles wide character strings.
  2436. The @code{wcstouq} function is a GNU extension.
  2437. @end deftypefun
  2438. @deftypefun intmax_t strtoimax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2439. @standards{ISO, inttypes.h}
  2440. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2441. The @code{strtoimax} function is like @code{strtol} except that it returns
  2442. a @code{intmax_t} value, and accepts numbers of a corresponding range.
  2443. If the string has valid syntax for an integer but the value is not
  2444. representable because of overflow, @code{strtoimax} returns either
  2445. @code{INTMAX_MAX} or @code{INTMAX_MIN} (@pxref{Integers}), as
  2446. appropriate for the sign of the value. It also sets @code{errno} to
  2447. @code{ERANGE} to indicate there was overflow.
  2448. See @ref{Integers} for a description of the @code{intmax_t} type. The
  2449. @code{strtoimax} function was introduced in @w{ISO C99}.
  2450. @end deftypefun
  2451. @deftypefun intmax_t wcstoimax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2452. @standards{ISO, wchar.h}
  2453. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2454. The @code{wcstoimax} function is equivalent to the @code{strtoimax} function
  2455. in nearly all aspects but handles wide character strings.
  2456. The @code{wcstoimax} function was introduced in @w{ISO C99}.
  2457. @end deftypefun
  2458. @deftypefun uintmax_t strtoumax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2459. @standards{ISO, inttypes.h}
  2460. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2461. The @code{strtoumax} function is related to @code{strtoimax}
  2462. the same way that @code{strtoul} is related to @code{strtol}.
  2463. See @ref{Integers} for a description of the @code{intmax_t} type. The
  2464. @code{strtoumax} function was introduced in @w{ISO C99}.
  2465. @end deftypefun
  2466. @deftypefun uintmax_t wcstoumax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2467. @standards{ISO, wchar.h}
  2468. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2469. The @code{wcstoumax} function is equivalent to the @code{strtoumax} function
  2470. in nearly all aspects but handles wide character strings.
  2471. The @code{wcstoumax} function was introduced in @w{ISO C99}.
  2472. @end deftypefun
  2473. @deftypefun {long int} atol (const char *@var{string})
  2474. @standards{ISO, stdlib.h}
  2475. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2476. This function is similar to the @code{strtol} function with a @var{base}
  2477. argument of @code{10}, except that it need not detect overflow errors.
  2478. The @code{atol} function is provided mostly for compatibility with
  2479. existing code; using @code{strtol} is more robust.
  2480. @end deftypefun
  2481. @deftypefun int atoi (const char *@var{string})
  2482. @standards{ISO, stdlib.h}
  2483. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2484. This function is like @code{atol}, except that it returns an @code{int}.
  2485. The @code{atoi} function is also considered obsolete; use @code{strtol}
  2486. instead.
  2487. @end deftypefun
  2488. @deftypefun {long long int} atoll (const char *@var{string})
  2489. @standards{ISO, stdlib.h}
  2490. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2491. This function is similar to @code{atol}, except it returns a @code{long
  2492. long int}.
  2493. The @code{atoll} function was introduced in @w{ISO C99}. It too is
  2494. obsolete (despite having just been added); use @code{strtoll} instead.
  2495. @end deftypefun
  2496. All the functions mentioned in this section so far do not handle
  2497. alternative representations of characters as described in the locale
  2498. data. Some locales specify thousands separator and the way they have to
  2499. be used which can help to make large numbers more readable. To read
  2500. such numbers one has to use the @code{scanf} functions with the @samp{'}
  2501. flag.
  2502. Here is a function which parses a string as a sequence of integers and
  2503. returns the sum of them:
  2504. @smallexample
  2505. int
  2506. sum_ints_from_string (char *string)
  2507. @{
  2508. int sum = 0;
  2509. while (1) @{
  2510. char *tail;
  2511. int next;
  2512. /* @r{Skip whitespace by hand, to detect the end.} */
  2513. while (isspace (*string)) string++;
  2514. if (*string == 0)
  2515. break;
  2516. /* @r{There is more nonwhitespace,} */
  2517. /* @r{so it ought to be another number.} */
  2518. errno = 0;
  2519. /* @r{Parse it.} */
  2520. next = strtol (string, &tail, 0);
  2521. /* @r{Add it in, if not overflow.} */
  2522. if (errno)
  2523. printf ("Overflow\n");
  2524. else
  2525. sum += next;
  2526. /* @r{Advance past it.} */
  2527. string = tail;
  2528. @}
  2529. return sum;
  2530. @}
  2531. @end smallexample
  2532. @node Parsing of Floats
  2533. @subsection Parsing of Floats
  2534. @pindex stdlib.h
  2535. The @samp{str} functions are declared in @file{stdlib.h} and those
  2536. beginning with @samp{wcs} are declared in @file{wchar.h}. One might
  2537. wonder about the use of @code{restrict} in the prototypes of the
  2538. functions in this section. It is seemingly useless but the @w{ISO C}
  2539. standard uses it (for the functions defined there) so we have to do it
  2540. as well.
  2541. @deftypefun double strtod (const char *restrict @var{string}, char **restrict @var{tailptr})
  2542. @standards{ISO, stdlib.h}
  2543. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2544. @c Besides the unsafe-but-ruled-safe locale uses, this uses a lot of
  2545. @c mpn, but it's all safe.
  2546. @c
  2547. @c round_and_return
  2548. @c get_rounding_mode ok
  2549. @c mpn_add_1 ok
  2550. @c mpn_rshift ok
  2551. @c MPN_ZERO ok
  2552. @c MPN2FLOAT -> mpn_construct_(float|double|long_double) ok
  2553. @c str_to_mpn
  2554. @c mpn_mul_1 -> umul_ppmm ok
  2555. @c mpn_add_1 ok
  2556. @c mpn_lshift_1 -> mpn_lshift ok
  2557. @c STRTOF_INTERNAL
  2558. @c MPN_VAR ok
  2559. @c SET_NAN_PAYLOAD ok
  2560. @c STRNCASECMP ok, wide and narrow
  2561. @c round_and_return ok
  2562. @c mpn_mul ok
  2563. @c mpn_addmul_1 ok
  2564. @c ... mpn_sub
  2565. @c mpn_lshift ok
  2566. @c udiv_qrnnd ok
  2567. @c count_leading_zeros ok
  2568. @c add_ssaaaa ok
  2569. @c sub_ddmmss ok
  2570. @c umul_ppmm ok
  2571. @c mpn_submul_1 ok
  2572. The @code{strtod} (``string-to-double'') function converts the initial
  2573. part of @var{string} to a floating-point number, which is returned as a
  2574. value of type @code{double}.
  2575. This function attempts to decompose @var{string} as follows:
  2576. @itemize @bullet
  2577. @item
  2578. A (possibly empty) sequence of whitespace characters. Which characters
  2579. are whitespace is determined by the @code{isspace} function
  2580. (@pxref{Classification of Characters}). These are discarded.
  2581. @item
  2582. An optional plus or minus sign (@samp{+} or @samp{-}).
  2583. @item A floating point number in decimal or hexadecimal format. The
  2584. decimal format is:
  2585. @itemize @minus
  2586. @item
  2587. A nonempty sequence of digits optionally containing a decimal-point
  2588. character---normally @samp{.}, but it depends on the locale
  2589. (@pxref{General Numeric}).
  2590. @item
  2591. An optional exponent part, consisting of a character @samp{e} or
  2592. @samp{E}, an optional sign, and a sequence of digits.
  2593. @end itemize
  2594. The hexadecimal format is as follows:
  2595. @itemize @minus
  2596. @item
  2597. A 0x or 0X followed by a nonempty sequence of hexadecimal digits
  2598. optionally containing a decimal-point character---normally @samp{.}, but
  2599. it depends on the locale (@pxref{General Numeric}).
  2600. @item
  2601. An optional binary-exponent part, consisting of a character @samp{p} or
  2602. @samp{P}, an optional sign, and a sequence of digits.
  2603. @end itemize
  2604. @item
  2605. Any remaining characters in the string. If @var{tailptr} is not a null
  2606. pointer, a pointer to this tail of the string is stored in
  2607. @code{*@var{tailptr}}.
  2608. @end itemize
  2609. If the string is empty, contains only whitespace, or does not contain an
  2610. initial substring that has the expected syntax for a floating-point
  2611. number, no conversion is performed. In this case, @code{strtod} returns
  2612. a value of zero and the value returned in @code{*@var{tailptr}} is the
  2613. value of @var{string}.
  2614. In a locale other than the standard @code{"C"} or @code{"POSIX"} locales,
  2615. this function may recognize additional locale-dependent syntax.
  2616. If the string has valid syntax for a floating-point number but the value
  2617. is outside the range of a @code{double}, @code{strtod} will signal
  2618. overflow or underflow as described in @ref{Math Error Reporting}.
  2619. @code{strtod} recognizes four special input strings. The strings
  2620. @code{"inf"} and @code{"infinity"} are converted to @math{@infinity{}},
  2621. or to the largest representable value if the floating-point format
  2622. doesn't support infinities. You can prepend a @code{"+"} or @code{"-"}
  2623. to specify the sign. Case is ignored when scanning these strings.
  2624. The strings @code{"nan"} and @code{"nan(@var{chars@dots{}})"} are converted
  2625. to NaN. Again, case is ignored. If @var{chars@dots{}} are provided, they
  2626. are used in some unspecified fashion to select a particular
  2627. representation of NaN (there can be several).
  2628. Since zero is a valid result as well as the value returned on error, you
  2629. should check for errors in the same way as for @code{strtol}, by
  2630. examining @code{errno} and @var{tailptr}.
  2631. @end deftypefun
  2632. @deftypefun float strtof (const char *@var{string}, char **@var{tailptr})
  2633. @deftypefunx {long double} strtold (const char *@var{string}, char **@var{tailptr})
  2634. @standards{ISO, stdlib.h}
  2635. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2636. @comment See safety comments for strtod.
  2637. These functions are analogous to @code{strtod}, but return @code{float}
  2638. and @code{long double} values respectively. They report errors in the
  2639. same way as @code{strtod}. @code{strtof} can be substantially faster
  2640. than @code{strtod}, but has less precision; conversely, @code{strtold}
  2641. can be much slower but has more precision (on systems where @code{long
  2642. double} is a separate type).
  2643. These functions have been GNU extensions and are new to @w{ISO C99}.
  2644. @end deftypefun
  2645. @deftypefun _FloatN strtofN (const char *@var{string}, char **@var{tailptr})
  2646. @deftypefunx _FloatNx strtofNx (const char *@var{string}, char **@var{tailptr})
  2647. @standards{ISO/IEC TS 18661-3, stdlib.h}
  2648. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2649. @comment See safety comments for strtod.
  2650. These functions are like @code{strtod}, except for the return type.
  2651. They were introduced in @w{ISO/IEC TS 18661-3} and are available on machines
  2652. that support the related types; @pxref{Mathematics}.
  2653. @end deftypefun
  2654. @deftypefun double wcstod (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr})
  2655. @deftypefunx float wcstof (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2656. @deftypefunx {long double} wcstold (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2657. @deftypefunx _FloatN wcstofN (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2658. @deftypefunx _FloatNx wcstofNx (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2659. @standards{ISO, wchar.h}
  2660. @standardsx{wcstofN, GNU, wchar.h}
  2661. @standardsx{wcstofNx, GNU, wchar.h}
  2662. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2663. @comment See safety comments for strtod.
  2664. The @code{wcstod}, @code{wcstof}, @code{wcstol}, @code{wcstof@var{N}},
  2665. and @code{wcstof@var{N}x} functions are equivalent in nearly all aspects
  2666. to the @code{strtod}, @code{strtof}, @code{strtold},
  2667. @code{strtof@var{N}}, and @code{strtof@var{N}x} functions, but they
  2668. handle wide character strings.
  2669. The @code{wcstod} function was introduced in @w{Amendment 1} of @w{ISO
  2670. C90}. The @code{wcstof} and @code{wcstold} functions were introduced in
  2671. @w{ISO C99}.
  2672. The @code{wcstof@var{N}} and @code{wcstof@var{N}x} functions are not in
  2673. any standard, but are added to provide completeness for the
  2674. non-deprecated interface of wide character string to floating-point
  2675. conversion functions. They are only available on machines that support
  2676. the related types; @pxref{Mathematics}.
  2677. @end deftypefun
  2678. @deftypefun double atof (const char *@var{string})
  2679. @standards{ISO, stdlib.h}
  2680. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2681. This function is similar to the @code{strtod} function, except that it
  2682. need not detect overflow and underflow errors. The @code{atof} function
  2683. is provided mostly for compatibility with existing code; using
  2684. @code{strtod} is more robust.
  2685. @end deftypefun
  2686. @Theglibc{} also provides @samp{_l} versions of these functions,
  2687. which take an additional argument, the locale to use in conversion.
  2688. See also @ref{Parsing of Integers}.
  2689. @node Printing of Floats
  2690. @section Printing of Floats
  2691. @pindex stdlib.h
  2692. The @samp{strfrom} functions are declared in @file{stdlib.h}.
  2693. @deftypefun int strfromd (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, double @var{value})
  2694. @deftypefunx int strfromf (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, float @var{value})
  2695. @deftypefunx int strfroml (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, long double @var{value})
  2696. @standards{ISO/IEC TS 18661-1, stdlib.h}
  2697. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2698. @comment All these functions depend on both __printf_fp and __printf_fphex,
  2699. @comment which are both AS-unsafe (ascuheap) and AC-unsafe (acsmem).
  2700. The functions @code{strfromd} (``string-from-double''), @code{strfromf}
  2701. (``string-from-float''), and @code{strfroml} (``string-from-long-double'')
  2702. convert the floating-point number @var{value} to a string of characters and
  2703. stores them into the area pointed to by @var{string}. The conversion
  2704. writes at most @var{size} characters and respects the format specified by
  2705. @var{format}.
  2706. The format string must start with the character @samp{%}. An optional
  2707. precision follows, which starts with a period, @samp{.}, and may be
  2708. followed by a decimal integer, representing the precision. If a decimal
  2709. integer is not specified after the period, the precision is taken to be
  2710. zero. The character @samp{*} is not allowed. Finally, the format string
  2711. ends with one of the following conversion specifiers: @samp{a}, @samp{A},
  2712. @samp{e}, @samp{E}, @samp{f}, @samp{F}, @samp{g} or @samp{G} (@pxref{Table
  2713. of Output Conversions}). Invalid format strings result in undefined
  2714. behavior.
  2715. These functions return the number of characters that would have been
  2716. written to @var{string} had @var{size} been sufficiently large, not
  2717. counting the terminating null character. Thus, the null-terminated output
  2718. has been completely written if and only if the returned value is less than
  2719. @var{size}.
  2720. These functions were introduced by ISO/IEC TS 18661-1.
  2721. @end deftypefun
  2722. @deftypefun int strfromfN (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, _Float@var{N} @var{value})
  2723. @deftypefunx int strfromfNx (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, _Float@var{N}x @var{value})
  2724. @standards{ISO/IEC TS 18661-3, stdlib.h}
  2725. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2726. @comment See safety comments for strfromd.
  2727. These functions are like @code{strfromd}, except for the type of
  2728. @code{value}.
  2729. They were introduced in @w{ISO/IEC TS 18661-3} and are available on machines
  2730. that support the related types; @pxref{Mathematics}.
  2731. @end deftypefun
  2732. @node System V Number Conversion
  2733. @section Old-fashioned System V number-to-string functions
  2734. The old @w{System V} C library provided three functions to convert
  2735. numbers to strings, with unusual and hard-to-use semantics. @Theglibc{}
  2736. also provides these functions and some natural extensions.
  2737. These functions are only available in @theglibc{} and on systems descended
  2738. from AT&T Unix. Therefore, unless these functions do precisely what you
  2739. need, it is better to use @code{sprintf}, which is standard.
  2740. All these functions are defined in @file{stdlib.h}.
  2741. @deftypefun {char *} ecvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2742. @standards{SVID, stdlib.h}
  2743. @standards{Unix98, stdlib.h}
  2744. @safety{@prelim{}@mtunsafe{@mtasurace{:ecvt}}@asunsafe{}@acsafe{}}
  2745. The function @code{ecvt} converts the floating-point number @var{value}
  2746. to a string with at most @var{ndigit} decimal digits. The
  2747. returned string contains no decimal point or sign. The first digit of
  2748. the string is non-zero (unless @var{value} is actually zero) and the
  2749. last digit is rounded to nearest. @code{*@var{decpt}} is set to the
  2750. index in the string of the first digit after the decimal point.
  2751. @code{*@var{neg}} is set to a nonzero value if @var{value} is negative,
  2752. zero otherwise.
  2753. If @var{ndigit} decimal digits would exceed the precision of a
  2754. @code{double} it is reduced to a system-specific value.
  2755. The returned string is statically allocated and overwritten by each call
  2756. to @code{ecvt}.
  2757. If @var{value} is zero, it is implementation defined whether
  2758. @code{*@var{decpt}} is @code{0} or @code{1}.
  2759. For example: @code{ecvt (12.3, 5, &d, &n)} returns @code{"12300"}
  2760. and sets @var{d} to @code{2} and @var{n} to @code{0}.
  2761. @end deftypefun
  2762. @deftypefun {char *} fcvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2763. @standards{SVID, stdlib.h}
  2764. @standards{Unix98, stdlib.h}
  2765. @safety{@prelim{}@mtunsafe{@mtasurace{:fcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2766. The function @code{fcvt} is like @code{ecvt}, but @var{ndigit} specifies
  2767. the number of digits after the decimal point. If @var{ndigit} is less
  2768. than zero, @var{value} is rounded to the @math{@var{ndigit}+1}'th place to the
  2769. left of the decimal point. For example, if @var{ndigit} is @code{-1},
  2770. @var{value} will be rounded to the nearest 10. If @var{ndigit} is
  2771. negative and larger than the number of digits to the left of the decimal
  2772. point in @var{value}, @var{value} will be rounded to one significant digit.
  2773. If @var{ndigit} decimal digits would exceed the precision of a
  2774. @code{double} it is reduced to a system-specific value.
  2775. The returned string is statically allocated and overwritten by each call
  2776. to @code{fcvt}.
  2777. @end deftypefun
  2778. @deftypefun {char *} gcvt (double @var{value}, int @var{ndigit}, char *@var{buf})
  2779. @standards{SVID, stdlib.h}
  2780. @standards{Unix98, stdlib.h}
  2781. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2782. @c gcvt calls sprintf, that ultimately calls vfprintf, which malloc()s
  2783. @c args_value if it's too large, but gcvt never exercises this path.
  2784. @code{gcvt} is functionally equivalent to @samp{sprintf(buf, "%*g",
  2785. ndigit, value)}. It is provided only for compatibility's sake. It
  2786. returns @var{buf}.
  2787. If @var{ndigit} decimal digits would exceed the precision of a
  2788. @code{double} it is reduced to a system-specific value.
  2789. @end deftypefun
  2790. As extensions, @theglibc{} provides versions of these three
  2791. functions that take @code{long double} arguments.
  2792. @deftypefun {char *} qecvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2793. @standards{GNU, stdlib.h}
  2794. @safety{@prelim{}@mtunsafe{@mtasurace{:qecvt}}@asunsafe{}@acsafe{}}
  2795. This function is equivalent to @code{ecvt} except that it takes a
  2796. @code{long double} for the first parameter and that @var{ndigit} is
  2797. restricted by the precision of a @code{long double}.
  2798. @end deftypefun
  2799. @deftypefun {char *} qfcvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2800. @standards{GNU, stdlib.h}
  2801. @safety{@prelim{}@mtunsafe{@mtasurace{:qfcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2802. This function is equivalent to @code{fcvt} except that it
  2803. takes a @code{long double} for the first parameter and that @var{ndigit} is
  2804. restricted by the precision of a @code{long double}.
  2805. @end deftypefun
  2806. @deftypefun {char *} qgcvt (long double @var{value}, int @var{ndigit}, char *@var{buf})
  2807. @standards{GNU, stdlib.h}
  2808. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2809. This function is equivalent to @code{gcvt} except that it takes a
  2810. @code{long double} for the first parameter and that @var{ndigit} is
  2811. restricted by the precision of a @code{long double}.
  2812. @end deftypefun
  2813. @cindex gcvt_r
  2814. The @code{ecvt} and @code{fcvt} functions, and their @code{long double}
  2815. equivalents, all return a string located in a static buffer which is
  2816. overwritten by the next call to the function. @Theglibc{}
  2817. provides another set of extended functions which write the converted
  2818. string into a user-supplied buffer. These have the conventional
  2819. @code{_r} suffix.
  2820. @code{gcvt_r} is not necessary, because @code{gcvt} already uses a
  2821. user-supplied buffer.
  2822. @deftypefun int ecvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2823. @standards{GNU, stdlib.h}
  2824. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2825. The @code{ecvt_r} function is the same as @code{ecvt}, except
  2826. that it places its result into the user-specified buffer pointed to by
  2827. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2828. case of an error and zero otherwise.
  2829. This function is a GNU extension.
  2830. @end deftypefun
  2831. @deftypefun int fcvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2832. @standards{SVID, stdlib.h}
  2833. @standards{Unix98, stdlib.h}
  2834. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2835. The @code{fcvt_r} function is the same as @code{fcvt}, except that it
  2836. places its result into the user-specified buffer pointed to by
  2837. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2838. case of an error and zero otherwise.
  2839. This function is a GNU extension.
  2840. @end deftypefun
  2841. @deftypefun int qecvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2842. @standards{GNU, stdlib.h}
  2843. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2844. The @code{qecvt_r} function is the same as @code{qecvt}, except
  2845. that it places its result into the user-specified buffer pointed to by
  2846. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2847. case of an error and zero otherwise.
  2848. This function is a GNU extension.
  2849. @end deftypefun
  2850. @deftypefun int qfcvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2851. @standards{GNU, stdlib.h}
  2852. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2853. The @code{qfcvt_r} function is the same as @code{qfcvt}, except
  2854. that it places its result into the user-specified buffer pointed to by
  2855. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2856. case of an error and zero otherwise.
  2857. This function is a GNU extension.
  2858. @end deftypefun