arith.texi 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170
  1. @node Arithmetic, Date and Time, 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. Similiarly, 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. @end deftypefun
  989. @deftypefun int fedisableexcept (int @var{excepts})
  990. @standards{GNU, fenv.h}
  991. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  992. This function disables traps for each of the exceptions as indicated by
  993. the parameter @var{excepts}. The individual exceptions are described in
  994. @ref{Status bit operations}. Only the specified exceptions are
  995. disabled, the status of the other exceptions is not changed.
  996. The function returns the previous enabled exceptions in case the
  997. operation was successful, @code{-1} otherwise.
  998. @end deftypefun
  999. @deftypefun int fegetexcept (void)
  1000. @standards{GNU, fenv.h}
  1001. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1002. The function returns a bitmask of all currently enabled exceptions. It
  1003. returns @code{-1} in case of failure.
  1004. @end deftypefun
  1005. @node Arithmetic Functions
  1006. @section Arithmetic Functions
  1007. The C library provides functions to do basic operations on
  1008. floating-point numbers. These include absolute value, maximum and minimum,
  1009. normalization, bit twiddling, rounding, and a few others.
  1010. @menu
  1011. * Absolute Value:: Absolute values of integers and floats.
  1012. * Normalization Functions:: Extracting exponents and putting them back.
  1013. * Rounding Functions:: Rounding floats to integers.
  1014. * Remainder Functions:: Remainders on division, precisely defined.
  1015. * FP Bit Twiddling:: Sign bit adjustment. Adding epsilon.
  1016. * FP Comparison Functions:: Comparisons without risk of exceptions.
  1017. * Misc FP Arithmetic:: Max, min, positive difference, multiply-add.
  1018. @end menu
  1019. @node Absolute Value
  1020. @subsection Absolute Value
  1021. @cindex absolute value functions
  1022. These functions are provided for obtaining the @dfn{absolute value} (or
  1023. @dfn{magnitude}) of a number. The absolute value of a real number
  1024. @var{x} is @var{x} if @var{x} is positive, @minus{}@var{x} if @var{x} is
  1025. negative. For a complex number @var{z}, whose real part is @var{x} and
  1026. whose imaginary part is @var{y}, the absolute value is @w{@code{sqrt
  1027. (@var{x}*@var{x} + @var{y}*@var{y})}}.
  1028. @pindex math.h
  1029. @pindex stdlib.h
  1030. Prototypes for @code{abs}, @code{labs} and @code{llabs} are in @file{stdlib.h};
  1031. @code{imaxabs} is declared in @file{inttypes.h};
  1032. the @code{fabs} functions are declared in @file{math.h};
  1033. the @code{cabs} functions are declared in @file{complex.h}.
  1034. @deftypefun int abs (int @var{number})
  1035. @deftypefunx {long int} labs (long int @var{number})
  1036. @deftypefunx {long long int} llabs (long long int @var{number})
  1037. @deftypefunx intmax_t imaxabs (intmax_t @var{number})
  1038. @standards{ISO, stdlib.h}
  1039. @standardsx{imaxabs, ISO, inttypes.h}
  1040. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1041. These functions return the absolute value of @var{number}.
  1042. Most computers use a two's complement integer representation, in which
  1043. the absolute value of @code{INT_MIN} (the smallest possible @code{int})
  1044. cannot be represented; thus, @w{@code{abs (INT_MIN)}} is not defined.
  1045. @code{llabs} and @code{imaxdiv} are new to @w{ISO C99}.
  1046. See @ref{Integers} for a description of the @code{intmax_t} type.
  1047. @end deftypefun
  1048. @deftypefun double fabs (double @var{number})
  1049. @deftypefunx float fabsf (float @var{number})
  1050. @deftypefunx {long double} fabsl (long double @var{number})
  1051. @deftypefunx _FloatN fabsfN (_Float@var{N} @var{number})
  1052. @deftypefunx _FloatNx fabsfNx (_Float@var{N}x @var{number})
  1053. @standards{ISO, math.h}
  1054. @standardsx{fabsfN, TS 18661-3:2015, math.h}
  1055. @standardsx{fabsfNx, TS 18661-3:2015, math.h}
  1056. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1057. This function returns the absolute value of the floating-point number
  1058. @var{number}.
  1059. @end deftypefun
  1060. @deftypefun double cabs (complex double @var{z})
  1061. @deftypefunx float cabsf (complex float @var{z})
  1062. @deftypefunx {long double} cabsl (complex long double @var{z})
  1063. @deftypefunx _FloatN cabsfN (complex _Float@var{N} @var{z})
  1064. @deftypefunx _FloatNx cabsfNx (complex _Float@var{N}x @var{z})
  1065. @standards{ISO, complex.h}
  1066. @standardsx{cabsfN, TS 18661-3:2015, complex.h}
  1067. @standardsx{cabsfNx, TS 18661-3:2015, complex.h}
  1068. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1069. These functions return the absolute value of the complex number @var{z}
  1070. (@pxref{Complex Numbers}). The absolute value of a complex number is:
  1071. @smallexample
  1072. sqrt (creal (@var{z}) * creal (@var{z}) + cimag (@var{z}) * cimag (@var{z}))
  1073. @end smallexample
  1074. This function should always be used instead of the direct formula
  1075. because it takes special care to avoid losing precision. It may also
  1076. take advantage of hardware support for this operation. See @code{hypot}
  1077. in @ref{Exponents and Logarithms}.
  1078. @end deftypefun
  1079. @node Normalization Functions
  1080. @subsection Normalization Functions
  1081. @cindex normalization functions (floating-point)
  1082. The functions described in this section are primarily provided as a way
  1083. to efficiently perform certain low-level manipulations on floating point
  1084. numbers that are represented internally using a binary radix;
  1085. see @ref{Floating Point Concepts}. These functions are required to
  1086. have equivalent behavior even if the representation does not use a radix
  1087. of 2, but of course they are unlikely to be particularly efficient in
  1088. those cases.
  1089. @pindex math.h
  1090. All these functions are declared in @file{math.h}.
  1091. @deftypefun double frexp (double @var{value}, int *@var{exponent})
  1092. @deftypefunx float frexpf (float @var{value}, int *@var{exponent})
  1093. @deftypefunx {long double} frexpl (long double @var{value}, int *@var{exponent})
  1094. @deftypefunx _FloatN frexpfN (_Float@var{N} @var{value}, int *@var{exponent})
  1095. @deftypefunx _FloatNx frexpfNx (_Float@var{N}x @var{value}, int *@var{exponent})
  1096. @standards{ISO, math.h}
  1097. @standardsx{frexpfN, TS 18661-3:2015, math.h}
  1098. @standardsx{frexpfNx, TS 18661-3:2015, math.h}
  1099. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1100. These functions are used to split the number @var{value}
  1101. into a normalized fraction and an exponent.
  1102. If the argument @var{value} is not zero, the return value is @var{value}
  1103. times a power of two, and its magnitude is always in the range 1/2
  1104. (inclusive) to 1 (exclusive). The corresponding exponent is stored in
  1105. @code{*@var{exponent}}; the return value multiplied by 2 raised to this
  1106. exponent equals the original number @var{value}.
  1107. For example, @code{frexp (12.8, &exponent)} returns @code{0.8} and
  1108. stores @code{4} in @code{exponent}.
  1109. If @var{value} is zero, then the return value is zero and
  1110. zero is stored in @code{*@var{exponent}}.
  1111. @end deftypefun
  1112. @deftypefun double ldexp (double @var{value}, int @var{exponent})
  1113. @deftypefunx float ldexpf (float @var{value}, int @var{exponent})
  1114. @deftypefunx {long double} ldexpl (long double @var{value}, int @var{exponent})
  1115. @deftypefunx _FloatN ldexpfN (_Float@var{N} @var{value}, int @var{exponent})
  1116. @deftypefunx _FloatNx ldexpfNx (_Float@var{N}x @var{value}, int @var{exponent})
  1117. @standards{ISO, math.h}
  1118. @standardsx{ldexpfN, TS 18661-3:2015, math.h}
  1119. @standardsx{ldexpfNx, TS 18661-3:2015, math.h}
  1120. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1121. These functions return the result of multiplying the floating-point
  1122. number @var{value} by 2 raised to the power @var{exponent}. (It can
  1123. be used to reassemble floating-point numbers that were taken apart
  1124. by @code{frexp}.)
  1125. For example, @code{ldexp (0.8, 4)} returns @code{12.8}.
  1126. @end deftypefun
  1127. The following functions, which come from BSD, provide facilities
  1128. equivalent to those of @code{ldexp} and @code{frexp}. See also the
  1129. @w{ISO C} function @code{logb} which originally also appeared in BSD.
  1130. The @code{_Float@var{N}} and @code{_Float@var{N}} variants of the
  1131. following functions come from TS 18661-3:2015.
  1132. @deftypefun double scalb (double @var{value}, double @var{exponent})
  1133. @deftypefunx float scalbf (float @var{value}, float @var{exponent})
  1134. @deftypefunx {long double} scalbl (long double @var{value}, long double @var{exponent})
  1135. @standards{BSD, math.h}
  1136. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1137. The @code{scalb} function is the BSD name for @code{ldexp}.
  1138. @end deftypefun
  1139. @deftypefun double scalbn (double @var{x}, int @var{n})
  1140. @deftypefunx float scalbnf (float @var{x}, int @var{n})
  1141. @deftypefunx {long double} scalbnl (long double @var{x}, int @var{n})
  1142. @deftypefunx _FloatN scalbnfN (_Float@var{N} @var{x}, int @var{n})
  1143. @deftypefunx _FloatNx scalbnfNx (_Float@var{N}x @var{x}, int @var{n})
  1144. @standards{BSD, math.h}
  1145. @standardsx{scalbnfN, TS 18661-3:2015, math.h}
  1146. @standardsx{scalbnfNx, TS 18661-3:2015, math.h}
  1147. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1148. @code{scalbn} is identical to @code{scalb}, except that the exponent
  1149. @var{n} is an @code{int} instead of a floating-point number.
  1150. @end deftypefun
  1151. @deftypefun double scalbln (double @var{x}, long int @var{n})
  1152. @deftypefunx float scalblnf (float @var{x}, long int @var{n})
  1153. @deftypefunx {long double} scalblnl (long double @var{x}, long int @var{n})
  1154. @deftypefunx _FloatN scalblnfN (_Float@var{N} @var{x}, long int @var{n})
  1155. @deftypefunx _FloatNx scalblnfNx (_Float@var{N}x @var{x}, long int @var{n})
  1156. @standards{BSD, math.h}
  1157. @standardsx{scalblnfN, TS 18661-3:2015, math.h}
  1158. @standardsx{scalblnfNx, TS 18661-3:2015, math.h}
  1159. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1160. @code{scalbln} is identical to @code{scalb}, except that the exponent
  1161. @var{n} is a @code{long int} instead of a floating-point number.
  1162. @end deftypefun
  1163. @deftypefun double significand (double @var{x})
  1164. @deftypefunx float significandf (float @var{x})
  1165. @deftypefunx {long double} significandl (long double @var{x})
  1166. @standards{BSD, math.h}
  1167. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1168. @code{significand} returns the mantissa of @var{x} scaled to the range
  1169. @math{[1, 2)}.
  1170. It is equivalent to @w{@code{scalb (@var{x}, (double) -ilogb (@var{x}))}}.
  1171. This function exists mainly for use in certain standardized tests
  1172. of @w{IEEE 754} conformance.
  1173. @end deftypefun
  1174. @node Rounding Functions
  1175. @subsection Rounding Functions
  1176. @cindex converting floats to integers
  1177. @pindex math.h
  1178. The functions listed here perform operations such as rounding and
  1179. truncation of floating-point values. Some of these functions convert
  1180. floating point numbers to integer values. They are all declared in
  1181. @file{math.h}.
  1182. You can also convert floating-point numbers to integers simply by
  1183. casting them to @code{int}. This discards the fractional part,
  1184. effectively rounding towards zero. However, this only works if the
  1185. result can actually be represented as an @code{int}---for very large
  1186. numbers, this is impossible. The functions listed here return the
  1187. result as a @code{double} instead to get around this problem.
  1188. The @code{fromfp} functions use the following macros, from TS
  1189. 18661-1:2014, to specify the direction of rounding. These correspond
  1190. to the rounding directions defined in IEEE 754-2008.
  1191. @vtable @code
  1192. @item FP_INT_UPWARD
  1193. @standards{ISO, math.h}
  1194. Round toward @math{+@infinity{}}.
  1195. @item FP_INT_DOWNWARD
  1196. @standards{ISO, math.h}
  1197. Round toward @math{-@infinity{}}.
  1198. @item FP_INT_TOWARDZERO
  1199. @standards{ISO, math.h}
  1200. Round toward zero.
  1201. @item FP_INT_TONEARESTFROMZERO
  1202. @standards{ISO, math.h}
  1203. Round to nearest, ties round away from zero.
  1204. @item FP_INT_TONEAREST
  1205. @standards{ISO, math.h}
  1206. Round to nearest, ties round to even.
  1207. @end vtable
  1208. @deftypefun double ceil (double @var{x})
  1209. @deftypefunx float ceilf (float @var{x})
  1210. @deftypefunx {long double} ceill (long double @var{x})
  1211. @deftypefunx _FloatN ceilfN (_Float@var{N} @var{x})
  1212. @deftypefunx _FloatNx ceilfNx (_Float@var{N}x @var{x})
  1213. @standards{ISO, math.h}
  1214. @standardsx{ceilfN, TS 18661-3:2015, math.h}
  1215. @standardsx{ceilfNx, TS 18661-3:2015, math.h}
  1216. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1217. These functions round @var{x} upwards to the nearest integer,
  1218. returning that value as a @code{double}. Thus, @code{ceil (1.5)}
  1219. is @code{2.0}.
  1220. @end deftypefun
  1221. @deftypefun double floor (double @var{x})
  1222. @deftypefunx float floorf (float @var{x})
  1223. @deftypefunx {long double} floorl (long double @var{x})
  1224. @deftypefunx _FloatN floorfN (_Float@var{N} @var{x})
  1225. @deftypefunx _FloatNx floorfNx (_Float@var{N}x @var{x})
  1226. @standards{ISO, math.h}
  1227. @standardsx{floorfN, TS 18661-3:2015, math.h}
  1228. @standardsx{floorfNx, TS 18661-3:2015, math.h}
  1229. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1230. These functions round @var{x} downwards to the nearest
  1231. integer, returning that value as a @code{double}. Thus, @code{floor
  1232. (1.5)} is @code{1.0} and @code{floor (-1.5)} is @code{-2.0}.
  1233. @end deftypefun
  1234. @deftypefun double trunc (double @var{x})
  1235. @deftypefunx float truncf (float @var{x})
  1236. @deftypefunx {long double} truncl (long double @var{x})
  1237. @deftypefunx _FloatN truncfN (_Float@var{N} @var{x})
  1238. @deftypefunx _FloatNx truncfNx (_Float@var{N}x @var{x})
  1239. @standards{ISO, math.h}
  1240. @standardsx{truncfN, TS 18661-3:2015, math.h}
  1241. @standardsx{truncfNx, TS 18661-3:2015, math.h}
  1242. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1243. The @code{trunc} functions round @var{x} towards zero to the nearest
  1244. integer (returned in floating-point format). Thus, @code{trunc (1.5)}
  1245. is @code{1.0} and @code{trunc (-1.5)} is @code{-1.0}.
  1246. @end deftypefun
  1247. @deftypefun double rint (double @var{x})
  1248. @deftypefunx float rintf (float @var{x})
  1249. @deftypefunx {long double} rintl (long double @var{x})
  1250. @deftypefunx _FloatN rintfN (_Float@var{N} @var{x})
  1251. @deftypefunx _FloatNx rintfNx (_Float@var{N}x @var{x})
  1252. @standards{ISO, math.h}
  1253. @standardsx{rintfN, TS 18661-3:2015, math.h}
  1254. @standardsx{rintfNx, TS 18661-3:2015, math.h}
  1255. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1256. These functions round @var{x} to an integer value according to the
  1257. current rounding mode. @xref{Floating Point Parameters}, for
  1258. information about the various rounding modes. The default
  1259. rounding mode is to round to the nearest integer; some machines
  1260. support other modes, but round-to-nearest is always used unless
  1261. you explicitly select another.
  1262. If @var{x} was not initially an integer, these functions raise the
  1263. inexact exception.
  1264. @end deftypefun
  1265. @deftypefun double nearbyint (double @var{x})
  1266. @deftypefunx float nearbyintf (float @var{x})
  1267. @deftypefunx {long double} nearbyintl (long double @var{x})
  1268. @deftypefunx _FloatN nearbyintfN (_Float@var{N} @var{x})
  1269. @deftypefunx _FloatNx nearbyintfNx (_Float@var{N}x @var{x})
  1270. @standards{ISO, math.h}
  1271. @standardsx{nearbyintfN, TS 18661-3:2015, math.h}
  1272. @standardsx{nearbyintfNx, TS 18661-3:2015, math.h}
  1273. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1274. These functions return the same value as the @code{rint} functions, but
  1275. do not raise the inexact exception if @var{x} is not an integer.
  1276. @end deftypefun
  1277. @deftypefun double round (double @var{x})
  1278. @deftypefunx float roundf (float @var{x})
  1279. @deftypefunx {long double} roundl (long double @var{x})
  1280. @deftypefunx _FloatN roundfN (_Float@var{N} @var{x})
  1281. @deftypefunx _FloatNx roundfNx (_Float@var{N}x @var{x})
  1282. @standards{ISO, math.h}
  1283. @standardsx{roundfN, TS 18661-3:2015, math.h}
  1284. @standardsx{roundfNx, TS 18661-3:2015, math.h}
  1285. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1286. These functions are similar to @code{rint}, but they round halfway
  1287. cases away from zero instead of to the nearest integer (or other
  1288. current rounding mode).
  1289. @end deftypefun
  1290. @deftypefun double roundeven (double @var{x})
  1291. @deftypefunx float roundevenf (float @var{x})
  1292. @deftypefunx {long double} roundevenl (long double @var{x})
  1293. @deftypefunx _FloatN roundevenfN (_Float@var{N} @var{x})
  1294. @deftypefunx _FloatNx roundevenfNx (_Float@var{N}x @var{x})
  1295. @standards{ISO, math.h}
  1296. @standardsx{roundevenfN, TS 18661-3:2015, math.h}
  1297. @standardsx{roundevenfNx, TS 18661-3:2015, math.h}
  1298. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1299. These functions, from TS 18661-1:2014 and TS 18661-3:2015, are similar
  1300. to @code{round}, but they round halfway cases to even instead of away
  1301. from zero.
  1302. @end deftypefun
  1303. @deftypefun {long int} lrint (double @var{x})
  1304. @deftypefunx {long int} lrintf (float @var{x})
  1305. @deftypefunx {long int} lrintl (long double @var{x})
  1306. @deftypefunx {long int} lrintfN (_Float@var{N} @var{x})
  1307. @deftypefunx {long int} lrintfNx (_Float@var{N}x @var{x})
  1308. @standards{ISO, math.h}
  1309. @standardsx{lrintfN, TS 18661-3:2015, math.h}
  1310. @standardsx{lrintfNx, TS 18661-3:2015, math.h}
  1311. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1312. These functions are just like @code{rint}, but they return a
  1313. @code{long int} instead of a floating-point number.
  1314. @end deftypefun
  1315. @deftypefun {long long int} llrint (double @var{x})
  1316. @deftypefunx {long long int} llrintf (float @var{x})
  1317. @deftypefunx {long long int} llrintl (long double @var{x})
  1318. @deftypefunx {long long int} llrintfN (_Float@var{N} @var{x})
  1319. @deftypefunx {long long int} llrintfNx (_Float@var{N}x @var{x})
  1320. @standards{ISO, math.h}
  1321. @standardsx{llrintfN, TS 18661-3:2015, math.h}
  1322. @standardsx{llrintfNx, TS 18661-3:2015, math.h}
  1323. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1324. These functions are just like @code{rint}, but they return a
  1325. @code{long long int} instead of a floating-point number.
  1326. @end deftypefun
  1327. @deftypefun {long int} lround (double @var{x})
  1328. @deftypefunx {long int} lroundf (float @var{x})
  1329. @deftypefunx {long int} lroundl (long double @var{x})
  1330. @deftypefunx {long int} lroundfN (_Float@var{N} @var{x})
  1331. @deftypefunx {long int} lroundfNx (_Float@var{N}x @var{x})
  1332. @standards{ISO, math.h}
  1333. @standardsx{lroundfN, TS 18661-3:2015, math.h}
  1334. @standardsx{lroundfNx, TS 18661-3:2015, math.h}
  1335. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1336. These functions are just like @code{round}, but they return a
  1337. @code{long int} instead of a floating-point number.
  1338. @end deftypefun
  1339. @deftypefun {long long int} llround (double @var{x})
  1340. @deftypefunx {long long int} llroundf (float @var{x})
  1341. @deftypefunx {long long int} llroundl (long double @var{x})
  1342. @deftypefunx {long long int} llroundfN (_Float@var{N} @var{x})
  1343. @deftypefunx {long long int} llroundfNx (_Float@var{N}x @var{x})
  1344. @standards{ISO, math.h}
  1345. @standardsx{llroundfN, TS 18661-3:2015, math.h}
  1346. @standardsx{llroundfNx, TS 18661-3:2015, math.h}
  1347. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1348. These functions are just like @code{round}, but they return a
  1349. @code{long long int} instead of a floating-point number.
  1350. @end deftypefun
  1351. @deftypefun intmax_t fromfp (double @var{x}, int @var{round}, unsigned int @var{width})
  1352. @deftypefunx intmax_t fromfpf (float @var{x}, int @var{round}, unsigned int @var{width})
  1353. @deftypefunx intmax_t fromfpl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1354. @deftypefunx intmax_t fromfpfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1355. @deftypefunx intmax_t fromfpfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1356. @deftypefunx uintmax_t ufromfp (double @var{x}, int @var{round}, unsigned int @var{width})
  1357. @deftypefunx uintmax_t ufromfpf (float @var{x}, int @var{round}, unsigned int @var{width})
  1358. @deftypefunx uintmax_t ufromfpl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1359. @deftypefunx uintmax_t ufromfpfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1360. @deftypefunx uintmax_t ufromfpfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1361. @deftypefunx intmax_t fromfpx (double @var{x}, int @var{round}, unsigned int @var{width})
  1362. @deftypefunx intmax_t fromfpxf (float @var{x}, int @var{round}, unsigned int @var{width})
  1363. @deftypefunx intmax_t fromfpxl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1364. @deftypefunx intmax_t fromfpxfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1365. @deftypefunx intmax_t fromfpxfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1366. @deftypefunx uintmax_t ufromfpx (double @var{x}, int @var{round}, unsigned int @var{width})
  1367. @deftypefunx uintmax_t ufromfpxf (float @var{x}, int @var{round}, unsigned int @var{width})
  1368. @deftypefunx uintmax_t ufromfpxl (long double @var{x}, int @var{round}, unsigned int @var{width})
  1369. @deftypefunx uintmax_t ufromfpxfN (_Float@var{N} @var{x}, int @var{round}, unsigned int @var{width})
  1370. @deftypefunx uintmax_t ufromfpxfNx (_Float@var{N}x @var{x}, int @var{round}, unsigned int @var{width})
  1371. @standards{ISO, math.h}
  1372. @standardsx{fromfpfN, TS 18661-3:2015, math.h}
  1373. @standardsx{fromfpfNx, TS 18661-3:2015, math.h}
  1374. @standardsx{ufromfpfN, TS 18661-3:2015, math.h}
  1375. @standardsx{ufromfpfNx, TS 18661-3:2015, math.h}
  1376. @standardsx{fromfpxfN, TS 18661-3:2015, math.h}
  1377. @standardsx{fromfpxfNx, TS 18661-3:2015, math.h}
  1378. @standardsx{ufromfpxfN, TS 18661-3:2015, math.h}
  1379. @standardsx{ufromfpxfNx, TS 18661-3:2015, math.h}
  1380. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1381. These functions, from TS 18661-1:2014 and TS 18661-3:2015, convert a
  1382. floating-point number to an integer according to the rounding direction
  1383. @var{round} (one of the @code{FP_INT_*} macros). If the integer is
  1384. outside the range of a signed or unsigned (depending on the return type
  1385. of the function) type of width @var{width} bits (or outside the range of
  1386. the return type, if @var{width} is larger), or if @var{x} is infinite or
  1387. NaN, or if @var{width} is zero, a domain error occurs and an unspecified
  1388. value is returned. The functions with an @samp{x} in their names raise
  1389. the inexact exception when a domain error does not occur and the
  1390. argument is not an integer; the other functions do not raise the inexact
  1391. exception.
  1392. @end deftypefun
  1393. @deftypefun double modf (double @var{value}, double *@var{integer-part})
  1394. @deftypefunx float modff (float @var{value}, float *@var{integer-part})
  1395. @deftypefunx {long double} modfl (long double @var{value}, long double *@var{integer-part})
  1396. @deftypefunx _FloatN modffN (_Float@var{N} @var{value}, _Float@var{N} *@var{integer-part})
  1397. @deftypefunx _FloatNx modffNx (_Float@var{N}x @var{value}, _Float@var{N}x *@var{integer-part})
  1398. @standards{ISO, math.h}
  1399. @standardsx{modffN, TS 18661-3:2015, math.h}
  1400. @standardsx{modffNx, TS 18661-3:2015, math.h}
  1401. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1402. These functions break the argument @var{value} into an integer part and a
  1403. fractional part (between @code{-1} and @code{1}, exclusive). Their sum
  1404. equals @var{value}. Each of the parts has the same sign as @var{value},
  1405. and the integer part is always rounded toward zero.
  1406. @code{modf} stores the integer part in @code{*@var{integer-part}}, and
  1407. returns the fractional part. For example, @code{modf (2.5, &intpart)}
  1408. returns @code{0.5} and stores @code{2.0} into @code{intpart}.
  1409. @end deftypefun
  1410. @node Remainder Functions
  1411. @subsection Remainder Functions
  1412. The functions in this section compute the remainder on division of two
  1413. floating-point numbers. Each is a little different; pick the one that
  1414. suits your problem.
  1415. @deftypefun double fmod (double @var{numerator}, double @var{denominator})
  1416. @deftypefunx float fmodf (float @var{numerator}, float @var{denominator})
  1417. @deftypefunx {long double} fmodl (long double @var{numerator}, long double @var{denominator})
  1418. @deftypefunx _FloatN fmodfN (_Float@var{N} @var{numerator}, _Float@var{N} @var{denominator})
  1419. @deftypefunx _FloatNx fmodfNx (_Float@var{N}x @var{numerator}, _Float@var{N}x @var{denominator})
  1420. @standards{ISO, math.h}
  1421. @standardsx{fmodfN, TS 18661-3:2015, math.h}
  1422. @standardsx{fmodfNx, TS 18661-3:2015, math.h}
  1423. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1424. These functions compute the remainder from the division of
  1425. @var{numerator} by @var{denominator}. Specifically, the return value is
  1426. @code{@var{numerator} - @w{@var{n} * @var{denominator}}}, where @var{n}
  1427. is the quotient of @var{numerator} divided by @var{denominator}, rounded
  1428. towards zero to an integer. Thus, @w{@code{fmod (6.5, 2.3)}} returns
  1429. @code{1.9}, which is @code{6.5} minus @code{4.6}.
  1430. The result has the same sign as the @var{numerator} and has magnitude
  1431. less than the magnitude of the @var{denominator}.
  1432. If @var{denominator} is zero, @code{fmod} signals a domain error.
  1433. @end deftypefun
  1434. @deftypefun double remainder (double @var{numerator}, double @var{denominator})
  1435. @deftypefunx float remainderf (float @var{numerator}, float @var{denominator})
  1436. @deftypefunx {long double} remainderl (long double @var{numerator}, long double @var{denominator})
  1437. @deftypefunx _FloatN remainderfN (_Float@var{N} @var{numerator}, _Float@var{N} @var{denominator})
  1438. @deftypefunx _FloatNx remainderfNx (_Float@var{N}x @var{numerator}, _Float@var{N}x @var{denominator})
  1439. @standards{ISO, math.h}
  1440. @standardsx{remainderfN, TS 18661-3:2015, math.h}
  1441. @standardsx{remainderfNx, TS 18661-3:2015, math.h}
  1442. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1443. These functions are like @code{fmod} except that they round the
  1444. internal quotient @var{n} to the nearest integer instead of towards zero
  1445. to an integer. For example, @code{remainder (6.5, 2.3)} returns
  1446. @code{-0.4}, which is @code{6.5} minus @code{6.9}.
  1447. The absolute value of the result is less than or equal to half the
  1448. absolute value of the @var{denominator}. The difference between
  1449. @code{fmod (@var{numerator}, @var{denominator})} and @code{remainder
  1450. (@var{numerator}, @var{denominator})} is always either
  1451. @var{denominator}, minus @var{denominator}, or zero.
  1452. If @var{denominator} is zero, @code{remainder} signals a domain error.
  1453. @end deftypefun
  1454. @deftypefun double drem (double @var{numerator}, double @var{denominator})
  1455. @deftypefunx float dremf (float @var{numerator}, float @var{denominator})
  1456. @deftypefunx {long double} dreml (long double @var{numerator}, long double @var{denominator})
  1457. @standards{BSD, math.h}
  1458. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1459. This function is another name for @code{remainder}.
  1460. @end deftypefun
  1461. @node FP Bit Twiddling
  1462. @subsection Setting and modifying single bits of FP values
  1463. @cindex FP arithmetic
  1464. There are some operations that are too complicated or expensive to
  1465. perform by hand on floating-point numbers. @w{ISO C99} defines
  1466. functions to do these operations, which mostly involve changing single
  1467. bits.
  1468. @deftypefun double copysign (double @var{x}, double @var{y})
  1469. @deftypefunx float copysignf (float @var{x}, float @var{y})
  1470. @deftypefunx {long double} copysignl (long double @var{x}, long double @var{y})
  1471. @deftypefunx _FloatN copysignfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1472. @deftypefunx _FloatNx copysignfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1473. @standards{ISO, math.h}
  1474. @standardsx{copysignfN, TS 18661-3:2015, math.h}
  1475. @standardsx{copysignfNx, TS 18661-3:2015, math.h}
  1476. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1477. These functions return @var{x} but with the sign of @var{y}. They work
  1478. even if @var{x} or @var{y} are NaN or zero. Both of these can carry a
  1479. sign (although not all implementations support it) and this is one of
  1480. the few operations that can tell the difference.
  1481. @code{copysign} never raises an exception.
  1482. @c except signalling NaNs
  1483. This function is defined in @w{IEC 559} (and the appendix with
  1484. recommended functions in @w{IEEE 754}/@w{IEEE 854}).
  1485. @end deftypefun
  1486. @deftypefun int signbit (@emph{float-type} @var{x})
  1487. @standards{ISO, math.h}
  1488. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1489. @code{signbit} is a generic macro which can work on all floating-point
  1490. types. It returns a nonzero value if the value of @var{x} has its sign
  1491. bit set.
  1492. This is not the same as @code{x < 0.0}, because @w{IEEE 754} floating
  1493. point allows zero to be signed. The comparison @code{-0.0 < 0.0} is
  1494. false, but @code{signbit (-0.0)} will return a nonzero value.
  1495. @end deftypefun
  1496. @deftypefun double nextafter (double @var{x}, double @var{y})
  1497. @deftypefunx float nextafterf (float @var{x}, float @var{y})
  1498. @deftypefunx {long double} nextafterl (long double @var{x}, long double @var{y})
  1499. @deftypefunx _FloatN nextafterfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1500. @deftypefunx _FloatNx nextafterfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1501. @standards{ISO, math.h}
  1502. @standardsx{nextafterfN, TS 18661-3:2015, math.h}
  1503. @standardsx{nextafterfNx, TS 18661-3:2015, math.h}
  1504. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1505. The @code{nextafter} function returns the next representable neighbor of
  1506. @var{x} in the direction towards @var{y}. The size of the step between
  1507. @var{x} and the result depends on the type of the result. If
  1508. @math{@var{x} = @var{y}} the function simply returns @var{y}. If either
  1509. value is @code{NaN}, @code{NaN} is returned. Otherwise
  1510. a value corresponding to the value of the least significant bit in the
  1511. mantissa is added or subtracted, depending on the direction.
  1512. @code{nextafter} will signal overflow or underflow if the result goes
  1513. outside of the range of normalized numbers.
  1514. This function is defined in @w{IEC 559} (and the appendix with
  1515. recommended functions in @w{IEEE 754}/@w{IEEE 854}).
  1516. @end deftypefun
  1517. @deftypefun double nexttoward (double @var{x}, long double @var{y})
  1518. @deftypefunx float nexttowardf (float @var{x}, long double @var{y})
  1519. @deftypefunx {long double} nexttowardl (long double @var{x}, long double @var{y})
  1520. @standards{ISO, math.h}
  1521. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1522. These functions are identical to the corresponding versions of
  1523. @code{nextafter} except that their second argument is a @code{long
  1524. double}.
  1525. @end deftypefun
  1526. @deftypefun double nextup (double @var{x})
  1527. @deftypefunx float nextupf (float @var{x})
  1528. @deftypefunx {long double} nextupl (long double @var{x})
  1529. @deftypefunx _FloatN nextupfN (_Float@var{N} @var{x})
  1530. @deftypefunx _FloatNx nextupfNx (_Float@var{N}x @var{x})
  1531. @standards{ISO, math.h}
  1532. @standardsx{nextupfN, TS 18661-3:2015, math.h}
  1533. @standardsx{nextupfNx, TS 18661-3:2015, math.h}
  1534. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1535. The @code{nextup} function returns the next representable neighbor of @var{x}
  1536. in the direction of positive infinity. If @var{x} is the smallest negative
  1537. subnormal number in the type of @var{x} the function returns @code{-0}. If
  1538. @math{@var{x} = @code{0}} the function returns the smallest positive subnormal
  1539. number in the type of @var{x}. If @var{x} is NaN, NaN is returned.
  1540. If @var{x} is @math{+@infinity{}}, @math{+@infinity{}} is returned.
  1541. @code{nextup} is from TS 18661-1:2014 and TS 18661-3:2015.
  1542. @code{nextup} never raises an exception except for signaling NaNs.
  1543. @end deftypefun
  1544. @deftypefun double nextdown (double @var{x})
  1545. @deftypefunx float nextdownf (float @var{x})
  1546. @deftypefunx {long double} nextdownl (long double @var{x})
  1547. @deftypefunx _FloatN nextdownfN (_Float@var{N} @var{x})
  1548. @deftypefunx _FloatNx nextdownfNx (_Float@var{N}x @var{x})
  1549. @standards{ISO, math.h}
  1550. @standardsx{nextdownfN, TS 18661-3:2015, math.h}
  1551. @standardsx{nextdownfNx, TS 18661-3:2015, math.h}
  1552. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1553. The @code{nextdown} function returns the next representable neighbor of @var{x}
  1554. in the direction of negative infinity. If @var{x} is the smallest positive
  1555. subnormal number in the type of @var{x} the function returns @code{+0}. If
  1556. @math{@var{x} = @code{0}} the function returns the smallest negative subnormal
  1557. number in the type of @var{x}. If @var{x} is NaN, NaN is returned.
  1558. If @var{x} is @math{-@infinity{}}, @math{-@infinity{}} is returned.
  1559. @code{nextdown} is from TS 18661-1:2014 and TS 18661-3:2015.
  1560. @code{nextdown} never raises an exception except for signaling NaNs.
  1561. @end deftypefun
  1562. @cindex NaN
  1563. @deftypefun double nan (const char *@var{tagp})
  1564. @deftypefunx float nanf (const char *@var{tagp})
  1565. @deftypefunx {long double} nanl (const char *@var{tagp})
  1566. @deftypefunx _FloatN nanfN (const char *@var{tagp})
  1567. @deftypefunx _FloatNx nanfNx (const char *@var{tagp})
  1568. @standards{ISO, math.h}
  1569. @standardsx{nanfN, TS 18661-3:2015, math.h}
  1570. @standardsx{nanfNx, TS 18661-3:2015, math.h}
  1571. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  1572. @c The unsafe-but-ruled-safe locale use comes from strtod.
  1573. The @code{nan} function returns a representation of NaN, provided that
  1574. NaN is supported by the target platform.
  1575. @code{nan ("@var{n-char-sequence}")} is equivalent to
  1576. @code{strtod ("NAN(@var{n-char-sequence})")}.
  1577. The argument @var{tagp} is used in an unspecified manner. On @w{IEEE
  1578. 754} systems, there are many representations of NaN, and @var{tagp}
  1579. selects one. On other systems it may do nothing.
  1580. @end deftypefun
  1581. @deftypefun int canonicalize (double *@var{cx}, const double *@var{x})
  1582. @deftypefunx int canonicalizef (float *@var{cx}, const float *@var{x})
  1583. @deftypefunx int canonicalizel (long double *@var{cx}, const long double *@var{x})
  1584. @deftypefunx int canonicalizefN (_Float@var{N} *@var{cx}, const _Float@var{N} *@var{x})
  1585. @deftypefunx int canonicalizefNx (_Float@var{N}x *@var{cx}, const _Float@var{N}x *@var{x})
  1586. @standards{ISO, math.h}
  1587. @standardsx{canonicalizefN, TS 18661-3:2015, math.h}
  1588. @standardsx{canonicalizefNx, TS 18661-3:2015, math.h}
  1589. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1590. In some floating-point formats, some values have canonical (preferred)
  1591. and noncanonical encodings (for IEEE interchange binary formats, all
  1592. encodings are canonical). These functions, defined by TS
  1593. 18661-1:2014 and TS 18661-3:2015, attempt to produce a canonical version
  1594. of the floating-point value pointed to by @var{x}; if that value is a
  1595. signaling NaN, they raise the invalid exception and produce a quiet
  1596. NaN. If a canonical value is produced, it is stored in the object
  1597. pointed to by @var{cx}, and these functions return zero. Otherwise
  1598. (if a canonical value could not be produced because the object pointed
  1599. to by @var{x} is not a valid representation of any floating-point
  1600. value), the object pointed to by @var{cx} is unchanged and a nonzero
  1601. value is returned.
  1602. Note that some formats have multiple encodings of a value which are
  1603. all equally canonical; when such an encoding is used as an input to
  1604. this function, any such encoding of the same value (or of the
  1605. corresponding quiet NaN, if that value is a signaling NaN) may be
  1606. produced as output.
  1607. @end deftypefun
  1608. @deftypefun double getpayload (const double *@var{x})
  1609. @deftypefunx float getpayloadf (const float *@var{x})
  1610. @deftypefunx {long double} getpayloadl (const long double *@var{x})
  1611. @deftypefunx _FloatN getpayloadfN (const _Float@var{N} *@var{x})
  1612. @deftypefunx _FloatNx getpayloadfNx (const _Float@var{N}x *@var{x})
  1613. @standards{ISO, math.h}
  1614. @standardsx{getpayloadfN, TS 18661-3:2015, math.h}
  1615. @standardsx{getpayloadfNx, TS 18661-3:2015, math.h}
  1616. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1617. IEEE 754 defines the @dfn{payload} of a NaN to be an integer value
  1618. encoded in the representation of the NaN. Payloads are typically
  1619. propagated from NaN inputs to the result of a floating-point
  1620. operation. These functions, defined by TS 18661-1:2014 and TS
  1621. 18661-3:2015, return the payload of the NaN pointed to by @var{x}
  1622. (returned as a positive integer, or positive zero, represented as a
  1623. floating-point number); if @var{x} is not a NaN, they return an
  1624. unspecified value. They raise no floating-point exceptions even for
  1625. signaling NaNs.
  1626. @end deftypefun
  1627. @deftypefun int setpayload (double *@var{x}, double @var{payload})
  1628. @deftypefunx int setpayloadf (float *@var{x}, float @var{payload})
  1629. @deftypefunx int setpayloadl (long double *@var{x}, long double @var{payload})
  1630. @deftypefunx int setpayloadfN (_Float@var{N} *@var{x}, _Float@var{N} @var{payload})
  1631. @deftypefunx int setpayloadfNx (_Float@var{N}x *@var{x}, _Float@var{N}x @var{payload})
  1632. @standards{ISO, math.h}
  1633. @standardsx{setpayloadfN, TS 18661-3:2015, math.h}
  1634. @standardsx{setpayloadfNx, TS 18661-3:2015, math.h}
  1635. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1636. These functions, defined by TS 18661-1:2014 and TS 18661-3:2015, set the
  1637. object pointed to by @var{x} to a quiet NaN with payload @var{payload}
  1638. and a zero sign bit and return zero. If @var{payload} is not a
  1639. positive-signed integer that is a valid payload for a quiet NaN of the
  1640. given type, the object pointed to by @var{x} is set to positive zero and
  1641. a nonzero value is returned. They raise no floating-point exceptions.
  1642. @end deftypefun
  1643. @deftypefun int setpayloadsig (double *@var{x}, double @var{payload})
  1644. @deftypefunx int setpayloadsigf (float *@var{x}, float @var{payload})
  1645. @deftypefunx int setpayloadsigl (long double *@var{x}, long double @var{payload})
  1646. @deftypefunx int setpayloadsigfN (_Float@var{N} *@var{x}, _Float@var{N} @var{payload})
  1647. @deftypefunx int setpayloadsigfNx (_Float@var{N}x *@var{x}, _Float@var{N}x @var{payload})
  1648. @standards{ISO, math.h}
  1649. @standardsx{setpayloadsigfN, TS 18661-3:2015, math.h}
  1650. @standardsx{setpayloadsigfNx, TS 18661-3:2015, math.h}
  1651. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1652. These functions, defined by TS 18661-1:2014 and TS 18661-3:2015, set the
  1653. object pointed to by @var{x} to a signaling NaN with payload
  1654. @var{payload} and a zero sign bit and return zero. If @var{payload} is
  1655. not a positive-signed integer that is a valid payload for a signaling
  1656. NaN of the given type, the object pointed to by @var{x} is set to
  1657. positive zero and a nonzero value is returned. They raise no
  1658. floating-point exceptions.
  1659. @end deftypefun
  1660. @node FP Comparison Functions
  1661. @subsection Floating-Point Comparison Functions
  1662. @cindex unordered comparison
  1663. The standard C comparison operators provoke exceptions when one or other
  1664. of the operands is NaN. For example,
  1665. @smallexample
  1666. int v = a < 1.0;
  1667. @end smallexample
  1668. @noindent
  1669. will raise an exception if @var{a} is NaN. (This does @emph{not}
  1670. happen with @code{==} and @code{!=}; those merely return false and true,
  1671. respectively, when NaN is examined.) Frequently this exception is
  1672. undesirable. @w{ISO C99} therefore defines comparison functions that
  1673. do not raise exceptions when NaN is examined. All of the functions are
  1674. implemented as macros which allow their arguments to be of any
  1675. floating-point type. The macros are guaranteed to evaluate their
  1676. arguments only once. TS 18661-1:2014 adds such a macro for an
  1677. equality comparison that @emph{does} raise an exception for a NaN
  1678. argument; it also adds functions that provide a total ordering on all
  1679. floating-point values, including NaNs, without raising any exceptions
  1680. even for signaling NaNs.
  1681. @deftypefn Macro int isgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1682. @standards{ISO, math.h}
  1683. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1684. This macro determines whether the argument @var{x} is greater than
  1685. @var{y}. It is equivalent to @code{(@var{x}) > (@var{y})}, but no
  1686. exception is raised if @var{x} or @var{y} are NaN.
  1687. @end deftypefn
  1688. @deftypefn Macro int isgreaterequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1689. @standards{ISO, math.h}
  1690. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1691. This macro determines whether the argument @var{x} is greater than or
  1692. equal to @var{y}. It is equivalent to @code{(@var{x}) >= (@var{y})}, but no
  1693. exception is raised if @var{x} or @var{y} are NaN.
  1694. @end deftypefn
  1695. @deftypefn Macro int isless (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1696. @standards{ISO, math.h}
  1697. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1698. This macro determines whether the argument @var{x} is less than @var{y}.
  1699. It is equivalent to @code{(@var{x}) < (@var{y})}, but no exception is
  1700. raised if @var{x} or @var{y} are NaN.
  1701. @end deftypefn
  1702. @deftypefn Macro int islessequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1703. @standards{ISO, math.h}
  1704. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1705. This macro determines whether the argument @var{x} is less than or equal
  1706. to @var{y}. It is equivalent to @code{(@var{x}) <= (@var{y})}, but no
  1707. exception is raised if @var{x} or @var{y} are NaN.
  1708. @end deftypefn
  1709. @deftypefn Macro int islessgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1710. @standards{ISO, math.h}
  1711. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1712. This macro determines whether the argument @var{x} is less or greater
  1713. than @var{y}. It is equivalent to @code{(@var{x}) < (@var{y}) ||
  1714. (@var{x}) > (@var{y})} (although it only evaluates @var{x} and @var{y}
  1715. once), but no exception is raised if @var{x} or @var{y} are NaN.
  1716. This macro is not equivalent to @code{@var{x} != @var{y}}, because that
  1717. expression is true if @var{x} or @var{y} are NaN.
  1718. @end deftypefn
  1719. @deftypefn Macro int isunordered (@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 its arguments are unordered. In other
  1723. words, it is true if @var{x} or @var{y} are NaN, and false otherwise.
  1724. @end deftypefn
  1725. @deftypefn Macro int iseqsig (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
  1726. @standards{ISO, math.h}
  1727. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1728. This macro determines whether its arguments are equal. It is
  1729. equivalent to @code{(@var{x}) == (@var{y})}, but it raises the invalid
  1730. exception and sets @code{errno} to @code{EDOM} if either argument is a
  1731. NaN.
  1732. @end deftypefn
  1733. @deftypefun int totalorder (double @var{x}, double @var{y})
  1734. @deftypefunx int totalorderf (float @var{x}, float @var{y})
  1735. @deftypefunx int totalorderl (long double @var{x}, long double @var{y})
  1736. @deftypefunx int totalorderfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1737. @deftypefunx int totalorderfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1738. @standards{TS 18661-1:2014, math.h}
  1739. @standardsx{totalorderfN, TS 18661-3:2015, math.h}
  1740. @standardsx{totalorderfNx, TS 18661-3:2015, math.h}
  1741. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1742. These functions determine whether the total order relationship,
  1743. defined in IEEE 754-2008, is true for @var{x} and @var{y}, returning
  1744. nonzero if it is true and zero if it is false. No exceptions are
  1745. raised even for signaling NaNs. The relationship is true if they are
  1746. the same floating-point value (including sign for zero and NaNs, and
  1747. payload for NaNs), or if @var{x} comes before @var{y} in the following
  1748. order: negative quiet NaNs, in order of decreasing payload; negative
  1749. signaling NaNs, in order of decreasing payload; negative infinity;
  1750. finite numbers, in ascending order, with negative zero before positive
  1751. zero; positive infinity; positive signaling NaNs, in order of
  1752. increasing payload; positive quiet NaNs, in order of increasing
  1753. payload.
  1754. @end deftypefun
  1755. @deftypefun int totalordermag (double @var{x}, double @var{y})
  1756. @deftypefunx int totalordermagf (float @var{x}, float @var{y})
  1757. @deftypefunx int totalordermagl (long double @var{x}, long double @var{y})
  1758. @deftypefunx int totalordermagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1759. @deftypefunx int totalordermagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1760. @standards{TS 18661-1:2014, math.h}
  1761. @standardsx{totalordermagfN, TS 18661-3:2015, math.h}
  1762. @standardsx{totalordermagfNx, TS 18661-3:2015, math.h}
  1763. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1764. These functions determine whether the total order relationship,
  1765. defined in IEEE 754-2008, is true for the absolute values of @var{x}
  1766. and @var{y}, returning nonzero if it is true and zero if it is false.
  1767. No exceptions are raised even for signaling NaNs.
  1768. @end deftypefun
  1769. Not all machines provide hardware support for these operations. On
  1770. machines that don't, the macros can be very slow. Therefore, you should
  1771. not use these functions when NaN is not a concern.
  1772. @strong{NB:} There are no macros @code{isequal} or @code{isunequal}.
  1773. They are unnecessary, because the @code{==} and @code{!=} operators do
  1774. @emph{not} throw an exception if one or both of the operands are NaN.
  1775. @node Misc FP Arithmetic
  1776. @subsection Miscellaneous FP arithmetic functions
  1777. @cindex minimum
  1778. @cindex maximum
  1779. @cindex positive difference
  1780. @cindex multiply-add
  1781. The functions in this section perform miscellaneous but common
  1782. operations that are awkward to express with C operators. On some
  1783. processors these functions can use special machine instructions to
  1784. perform these operations faster than the equivalent C code.
  1785. @deftypefun double fmin (double @var{x}, double @var{y})
  1786. @deftypefunx float fminf (float @var{x}, float @var{y})
  1787. @deftypefunx {long double} fminl (long double @var{x}, long double @var{y})
  1788. @deftypefunx _FloatN fminfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1789. @deftypefunx _FloatNx fminfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1790. @standards{ISO, math.h}
  1791. @standardsx{fminfN, TS 18661-3:2015, math.h}
  1792. @standardsx{fminfNx, TS 18661-3:2015, math.h}
  1793. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1794. The @code{fmin} function returns the lesser of the two values @var{x}
  1795. and @var{y}. It is similar to the expression
  1796. @smallexample
  1797. ((x) < (y) ? (x) : (y))
  1798. @end smallexample
  1799. except that @var{x} and @var{y} are only evaluated once.
  1800. If an argument is NaN, the other argument is returned. If both arguments
  1801. are NaN, NaN is returned.
  1802. @end deftypefun
  1803. @deftypefun double fmax (double @var{x}, double @var{y})
  1804. @deftypefunx float fmaxf (float @var{x}, float @var{y})
  1805. @deftypefunx {long double} fmaxl (long double @var{x}, long double @var{y})
  1806. @deftypefunx _FloatN fmaxfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1807. @deftypefunx _FloatNx fmaxfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1808. @standards{ISO, math.h}
  1809. @standardsx{fmaxfN, TS 18661-3:2015, math.h}
  1810. @standardsx{fmaxfNx, TS 18661-3:2015, math.h}
  1811. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1812. The @code{fmax} function returns the greater of the two values @var{x}
  1813. and @var{y}.
  1814. If an argument is NaN, the other argument is returned. If both arguments
  1815. are NaN, NaN is returned.
  1816. @end deftypefun
  1817. @deftypefun double fminmag (double @var{x}, double @var{y})
  1818. @deftypefunx float fminmagf (float @var{x}, float @var{y})
  1819. @deftypefunx {long double} fminmagl (long double @var{x}, long double @var{y})
  1820. @deftypefunx _FloatN fminmagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1821. @deftypefunx _FloatNx fminmagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1822. @standards{ISO, math.h}
  1823. @standardsx{fminmagfN, TS 18661-3:2015, math.h}
  1824. @standardsx{fminmagfNx, TS 18661-3:2015, math.h}
  1825. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1826. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  1827. whichever of the two values @var{x} and @var{y} has the smaller absolute
  1828. value. If both have the same absolute value, or either is NaN, they
  1829. behave the same as the @code{fmin} functions.
  1830. @end deftypefun
  1831. @deftypefun double fmaxmag (double @var{x}, double @var{y})
  1832. @deftypefunx float fmaxmagf (float @var{x}, float @var{y})
  1833. @deftypefunx {long double} fmaxmagl (long double @var{x}, long double @var{y})
  1834. @deftypefunx _FloatN fmaxmagfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1835. @deftypefunx _FloatNx fmaxmagfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1836. @standards{ISO, math.h}
  1837. @standardsx{fmaxmagfN, TS 18661-3:2015, math.h}
  1838. @standardsx{fmaxmagfNx, TS 18661-3:2015, math.h}
  1839. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1840. These functions, from TS 18661-1:2014, return whichever of the two
  1841. values @var{x} and @var{y} has the greater absolute value. If both
  1842. have the same absolute value, or either is NaN, they behave the same
  1843. as the @code{fmax} functions.
  1844. @end deftypefun
  1845. @deftypefun double fdim (double @var{x}, double @var{y})
  1846. @deftypefunx float fdimf (float @var{x}, float @var{y})
  1847. @deftypefunx {long double} fdiml (long double @var{x}, long double @var{y})
  1848. @deftypefunx _FloatN fdimfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1849. @deftypefunx _FloatNx fdimfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1850. @standards{ISO, math.h}
  1851. @standardsx{fdimfN, TS 18661-3:2015, math.h}
  1852. @standardsx{fdimfNx, TS 18661-3:2015, math.h}
  1853. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1854. The @code{fdim} function returns the positive difference between
  1855. @var{x} and @var{y}. The positive difference is @math{@var{x} -
  1856. @var{y}} if @var{x} is greater than @var{y}, and @math{0} otherwise.
  1857. If @var{x}, @var{y}, or both are NaN, NaN is returned.
  1858. @end deftypefun
  1859. @deftypefun double fma (double @var{x}, double @var{y}, double @var{z})
  1860. @deftypefunx float fmaf (float @var{x}, float @var{y}, float @var{z})
  1861. @deftypefunx {long double} fmal (long double @var{x}, long double @var{y}, long double @var{z})
  1862. @deftypefunx _FloatN fmafN (_Float@var{N} @var{x}, _Float@var{N} @var{y}, _Float@var{N} @var{z})
  1863. @deftypefunx _FloatNx fmafNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y}, _Float@var{N}x @var{z})
  1864. @standards{ISO, math.h}
  1865. @standardsx{fmafN, TS 18661-3:2015, math.h}
  1866. @standardsx{fmafNx, TS 18661-3:2015, math.h}
  1867. @cindex butterfly
  1868. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1869. The @code{fma} function performs floating-point multiply-add. This is
  1870. the operation @math{(@var{x} @mul{} @var{y}) + @var{z}}, but the
  1871. intermediate result is not rounded to the destination type. This can
  1872. sometimes improve the precision of a calculation.
  1873. This function was introduced because some processors have a special
  1874. instruction to perform multiply-add. The C compiler cannot use it
  1875. directly, because the expression @samp{x*y + z} is defined to round the
  1876. intermediate result. @code{fma} lets you choose when you want to round
  1877. only once.
  1878. @vindex FP_FAST_FMA
  1879. On processors which do not implement multiply-add in hardware,
  1880. @code{fma} can be very slow since it must avoid intermediate rounding.
  1881. @file{math.h} defines the symbols @code{FP_FAST_FMA},
  1882. @code{FP_FAST_FMAF}, and @code{FP_FAST_FMAL} when the corresponding
  1883. version of @code{fma} is no slower than the expression @samp{x*y + z}.
  1884. In @theglibc{}, this always means the operation is implemented in
  1885. hardware.
  1886. @end deftypefun
  1887. @deftypefun float fadd (double @var{x}, double @var{y})
  1888. @deftypefunx float faddl (long double @var{x}, long double @var{y})
  1889. @deftypefunx double daddl (long double @var{x}, long double @var{y})
  1890. @deftypefunx _FloatM fMaddfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1891. @deftypefunx _FloatM fMaddfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1892. @deftypefunx _FloatMx fMxaddfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1893. @deftypefunx _FloatMx fMxaddfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1894. @standards{TS 18661-1:2014, math.h}
  1895. @standardsx{fMaddfN, TS 18661-3:2015, math.h}
  1896. @standardsx{fMaddfNx, TS 18661-3:2015, math.h}
  1897. @standardsx{fMxaddfN, TS 18661-3:2015, math.h}
  1898. @standardsx{fMxaddfNx, TS 18661-3:2015, math.h}
  1899. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1900. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  1901. @math{@var{x} + @var{y}}, rounded once to the return type of the
  1902. function without any intermediate rounding to the type of the
  1903. arguments.
  1904. @end deftypefun
  1905. @deftypefun float fsub (double @var{x}, double @var{y})
  1906. @deftypefunx float fsubl (long double @var{x}, long double @var{y})
  1907. @deftypefunx double dsubl (long double @var{x}, long double @var{y})
  1908. @deftypefunx _FloatM fMsubfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1909. @deftypefunx _FloatM fMsubfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1910. @deftypefunx _FloatMx fMxsubfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1911. @deftypefunx _FloatMx fMxsubfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1912. @standards{TS 18661-1:2014, math.h}
  1913. @standardsx{fMsubfN, TS 18661-3:2015, math.h}
  1914. @standardsx{fMsubfNx, TS 18661-3:2015, math.h}
  1915. @standardsx{fMxsubfN, TS 18661-3:2015, math.h}
  1916. @standardsx{fMxsubfNx, TS 18661-3:2015, math.h}
  1917. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1918. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  1919. @math{@var{x} - @var{y}}, rounded once to the return type of the
  1920. function without any intermediate rounding to the type of the
  1921. arguments.
  1922. @end deftypefun
  1923. @deftypefun float fmul (double @var{x}, double @var{y})
  1924. @deftypefunx float fmull (long double @var{x}, long double @var{y})
  1925. @deftypefunx double dmull (long double @var{x}, long double @var{y})
  1926. @deftypefunx _FloatM fMmulfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1927. @deftypefunx _FloatM fMmulfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1928. @deftypefunx _FloatMx fMxmulfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1929. @deftypefunx _FloatMx fMxmulfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1930. @standards{TS 18661-1:2014, math.h}
  1931. @standardsx{fMmulfN, TS 18661-3:2015, math.h}
  1932. @standardsx{fMmulfNx, TS 18661-3:2015, math.h}
  1933. @standardsx{fMxmulfN, TS 18661-3:2015, math.h}
  1934. @standardsx{fMxmulfNx, TS 18661-3:2015, math.h}
  1935. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1936. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  1937. @math{@var{x} * @var{y}}, rounded once to the return type of the
  1938. function without any intermediate rounding to the type of the
  1939. arguments.
  1940. @end deftypefun
  1941. @deftypefun float fdiv (double @var{x}, double @var{y})
  1942. @deftypefunx float fdivl (long double @var{x}, long double @var{y})
  1943. @deftypefunx double ddivl (long double @var{x}, long double @var{y})
  1944. @deftypefunx _FloatM fMdivfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1945. @deftypefunx _FloatM fMdivfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1946. @deftypefunx _FloatMx fMxdivfN (_Float@var{N} @var{x}, _Float@var{N} @var{y})
  1947. @deftypefunx _FloatMx fMxdivfNx (_Float@var{N}x @var{x}, _Float@var{N}x @var{y})
  1948. @standards{TS 18661-1:2014, math.h}
  1949. @standardsx{fMdivfN, TS 18661-3:2015, math.h}
  1950. @standardsx{fMdivfNx, TS 18661-3:2015, math.h}
  1951. @standardsx{fMxdivfN, TS 18661-3:2015, math.h}
  1952. @standardsx{fMxdivfNx, TS 18661-3:2015, math.h}
  1953. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1954. These functions, from TS 18661-1:2014 and TS 18661-3:2015, return
  1955. @math{@var{x} / @var{y}}, rounded once to the return type of the
  1956. function without any intermediate rounding to the type of the
  1957. arguments.
  1958. @end deftypefun
  1959. @node Complex Numbers
  1960. @section Complex Numbers
  1961. @pindex complex.h
  1962. @cindex complex numbers
  1963. @w{ISO C99} introduces support for complex numbers in C. This is done
  1964. with a new type qualifier, @code{complex}. It is a keyword if and only
  1965. if @file{complex.h} has been included. There are three complex types,
  1966. corresponding to the three real types: @code{float complex},
  1967. @code{double complex}, and @code{long double complex}.
  1968. Likewise, on machines that have support for @code{_Float@var{N}} or
  1969. @code{_Float@var{N}x} enabled, the complex types @code{_Float@var{N}
  1970. complex} and @code{_Float@var{N}x complex} are also available if
  1971. @file{complex.h} has been included; @pxref{Mathematics}.
  1972. To construct complex numbers you need a way to indicate the imaginary
  1973. part of a number. There is no standard notation for an imaginary
  1974. floating point constant. Instead, @file{complex.h} defines two macros
  1975. that can be used to create complex numbers.
  1976. @deftypevr Macro {const float complex} _Complex_I
  1977. @standards{C99, complex.h}
  1978. This macro is a representation of the complex number ``@math{0+1i}''.
  1979. Multiplying a real floating-point value by @code{_Complex_I} gives a
  1980. complex number whose value is purely imaginary. You can use this to
  1981. construct complex constants:
  1982. @smallexample
  1983. @math{3.0 + 4.0i} = @code{3.0 + 4.0 * _Complex_I}
  1984. @end smallexample
  1985. Note that @code{_Complex_I * _Complex_I} has the value @code{-1}, but
  1986. the type of that value is @code{complex}.
  1987. @end deftypevr
  1988. @c Put this back in when gcc supports _Imaginary_I. It's too confusing.
  1989. @ignore
  1990. @noindent
  1991. Without an optimizing compiler this is more expensive than the use of
  1992. @code{_Imaginary_I} but with is better than nothing. You can avoid all
  1993. the hassles if you use the @code{I} macro below if the name is not
  1994. problem.
  1995. @deftypevr Macro {const float imaginary} _Imaginary_I
  1996. This macro is a representation of the value ``@math{1i}''. I.e., it is
  1997. the value for which
  1998. @smallexample
  1999. _Imaginary_I * _Imaginary_I = -1
  2000. @end smallexample
  2001. @noindent
  2002. The result is not of type @code{float imaginary} but instead @code{float}.
  2003. One can use it to easily construct complex number like in
  2004. @smallexample
  2005. 3.0 - _Imaginary_I * 4.0
  2006. @end smallexample
  2007. @noindent
  2008. which results in the complex number with a real part of 3.0 and a
  2009. imaginary part -4.0.
  2010. @end deftypevr
  2011. @end ignore
  2012. @noindent
  2013. @code{_Complex_I} is a bit of a mouthful. @file{complex.h} also defines
  2014. a shorter name for the same constant.
  2015. @deftypevr Macro {const float complex} I
  2016. @standards{C99, complex.h}
  2017. This macro has exactly the same value as @code{_Complex_I}. Most of the
  2018. time it is preferable. However, it causes problems if you want to use
  2019. the identifier @code{I} for something else. You can safely write
  2020. @smallexample
  2021. #include <complex.h>
  2022. #undef I
  2023. @end smallexample
  2024. @noindent
  2025. if you need @code{I} for your own purposes. (In that case we recommend
  2026. you also define some other short name for @code{_Complex_I}, such as
  2027. @code{J}.)
  2028. @ignore
  2029. If the implementation does not support the @code{imaginary} types
  2030. @code{I} is defined as @code{_Complex_I} which is the second best
  2031. solution. It still can be used in the same way but requires a most
  2032. clever compiler to get the same results.
  2033. @end ignore
  2034. @end deftypevr
  2035. @node Operations on Complex
  2036. @section Projections, Conjugates, and Decomposing of Complex Numbers
  2037. @cindex project complex numbers
  2038. @cindex conjugate complex numbers
  2039. @cindex decompose complex numbers
  2040. @pindex complex.h
  2041. @w{ISO C99} also defines functions that perform basic operations on
  2042. complex numbers, such as decomposition and conjugation. The prototypes
  2043. for all these functions are in @file{complex.h}. All functions are
  2044. available in three variants, one for each of the three complex types.
  2045. @deftypefun double creal (complex double @var{z})
  2046. @deftypefunx float crealf (complex float @var{z})
  2047. @deftypefunx {long double} creall (complex long double @var{z})
  2048. @deftypefunx _FloatN crealfN (complex _Float@var{N} @var{z})
  2049. @deftypefunx _FloatNx crealfNx (complex _Float@var{N}x @var{z})
  2050. @standards{ISO, complex.h}
  2051. @standardsx{crealfN, TS 18661-3:2015, complex.h}
  2052. @standardsx{crealfNx, TS 18661-3:2015, complex.h}
  2053. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2054. These functions return the real part of the complex number @var{z}.
  2055. @end deftypefun
  2056. @deftypefun double cimag (complex double @var{z})
  2057. @deftypefunx float cimagf (complex float @var{z})
  2058. @deftypefunx {long double} cimagl (complex long double @var{z})
  2059. @deftypefunx _FloatN cimagfN (complex _Float@var{N} @var{z})
  2060. @deftypefunx _FloatNx cimagfNx (complex _Float@var{N}x @var{z})
  2061. @standards{ISO, complex.h}
  2062. @standardsx{cimagfN, TS 18661-3:2015, complex.h}
  2063. @standardsx{cimagfNx, TS 18661-3:2015, complex.h}
  2064. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2065. These functions return the imaginary part of the complex number @var{z}.
  2066. @end deftypefun
  2067. @deftypefun {complex double} conj (complex double @var{z})
  2068. @deftypefunx {complex float} conjf (complex float @var{z})
  2069. @deftypefunx {complex long double} conjl (complex long double @var{z})
  2070. @deftypefunx {complex _FloatN} conjfN (complex _Float@var{N} @var{z})
  2071. @deftypefunx {complex _FloatNx} conjfNx (complex _Float@var{N}x @var{z})
  2072. @standards{ISO, complex.h}
  2073. @standardsx{conjfN, TS 18661-3:2015, complex.h}
  2074. @standardsx{conjfNx, TS 18661-3:2015, complex.h}
  2075. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2076. These functions return the conjugate value of the complex number
  2077. @var{z}. The conjugate of a complex number has the same real part and a
  2078. negated imaginary part. In other words, @samp{conj(a + bi) = a + -bi}.
  2079. @end deftypefun
  2080. @deftypefun double carg (complex double @var{z})
  2081. @deftypefunx float cargf (complex float @var{z})
  2082. @deftypefunx {long double} cargl (complex long double @var{z})
  2083. @deftypefunx _FloatN cargfN (complex _Float@var{N} @var{z})
  2084. @deftypefunx _FloatNx cargfNx (complex _Float@var{N}x @var{z})
  2085. @standards{ISO, complex.h}
  2086. @standardsx{cargfN, TS 18661-3:2015, complex.h}
  2087. @standardsx{cargfNx, TS 18661-3:2015, complex.h}
  2088. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2089. These functions return the argument of the complex number @var{z}.
  2090. The argument of a complex number is the angle in the complex plane
  2091. between the positive real axis and a line passing through zero and the
  2092. number. This angle is measured in the usual fashion and ranges from
  2093. @math{-@pi{}} to @math{@pi{}}.
  2094. @code{carg} has a branch cut along the negative real axis.
  2095. @end deftypefun
  2096. @deftypefun {complex double} cproj (complex double @var{z})
  2097. @deftypefunx {complex float} cprojf (complex float @var{z})
  2098. @deftypefunx {complex long double} cprojl (complex long double @var{z})
  2099. @deftypefunx {complex _FloatN} cprojfN (complex _Float@var{N} @var{z})
  2100. @deftypefunx {complex _FloatNx} cprojfNx (complex _Float@var{N}x @var{z})
  2101. @standards{ISO, complex.h}
  2102. @standardsx{cprojfN, TS 18661-3:2015, complex.h}
  2103. @standardsx{cprojfNx, TS 18661-3:2015, complex.h}
  2104. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2105. These functions return the projection of the complex value @var{z} onto
  2106. the Riemann sphere. Values with an infinite imaginary part are projected
  2107. to positive infinity on the real axis, even if the real part is NaN. If
  2108. the real part is infinite, the result is equivalent to
  2109. @smallexample
  2110. INFINITY + I * copysign (0.0, cimag (z))
  2111. @end smallexample
  2112. @end deftypefun
  2113. @node Parsing of Numbers
  2114. @section Parsing of Numbers
  2115. @cindex parsing numbers (in formatted input)
  2116. @cindex converting strings to numbers
  2117. @cindex number syntax, parsing
  2118. @cindex syntax, for reading numbers
  2119. This section describes functions for ``reading'' integer and
  2120. floating-point numbers from a string. It may be more convenient in some
  2121. cases to use @code{sscanf} or one of the related functions; see
  2122. @ref{Formatted Input}. But often you can make a program more robust by
  2123. finding the tokens in the string by hand, then converting the numbers
  2124. one by one.
  2125. @menu
  2126. * Parsing of Integers:: Functions for conversion of integer values.
  2127. * Parsing of Floats:: Functions for conversion of floating-point
  2128. values.
  2129. @end menu
  2130. @node Parsing of Integers
  2131. @subsection Parsing of Integers
  2132. @pindex stdlib.h
  2133. @pindex wchar.h
  2134. The @samp{str} functions are declared in @file{stdlib.h} and those
  2135. beginning with @samp{wcs} are declared in @file{wchar.h}. One might
  2136. wonder about the use of @code{restrict} in the prototypes of the
  2137. functions in this section. It is seemingly useless but the @w{ISO C}
  2138. standard uses it (for the functions defined there) so we have to do it
  2139. as well.
  2140. @deftypefun {long int} strtol (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2141. @standards{ISO, stdlib.h}
  2142. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2143. @c strtol uses the thread-local pointer to the locale in effect, and
  2144. @c strtol_l loads the LC_NUMERIC locale data from it early on and once,
  2145. @c but if the locale is the global locale, and another thread calls
  2146. @c setlocale in a way that modifies the pointer to the LC_CTYPE locale
  2147. @c category, the behavior of e.g. IS*, TOUPPER will vary throughout the
  2148. @c execution of the function, because they re-read the locale data from
  2149. @c the given locale pointer. We solved this by documenting setlocale as
  2150. @c MT-Unsafe.
  2151. The @code{strtol} (``string-to-long'') function converts the initial
  2152. part of @var{string} to a signed integer, which is returned as a value
  2153. of type @code{long int}.
  2154. This function attempts to decompose @var{string} as follows:
  2155. @itemize @bullet
  2156. @item
  2157. A (possibly empty) sequence of whitespace characters. Which characters
  2158. are whitespace is determined by the @code{isspace} function
  2159. (@pxref{Classification of Characters}). These are discarded.
  2160. @item
  2161. An optional plus or minus sign (@samp{+} or @samp{-}).
  2162. @item
  2163. A nonempty sequence of digits in the radix specified by @var{base}.
  2164. If @var{base} is zero, decimal radix is assumed unless the series of
  2165. digits begins with @samp{0} (specifying octal radix), or @samp{0x} or
  2166. @samp{0X} (specifying hexadecimal radix); in other words, the same
  2167. syntax used for integer constants in C.
  2168. Otherwise @var{base} must have a value between @code{2} and @code{36}.
  2169. If @var{base} is @code{16}, the digits may optionally be preceded by
  2170. @samp{0x} or @samp{0X}. If base has no legal value the value returned
  2171. is @code{0l} and the global variable @code{errno} is set to @code{EINVAL}.
  2172. @item
  2173. Any remaining characters in the string. If @var{tailptr} is not a null
  2174. pointer, @code{strtol} stores a pointer to this tail in
  2175. @code{*@var{tailptr}}.
  2176. @end itemize
  2177. If the string is empty, contains only whitespace, or does not contain an
  2178. initial substring that has the expected syntax for an integer in the
  2179. specified @var{base}, no conversion is performed. In this case,
  2180. @code{strtol} returns a value of zero and the value stored in
  2181. @code{*@var{tailptr}} is the value of @var{string}.
  2182. In a locale other than the standard @code{"C"} locale, this function
  2183. may recognize additional implementation-dependent syntax.
  2184. If the string has valid syntax for an integer but the value is not
  2185. representable because of overflow, @code{strtol} returns either
  2186. @code{LONG_MAX} or @code{LONG_MIN} (@pxref{Range of Type}), as
  2187. appropriate for the sign of the value. It also sets @code{errno}
  2188. to @code{ERANGE} to indicate there was overflow.
  2189. You should not check for errors by examining the return value of
  2190. @code{strtol}, because the string might be a valid representation of
  2191. @code{0l}, @code{LONG_MAX}, or @code{LONG_MIN}. Instead, check whether
  2192. @var{tailptr} points to what you expect after the number
  2193. (e.g. @code{'\0'} if the string should end after the number). You also
  2194. need to clear @code{errno} before the call and check it afterward, in
  2195. case there was overflow.
  2196. There is an example at the end of this section.
  2197. @end deftypefun
  2198. @deftypefun {long int} wcstol (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2199. @standards{ISO, wchar.h}
  2200. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2201. The @code{wcstol} function is equivalent to the @code{strtol} function
  2202. in nearly all aspects but handles wide character strings.
  2203. The @code{wcstol} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2204. @end deftypefun
  2205. @deftypefun {unsigned long int} strtoul (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2206. @standards{ISO, stdlib.h}
  2207. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2208. The @code{strtoul} (``string-to-unsigned-long'') function is like
  2209. @code{strtol} except it converts to an @code{unsigned long int} value.
  2210. The syntax is the same as described above for @code{strtol}. The value
  2211. returned on overflow is @code{ULONG_MAX} (@pxref{Range of Type}).
  2212. If @var{string} depicts a negative number, @code{strtoul} acts the same
  2213. as @var{strtol} but casts the result to an unsigned integer. That means
  2214. for example that @code{strtoul} on @code{"-1"} returns @code{ULONG_MAX}
  2215. and an input more negative than @code{LONG_MIN} returns
  2216. (@code{ULONG_MAX} + 1) / 2.
  2217. @code{strtoul} sets @code{errno} to @code{EINVAL} if @var{base} is out of
  2218. range, or @code{ERANGE} on overflow.
  2219. @end deftypefun
  2220. @deftypefun {unsigned long int} wcstoul (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2221. @standards{ISO, wchar.h}
  2222. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2223. The @code{wcstoul} function is equivalent to the @code{strtoul} function
  2224. in nearly all aspects but handles wide character strings.
  2225. The @code{wcstoul} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2226. @end deftypefun
  2227. @deftypefun {long long int} strtoll (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2228. @standards{ISO, stdlib.h}
  2229. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2230. The @code{strtoll} function is like @code{strtol} except that it returns
  2231. a @code{long long int} value, and accepts numbers with a correspondingly
  2232. larger range.
  2233. If the string has valid syntax for an integer but the value is not
  2234. representable because of overflow, @code{strtoll} returns either
  2235. @code{LLONG_MAX} or @code{LLONG_MIN} (@pxref{Range of Type}), as
  2236. appropriate for the sign of the value. It also sets @code{errno} to
  2237. @code{ERANGE} to indicate there was overflow.
  2238. The @code{strtoll} function was introduced in @w{ISO C99}.
  2239. @end deftypefun
  2240. @deftypefun {long long int} wcstoll (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2241. @standards{ISO, wchar.h}
  2242. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2243. The @code{wcstoll} function is equivalent to the @code{strtoll} function
  2244. in nearly all aspects but handles wide character strings.
  2245. The @code{wcstoll} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2246. @end deftypefun
  2247. @deftypefun {long long int} strtoq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2248. @standards{BSD, stdlib.h}
  2249. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2250. @code{strtoq} (``string-to-quad-word'') is the BSD name for @code{strtoll}.
  2251. @end deftypefun
  2252. @deftypefun {long long int} wcstoq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2253. @standards{GNU, wchar.h}
  2254. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2255. The @code{wcstoq} function is equivalent to the @code{strtoq} function
  2256. in nearly all aspects but handles wide character strings.
  2257. The @code{wcstoq} function is a GNU extension.
  2258. @end deftypefun
  2259. @deftypefun {unsigned long long int} strtoull (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2260. @standards{ISO, stdlib.h}
  2261. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2262. The @code{strtoull} function is related to @code{strtoll} the same way
  2263. @code{strtoul} is related to @code{strtol}.
  2264. The @code{strtoull} function was introduced in @w{ISO C99}.
  2265. @end deftypefun
  2266. @deftypefun {unsigned long long int} wcstoull (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2267. @standards{ISO, wchar.h}
  2268. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2269. The @code{wcstoull} function is equivalent to the @code{strtoull} function
  2270. in nearly all aspects but handles wide character strings.
  2271. The @code{wcstoull} function was introduced in @w{Amendment 1} of @w{ISO C90}.
  2272. @end deftypefun
  2273. @deftypefun {unsigned long long int} strtouq (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2274. @standards{BSD, stdlib.h}
  2275. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2276. @code{strtouq} is the BSD name for @code{strtoull}.
  2277. @end deftypefun
  2278. @deftypefun {unsigned long long int} wcstouq (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2279. @standards{GNU, wchar.h}
  2280. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2281. The @code{wcstouq} function is equivalent to the @code{strtouq} function
  2282. in nearly all aspects but handles wide character strings.
  2283. The @code{wcstouq} function is a GNU extension.
  2284. @end deftypefun
  2285. @deftypefun intmax_t strtoimax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2286. @standards{ISO, inttypes.h}
  2287. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2288. The @code{strtoimax} function is like @code{strtol} except that it returns
  2289. a @code{intmax_t} value, and accepts numbers of a corresponding range.
  2290. If the string has valid syntax for an integer but the value is not
  2291. representable because of overflow, @code{strtoimax} returns either
  2292. @code{INTMAX_MAX} or @code{INTMAX_MIN} (@pxref{Integers}), as
  2293. appropriate for the sign of the value. It also sets @code{errno} to
  2294. @code{ERANGE} to indicate there was overflow.
  2295. See @ref{Integers} for a description of the @code{intmax_t} type. The
  2296. @code{strtoimax} function was introduced in @w{ISO C99}.
  2297. @end deftypefun
  2298. @deftypefun intmax_t wcstoimax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2299. @standards{ISO, wchar.h}
  2300. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2301. The @code{wcstoimax} function is equivalent to the @code{strtoimax} function
  2302. in nearly all aspects but handles wide character strings.
  2303. The @code{wcstoimax} function was introduced in @w{ISO C99}.
  2304. @end deftypefun
  2305. @deftypefun uintmax_t strtoumax (const char *restrict @var{string}, char **restrict @var{tailptr}, int @var{base})
  2306. @standards{ISO, inttypes.h}
  2307. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2308. The @code{strtoumax} function is related to @code{strtoimax}
  2309. the same way that @code{strtoul} is related to @code{strtol}.
  2310. See @ref{Integers} for a description of the @code{intmax_t} type. The
  2311. @code{strtoumax} function was introduced in @w{ISO C99}.
  2312. @end deftypefun
  2313. @deftypefun uintmax_t wcstoumax (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr}, int @var{base})
  2314. @standards{ISO, wchar.h}
  2315. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2316. The @code{wcstoumax} function is equivalent to the @code{strtoumax} function
  2317. in nearly all aspects but handles wide character strings.
  2318. The @code{wcstoumax} function was introduced in @w{ISO C99}.
  2319. @end deftypefun
  2320. @deftypefun {long int} atol (const char *@var{string})
  2321. @standards{ISO, stdlib.h}
  2322. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2323. This function is similar to the @code{strtol} function with a @var{base}
  2324. argument of @code{10}, except that it need not detect overflow errors.
  2325. The @code{atol} function is provided mostly for compatibility with
  2326. existing code; using @code{strtol} is more robust.
  2327. @end deftypefun
  2328. @deftypefun int atoi (const char *@var{string})
  2329. @standards{ISO, stdlib.h}
  2330. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2331. This function is like @code{atol}, except that it returns an @code{int}.
  2332. The @code{atoi} function is also considered obsolete; use @code{strtol}
  2333. instead.
  2334. @end deftypefun
  2335. @deftypefun {long long int} atoll (const char *@var{string})
  2336. @standards{ISO, stdlib.h}
  2337. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2338. This function is similar to @code{atol}, except it returns a @code{long
  2339. long int}.
  2340. The @code{atoll} function was introduced in @w{ISO C99}. It too is
  2341. obsolete (despite having just been added); use @code{strtoll} instead.
  2342. @end deftypefun
  2343. All the functions mentioned in this section so far do not handle
  2344. alternative representations of characters as described in the locale
  2345. data. Some locales specify thousands separator and the way they have to
  2346. be used which can help to make large numbers more readable. To read
  2347. such numbers one has to use the @code{scanf} functions with the @samp{'}
  2348. flag.
  2349. Here is a function which parses a string as a sequence of integers and
  2350. returns the sum of them:
  2351. @smallexample
  2352. int
  2353. sum_ints_from_string (char *string)
  2354. @{
  2355. int sum = 0;
  2356. while (1) @{
  2357. char *tail;
  2358. int next;
  2359. /* @r{Skip whitespace by hand, to detect the end.} */
  2360. while (isspace (*string)) string++;
  2361. if (*string == 0)
  2362. break;
  2363. /* @r{There is more nonwhitespace,} */
  2364. /* @r{so it ought to be another number.} */
  2365. errno = 0;
  2366. /* @r{Parse it.} */
  2367. next = strtol (string, &tail, 0);
  2368. /* @r{Add it in, if not overflow.} */
  2369. if (errno)
  2370. printf ("Overflow\n");
  2371. else
  2372. sum += next;
  2373. /* @r{Advance past it.} */
  2374. string = tail;
  2375. @}
  2376. return sum;
  2377. @}
  2378. @end smallexample
  2379. @node Parsing of Floats
  2380. @subsection Parsing of Floats
  2381. @pindex stdlib.h
  2382. The @samp{str} functions are declared in @file{stdlib.h} and those
  2383. beginning with @samp{wcs} are declared in @file{wchar.h}. One might
  2384. wonder about the use of @code{restrict} in the prototypes of the
  2385. functions in this section. It is seemingly useless but the @w{ISO C}
  2386. standard uses it (for the functions defined there) so we have to do it
  2387. as well.
  2388. @deftypefun double strtod (const char *restrict @var{string}, char **restrict @var{tailptr})
  2389. @standards{ISO, stdlib.h}
  2390. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2391. @c Besides the unsafe-but-ruled-safe locale uses, this uses a lot of
  2392. @c mpn, but it's all safe.
  2393. @c
  2394. @c round_and_return
  2395. @c get_rounding_mode ok
  2396. @c mpn_add_1 ok
  2397. @c mpn_rshift ok
  2398. @c MPN_ZERO ok
  2399. @c MPN2FLOAT -> mpn_construct_(float|double|long_double) ok
  2400. @c str_to_mpn
  2401. @c mpn_mul_1 -> umul_ppmm ok
  2402. @c mpn_add_1 ok
  2403. @c mpn_lshift_1 -> mpn_lshift ok
  2404. @c STRTOF_INTERNAL
  2405. @c MPN_VAR ok
  2406. @c SET_NAN_PAYLOAD ok
  2407. @c STRNCASECMP ok, wide and narrow
  2408. @c round_and_return ok
  2409. @c mpn_mul ok
  2410. @c mpn_addmul_1 ok
  2411. @c ... mpn_sub
  2412. @c mpn_lshift ok
  2413. @c udiv_qrnnd ok
  2414. @c count_leading_zeros ok
  2415. @c add_ssaaaa ok
  2416. @c sub_ddmmss ok
  2417. @c umul_ppmm ok
  2418. @c mpn_submul_1 ok
  2419. The @code{strtod} (``string-to-double'') function converts the initial
  2420. part of @var{string} to a floating-point number, which is returned as a
  2421. value of type @code{double}.
  2422. This function attempts to decompose @var{string} as follows:
  2423. @itemize @bullet
  2424. @item
  2425. A (possibly empty) sequence of whitespace characters. Which characters
  2426. are whitespace is determined by the @code{isspace} function
  2427. (@pxref{Classification of Characters}). These are discarded.
  2428. @item
  2429. An optional plus or minus sign (@samp{+} or @samp{-}).
  2430. @item A floating point number in decimal or hexadecimal format. The
  2431. decimal format is:
  2432. @itemize @minus
  2433. @item
  2434. A nonempty sequence of digits optionally containing a decimal-point
  2435. character---normally @samp{.}, but it depends on the locale
  2436. (@pxref{General Numeric}).
  2437. @item
  2438. An optional exponent part, consisting of a character @samp{e} or
  2439. @samp{E}, an optional sign, and a sequence of digits.
  2440. @end itemize
  2441. The hexadecimal format is as follows:
  2442. @itemize @minus
  2443. @item
  2444. A 0x or 0X followed by a nonempty sequence of hexadecimal digits
  2445. optionally containing a decimal-point character---normally @samp{.}, but
  2446. it depends on the locale (@pxref{General Numeric}).
  2447. @item
  2448. An optional binary-exponent part, consisting of a character @samp{p} or
  2449. @samp{P}, an optional sign, and a sequence of digits.
  2450. @end itemize
  2451. @item
  2452. Any remaining characters in the string. If @var{tailptr} is not a null
  2453. pointer, a pointer to this tail of the string is stored in
  2454. @code{*@var{tailptr}}.
  2455. @end itemize
  2456. If the string is empty, contains only whitespace, or does not contain an
  2457. initial substring that has the expected syntax for a floating-point
  2458. number, no conversion is performed. In this case, @code{strtod} returns
  2459. a value of zero and the value returned in @code{*@var{tailptr}} is the
  2460. value of @var{string}.
  2461. In a locale other than the standard @code{"C"} or @code{"POSIX"} locales,
  2462. this function may recognize additional locale-dependent syntax.
  2463. If the string has valid syntax for a floating-point number but the value
  2464. is outside the range of a @code{double}, @code{strtod} will signal
  2465. overflow or underflow as described in @ref{Math Error Reporting}.
  2466. @code{strtod} recognizes four special input strings. The strings
  2467. @code{"inf"} and @code{"infinity"} are converted to @math{@infinity{}},
  2468. or to the largest representable value if the floating-point format
  2469. doesn't support infinities. You can prepend a @code{"+"} or @code{"-"}
  2470. to specify the sign. Case is ignored when scanning these strings.
  2471. The strings @code{"nan"} and @code{"nan(@var{chars@dots{}})"} are converted
  2472. to NaN. Again, case is ignored. If @var{chars@dots{}} are provided, they
  2473. are used in some unspecified fashion to select a particular
  2474. representation of NaN (there can be several).
  2475. Since zero is a valid result as well as the value returned on error, you
  2476. should check for errors in the same way as for @code{strtol}, by
  2477. examining @code{errno} and @var{tailptr}.
  2478. @end deftypefun
  2479. @deftypefun float strtof (const char *@var{string}, char **@var{tailptr})
  2480. @deftypefunx {long double} strtold (const char *@var{string}, char **@var{tailptr})
  2481. @standards{ISO, stdlib.h}
  2482. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2483. @comment See safety comments for strtod.
  2484. These functions are analogous to @code{strtod}, but return @code{float}
  2485. and @code{long double} values respectively. They report errors in the
  2486. same way as @code{strtod}. @code{strtof} can be substantially faster
  2487. than @code{strtod}, but has less precision; conversely, @code{strtold}
  2488. can be much slower but has more precision (on systems where @code{long
  2489. double} is a separate type).
  2490. These functions have been GNU extensions and are new to @w{ISO C99}.
  2491. @end deftypefun
  2492. @deftypefun _FloatN strtofN (const char *@var{string}, char **@var{tailptr})
  2493. @deftypefunx _FloatNx strtofNx (const char *@var{string}, char **@var{tailptr})
  2494. @standards{ISO/IEC TS 18661-3, stdlib.h}
  2495. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2496. @comment See safety comments for strtod.
  2497. These functions are like @code{strtod}, except for the return type.
  2498. They were introduced in @w{ISO/IEC TS 18661-3} and are available on machines
  2499. that support the related types; @pxref{Mathematics}.
  2500. @end deftypefun
  2501. @deftypefun double wcstod (const wchar_t *restrict @var{string}, wchar_t **restrict @var{tailptr})
  2502. @deftypefunx float wcstof (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2503. @deftypefunx {long double} wcstold (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2504. @deftypefunx _FloatN wcstofN (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2505. @deftypefunx _FloatNx wcstofNx (const wchar_t *@var{string}, wchar_t **@var{tailptr})
  2506. @standards{ISO, wchar.h}
  2507. @standardsx{wcstofN, GNU, wchar.h}
  2508. @standardsx{wcstofNx, GNU, wchar.h}
  2509. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2510. @comment See safety comments for strtod.
  2511. The @code{wcstod}, @code{wcstof}, @code{wcstol}, @code{wcstof@var{N}},
  2512. and @code{wcstof@var{N}x} functions are equivalent in nearly all aspects
  2513. to the @code{strtod}, @code{strtof}, @code{strtold},
  2514. @code{strtof@var{N}}, and @code{strtof@var{N}x} functions, but they
  2515. handle wide character strings.
  2516. The @code{wcstod} function was introduced in @w{Amendment 1} of @w{ISO
  2517. C90}. The @code{wcstof} and @code{wcstold} functions were introduced in
  2518. @w{ISO C99}.
  2519. The @code{wcstof@var{N}} and @code{wcstof@var{N}x} functions are not in
  2520. any standard, but are added to provide completeness for the
  2521. non-deprecated interface of wide character string to floating-point
  2522. conversion functions. They are only available on machines that support
  2523. the related types; @pxref{Mathematics}.
  2524. @end deftypefun
  2525. @deftypefun double atof (const char *@var{string})
  2526. @standards{ISO, stdlib.h}
  2527. @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
  2528. This function is similar to the @code{strtod} function, except that it
  2529. need not detect overflow and underflow errors. The @code{atof} function
  2530. is provided mostly for compatibility with existing code; using
  2531. @code{strtod} is more robust.
  2532. @end deftypefun
  2533. @Theglibc{} also provides @samp{_l} versions of these functions,
  2534. which take an additional argument, the locale to use in conversion.
  2535. See also @ref{Parsing of Integers}.
  2536. @node Printing of Floats
  2537. @section Printing of Floats
  2538. @pindex stdlib.h
  2539. The @samp{strfrom} functions are declared in @file{stdlib.h}.
  2540. @deftypefun int strfromd (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, double @var{value})
  2541. @deftypefunx int strfromf (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, float @var{value})
  2542. @deftypefunx int strfroml (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, long double @var{value})
  2543. @standards{ISO/IEC TS 18661-1, stdlib.h}
  2544. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2545. @comment All these functions depend on both __printf_fp and __printf_fphex,
  2546. @comment which are both AS-unsafe (ascuheap) and AC-unsafe (acsmem).
  2547. The functions @code{strfromd} (``string-from-double''), @code{strfromf}
  2548. (``string-from-float''), and @code{strfroml} (``string-from-long-double'')
  2549. convert the floating-point number @var{value} to a string of characters and
  2550. stores them into the area pointed to by @var{string}. The conversion
  2551. writes at most @var{size} characters and respects the format specified by
  2552. @var{format}.
  2553. The format string must start with the character @samp{%}. An optional
  2554. precision follows, which starts with a period, @samp{.}, and may be
  2555. followed by a decimal integer, representing the precision. If a decimal
  2556. integer is not specified after the period, the precision is taken to be
  2557. zero. The character @samp{*} is not allowed. Finally, the format string
  2558. ends with one of the following conversion specifiers: @samp{a}, @samp{A},
  2559. @samp{e}, @samp{E}, @samp{f}, @samp{F}, @samp{g} or @samp{G} (@pxref{Table
  2560. of Output Conversions}). Invalid format strings result in undefined
  2561. behavior.
  2562. These functions return the number of characters that would have been
  2563. written to @var{string} had @var{size} been sufficiently large, not
  2564. counting the terminating null character. Thus, the null-terminated output
  2565. has been completely written if and only if the returned value is less than
  2566. @var{size}.
  2567. These functions were introduced by ISO/IEC TS 18661-1.
  2568. @end deftypefun
  2569. @deftypefun int strfromfN (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, _Float@var{N} @var{value})
  2570. @deftypefunx int strfromfNx (char *restrict @var{string}, size_t @var{size}, const char *restrict @var{format}, _Float@var{N}x @var{value})
  2571. @standards{ISO/IEC TS 18661-3, stdlib.h}
  2572. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2573. @comment See safety comments for strfromd.
  2574. These functions are like @code{strfromd}, except for the type of
  2575. @code{value}.
  2576. They were introduced in @w{ISO/IEC TS 18661-3} and are available on machines
  2577. that support the related types; @pxref{Mathematics}.
  2578. @end deftypefun
  2579. @node System V Number Conversion
  2580. @section Old-fashioned System V number-to-string functions
  2581. The old @w{System V} C library provided three functions to convert
  2582. numbers to strings, with unusual and hard-to-use semantics. @Theglibc{}
  2583. also provides these functions and some natural extensions.
  2584. These functions are only available in @theglibc{} and on systems descended
  2585. from AT&T Unix. Therefore, unless these functions do precisely what you
  2586. need, it is better to use @code{sprintf}, which is standard.
  2587. All these functions are defined in @file{stdlib.h}.
  2588. @deftypefun {char *} ecvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2589. @standards{SVID, stdlib.h}
  2590. @standards{Unix98, stdlib.h}
  2591. @safety{@prelim{}@mtunsafe{@mtasurace{:ecvt}}@asunsafe{}@acsafe{}}
  2592. The function @code{ecvt} converts the floating-point number @var{value}
  2593. to a string with at most @var{ndigit} decimal digits. The
  2594. returned string contains no decimal point or sign. The first digit of
  2595. the string is non-zero (unless @var{value} is actually zero) and the
  2596. last digit is rounded to nearest. @code{*@var{decpt}} is set to the
  2597. index in the string of the first digit after the decimal point.
  2598. @code{*@var{neg}} is set to a nonzero value if @var{value} is negative,
  2599. zero otherwise.
  2600. If @var{ndigit} decimal digits would exceed the precision of a
  2601. @code{double} it is reduced to a system-specific value.
  2602. The returned string is statically allocated and overwritten by each call
  2603. to @code{ecvt}.
  2604. If @var{value} is zero, it is implementation defined whether
  2605. @code{*@var{decpt}} is @code{0} or @code{1}.
  2606. For example: @code{ecvt (12.3, 5, &d, &n)} returns @code{"12300"}
  2607. and sets @var{d} to @code{2} and @var{n} to @code{0}.
  2608. @end deftypefun
  2609. @deftypefun {char *} fcvt (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2610. @standards{SVID, stdlib.h}
  2611. @standards{Unix98, stdlib.h}
  2612. @safety{@prelim{}@mtunsafe{@mtasurace{:fcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2613. The function @code{fcvt} is like @code{ecvt}, but @var{ndigit} specifies
  2614. the number of digits after the decimal point. If @var{ndigit} is less
  2615. than zero, @var{value} is rounded to the @math{@var{ndigit}+1}'th place to the
  2616. left of the decimal point. For example, if @var{ndigit} is @code{-1},
  2617. @var{value} will be rounded to the nearest 10. If @var{ndigit} is
  2618. negative and larger than the number of digits to the left of the decimal
  2619. point in @var{value}, @var{value} will be rounded to one significant digit.
  2620. If @var{ndigit} decimal digits would exceed the precision of a
  2621. @code{double} it is reduced to a system-specific value.
  2622. The returned string is statically allocated and overwritten by each call
  2623. to @code{fcvt}.
  2624. @end deftypefun
  2625. @deftypefun {char *} gcvt (double @var{value}, int @var{ndigit}, char *@var{buf})
  2626. @standards{SVID, stdlib.h}
  2627. @standards{Unix98, stdlib.h}
  2628. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2629. @c gcvt calls sprintf, that ultimately calls vfprintf, which malloc()s
  2630. @c args_value if it's too large, but gcvt never exercises this path.
  2631. @code{gcvt} is functionally equivalent to @samp{sprintf(buf, "%*g",
  2632. ndigit, value)}. It is provided only for compatibility's sake. It
  2633. returns @var{buf}.
  2634. If @var{ndigit} decimal digits would exceed the precision of a
  2635. @code{double} it is reduced to a system-specific value.
  2636. @end deftypefun
  2637. As extensions, @theglibc{} provides versions of these three
  2638. functions that take @code{long double} arguments.
  2639. @deftypefun {char *} qecvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2640. @standards{GNU, stdlib.h}
  2641. @safety{@prelim{}@mtunsafe{@mtasurace{:qecvt}}@asunsafe{}@acsafe{}}
  2642. This function is equivalent to @code{ecvt} except that it takes a
  2643. @code{long double} for the first parameter and that @var{ndigit} is
  2644. restricted by the precision of a @code{long double}.
  2645. @end deftypefun
  2646. @deftypefun {char *} qfcvt (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg})
  2647. @standards{GNU, stdlib.h}
  2648. @safety{@prelim{}@mtunsafe{@mtasurace{:qfcvt}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
  2649. This function is equivalent to @code{fcvt} except that it
  2650. takes a @code{long double} for the first parameter and that @var{ndigit} is
  2651. restricted by the precision of a @code{long double}.
  2652. @end deftypefun
  2653. @deftypefun {char *} qgcvt (long double @var{value}, int @var{ndigit}, char *@var{buf})
  2654. @standards{GNU, stdlib.h}
  2655. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2656. This function is equivalent to @code{gcvt} except that it takes a
  2657. @code{long double} for the first parameter and that @var{ndigit} is
  2658. restricted by the precision of a @code{long double}.
  2659. @end deftypefun
  2660. @cindex gcvt_r
  2661. The @code{ecvt} and @code{fcvt} functions, and their @code{long double}
  2662. equivalents, all return a string located in a static buffer which is
  2663. overwritten by the next call to the function. @Theglibc{}
  2664. provides another set of extended functions which write the converted
  2665. string into a user-supplied buffer. These have the conventional
  2666. @code{_r} suffix.
  2667. @code{gcvt_r} is not necessary, because @code{gcvt} already uses a
  2668. user-supplied buffer.
  2669. @deftypefun int ecvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2670. @standards{GNU, stdlib.h}
  2671. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2672. The @code{ecvt_r} function is the same as @code{ecvt}, except
  2673. that it places its result into the user-specified buffer pointed to by
  2674. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2675. case of an error and zero otherwise.
  2676. This function is a GNU extension.
  2677. @end deftypefun
  2678. @deftypefun int fcvt_r (double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2679. @standards{SVID, stdlib.h}
  2680. @standards{Unix98, stdlib.h}
  2681. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2682. The @code{fcvt_r} function is the same as @code{fcvt}, except that it
  2683. places its result into the user-specified buffer pointed to by
  2684. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2685. case of an error and zero otherwise.
  2686. This function is a GNU extension.
  2687. @end deftypefun
  2688. @deftypefun int qecvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2689. @standards{GNU, stdlib.h}
  2690. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2691. The @code{qecvt_r} function is the same as @code{qecvt}, except
  2692. that it places its result into the user-specified buffer pointed to by
  2693. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2694. case of an error and zero otherwise.
  2695. This function is a GNU extension.
  2696. @end deftypefun
  2697. @deftypefun int qfcvt_r (long double @var{value}, int @var{ndigit}, int *@var{decpt}, int *@var{neg}, char *@var{buf}, size_t @var{len})
  2698. @standards{GNU, stdlib.h}
  2699. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  2700. The @code{qfcvt_r} function is the same as @code{qfcvt}, except
  2701. that it places its result into the user-specified buffer pointed to by
  2702. @var{buf}, with length @var{len}. The return value is @code{-1} in
  2703. case of an error and zero otherwise.
  2704. This function is a GNU extension.
  2705. @end deftypefun