Update.
[glibc.git] / manual / math.texi
blob1991bfbdb838ce510fa05b48602e7db018f2f949
1 @c We need some definitions here.
2 @ifhtml
3 @set mult ยท
4 @end ifhtml
5 @iftex
6 @set mult @cdot
7 @end iftex
8 @ifclear mult
9 @set mult x
10 @end ifclear
11 @macro mul
12 @value{mult}
13 @end macro
14 @iftex
15 @set infty @infty
16 @end iftex
17 @ifclear infty
18 @set infty oo
19 @end ifclear
20 @macro infinity
21 @value{infty}
22 @end macro
24 @node Mathematics, Arithmetic, Low-Level Terminal Interface, Top
25 @chapter Mathematics
27 This chapter contains information about functions for performing
28 mathematical computations, such as trigonometric functions.  Most of
29 these functions have prototypes declared in the header file
30 @file{math.h}.
31 @pindex math.h
33 For all functions which take a single floating-point argument and for
34 several other functions as well there are three different functions
35 available for the type @code{double}, @code{float}, and @code{long
36 double}.  The @code{double} versions of the functions are mostly defined
37 even in the @w{ISO C 89} standard.  The @code{float} and @code{long
38 double} variants are introduced in the numeric extensions for the C
39 language which are part of the @w{ISO C 9X} standard.
41 Which of the three versions of the function should be used depends on
42 the situation.  For most functions and implementation it is true that
43 speed and precision do not go together.  I.e., the @code{float} versions
44 are normally faster than the @code{double} and @code{long double}
45 versions.  On the other hand the @code{long double} version has the
46 highest precision.  One should always think about the actual needs and
47 in case of double using @code{double} is a good compromise.
50 @menu
51 * Domain and Range Errors::      Detecting overflow conditions and the like.
52 * Exceptions in Math Functions:: Signalling exception in math functions.
53 * Mathematical Constants::       Precise numeric values for often used
54                                   constant.
55 * FP Comparison Functions::      Special functions to compare floating-point
56                                   numbers.
57 * FP Function Optimizations::    Fast code or small code.
58 * Trig Functions::               Sine, cosine, and tangent.
59 * Inverse Trig Functions::       Arc sine, arc cosine, and arc tangent.
60 * Exponents and Logarithms::     Also includes square root.
61 * Hyperbolic Functions::         Hyperbolic sine and friends.
62 * Pseudo-Random Numbers::        Functions for generating pseudo-random
63                                   numbers.
64 @end menu
66 @node Domain and Range Errors
67 @section Domain and Range Errors
69 @cindex domain error
70 Many of the functions listed in this chapter are defined mathematically
71 over a domain that is only a subset of real numbers.  For example, the
72 @code{acos} function is defined over the domain between @code{@minus{}1} and
73 @code{1}.  If you pass an argument to one of these functions that is
74 outside the domain over which it is defined, the function sets
75 @code{errno} to @code{EDOM} to indicate a @dfn{domain error}.  On
76 machines that support @w{IEEE 754} floating point, functions reporting
77 error @code{EDOM} also return a NaN.
79 Some of these functions are defined mathematically to result in a
80 complex value over parts of their domains.  The most familiar example of
81 this is taking the square root of a negative number.  The functions in
82 this chapter take only real arguments and return only real values;
83 therefore, if the value ought to be nonreal, this is treated as a domain
84 error.
86 @cindex range error
87 A related problem is that the mathematical result of a function may not
88 be representable as a floating point number.  If magnitude of the
89 correct result is too large to be represented, the function sets
90 @code{errno} to @code{ERANGE} to indicate a @dfn{range error}, and
91 returns a particular very large value (named by the macro
92 @code{HUGE_VAL}) or its negation (@code{@minus{}HUGE_VAL}).
94 If the magnitude of the result is too small, a value of zero is returned
95 instead.  In this case, @code{errno} might or might not be
96 set to @code{ERANGE}.
98 The only completely reliable way to check for domain and range errors is
99 to set @code{errno} to @code{0} before you call the mathematical function
100 and test @code{errno} afterward.  As a consequence of this use of
101 @code{errno}, use of the mathematical functions is not reentrant if you
102 check for errors.
104 @c ### This is no longer true.  --drepper
105 @c None of the mathematical functions ever generates signals as a result of
106 @c domain or range errors.  In particular, this means that you won't see
107 @c @code{SIGFPE} signals generated within these functions.  (@xref{Signal
108 @c Handling}, for more information about signals.)
110 @comment math.h
111 @comment ISO
112 @deftypevr Macro double HUGE_VAL
113 An expression representing a particular very large number.  On machines
114 that use @w{IEEE 754}/@w{IEEE 854} floating point format, the value is
115 ``infinity''.  On other machines, it's typically the largest positive
116 number that can be represented.
118 The value of this macro is used as the return value from various
119 mathematical @code{double} returning functions in overflow situations.
120 @end deftypevr
122 @comment math.h
123 @comment ISO
124 @deftypevr Macro float HUGE_VALF
125 This macro is similar to the @code{HUGE_VAL} macro except that it is
126 used by functions returning @code{float} values.
128 This macro is introduced in @w{ISO C 9X}.
129 @end deftypevr
131 @comment math.h
132 @comment ISO
133 @deftypevr Macro {long double} HUGE_VALL
134 This macro is similar to the @code{HUGE_VAL} macro except that it is
135 used by functions returning @code{long double} values.  The value is
136 only different from @code{HUGE_VAL} if the architecture really supports
137 @code{long double} values.
139 This macro is introduced in @w{ISO C 9X}.
140 @end deftypevr
143 A special case is the @code{ilogb} function @pxref{Exponents and
144 Logarithms}.  Since the return value is an integer value, one cannot
145 compare with @code{HUGE_VAL} etc.  Therefore two further values are
146 defined.
148 @comment math.h
149 @comment ISO
150 @deftypevr Macro int FP_ILOGB0
151 This value is returned by @code{ilogb} if the argument is @code{0}.  The
152 numeric value is either @code{INT_MIN} or @code{-INT_MAX}.
154 This macro is introduced in @w{ISO C 9X}.
155 @end deftypevr
157 @comment math.h
158 @comment ISO
159 @deftypevr Macro int FP_ILOGBNAN
160 This value is returned by @code{ilogb} if the argument is @code{NaN}.  The
161 numeric value is either @code{INT_MIN} or @code{INT_MAX}.
163 This macro is introduced in @w{ISO C 9X}.
164 @end deftypevr
167 For more information about floating-point representations and limits,
168 see @ref{Floating Point Parameters}.  In particular, the macro
169 @code{DBL_MAX} might be more appropriate than @code{HUGE_VAL} for many
170 uses other than testing for an error in a mathematical function.
173 @node Exceptions in Math Functions
174 @section Exceptions in Math Functions
175 @cindex exception
176 @cindex signal
178 Due to the restrictions in the size of the floating-point number
179 representation or the limitation of the input range of certain functions
180 some of the mathematical operations and functions have to signal
181 exceptional situations.  The @w{IEEE 754} standard specifies which
182 exceptions have to be supported and how they can be handled.
184 @w{IEEE 754} specifies two actions for floating-point exception: taking
185 a trap or continuing without doing so.  If the trap is taken a
186 (possibly) user defined trap handler is called and this function can
187 correct the argument or react somehow else on the call.  If the trap
188 handler returns, its return value is taken as the result of the
189 operation.
191 If no trap handler is called each of the known exceptions has a default
192 action.  This consists of setting a corresponding bit in the
193 floating-point status word to indicate which kind of exception was
194 raised and to return a default value, which depends on the exception
195 (see the table below).
197 @noindent
198 The exceptions defined in @w{IEEE 754} are:
200 @table @samp
201 @item Invalid Operation
202 This exception is raised if the given operands are invalid for the
203 operation to be performed.  Examples are
204 (see @w{IEEE 754}, @w{section 7}):
205 @enumerate
206 @item
207 Any operation on a signalling NaN.
208 @item
209 Addition or subtraction; magnitude subtraction of infinities such as
210 @math{(+@infinity{}) + (-@infinity{})}.
211 @item
212 Multiplication:
213 @math{0 @mul{} @infinity{}}.
215 @item
216 Division: @math{0/0} or @math{@infinity{}/@infinity{}}.
218 @item
219 Remainder: @math{x} REM @math{y}, where @math{y} is zero or @math{x} is
220 infinite.
221 @item
222 Square root if the operand is less then zero.
223 @item
224 Conversion of an internal floating-point number to an integer or to a
225 decimal string when overflow, infinity, or NaN precludes a faithful
226 representation in that format and this cannot otherwise be signaled.
227 @item
228 Conversion of an unrecognizable input string.
229 @item
230 Comparison via predicates involving @math{<} or @math{>}, without
231 @code{?}, when the operands are @dfn{unordered}.  (@math{?>} means the
232 unordered greater relation, @xref{FP Comparison Functions}).
233 @end enumerate
235 If the exception does not cause a trap handler to be called the result
236 of the operation is taken as a quiet NaN.
238 @item Division by Zero
239 This exception is raised if the divisor is zero and the dividend is a
240 finite nonzero number.  If no trap occurs the result is either
241 @math{+@infinity{}} or @math{-@infinity{}}, depending on the
242 signs of the operands.
244 @item Overflow
245 This exception is signalled whenever the result cannot be represented
246 as a finite value in the precision format of the destination.  If no trap
247 occurs the result depends on the sign of the intermediate result and the
248 current rounding mode (@w{IEEE 754}, @w{section 7.3}):
249 @enumerate
250 @item
251 Round to nearest carries all overflows to @math{@infinity{}}
252 with the sign of the intermediate result.
253 @item
254 Round toward @math{0} carries all overflows to the precision's largest
255 finite number with the sign of the intermediate result.
256 @item
257 Round toward @math{-@infinity{}} carries positive overflows to the
258 precision's largest finite number and carries negative overflows to
259 @math{-@infinity{}}.
261 @item
262 Round toward @math{@infinity{}} carries negative overflows to the
263 precision's most negative finite number and carries positive overflows
264 to @math{@infinity{}}.
265 @end enumerate
267 @item Underflow
268 The underflow exception is created when an intermediate result is too
269 small for the operation or if the operations result rounded to the
270 destination precision causes a loss of accuracy by approximating the
271 result by denormalized numbers.
273 When no trap is installed for the underflow exception, underflow shall
274 be signaled (via the underflow flag) only when both tininess and loss of
275 accuracy have been detected.  If no trap handler is installed the
276 operation continues with an inprecise small value or zero if the
277 destination precision cannot hold the small exact result.
279 @item Inexact
280 This exception is signalled if the rounded result is not exact (such as
281 computing the square root of two) or the result overflows without an
282 overflow trap.
283 @end table
285 To control whether an exception causes a trap to occur all @w{IEEE 754}
286 conformant floating-point implementations (either hardware or software)
287 have a control word.  By setting specific bits for each exception in
288 this control word the programmer can decide whether a trap is wanted or
289 not.
291 @w{ISO C 9X} introduces a set of function which can be used to control
292 exceptions.  There are functions to manipulate the control word, to
293 query the status word or to save and restore the whole state of the
294 floating-point unit.  There are also functions to control the rounding
295 mode used.
297 @menu
298 * Status bit operations::       Manipulate the FP status word.
299 * FPU environment::             Controlling the status of the FPU.
300 * Rounding Modes::              Controlling the rounding mode.
301 @end menu
303 @node Status bit operations
304 @subsection Controlling the FPU status word
306 To control the five types of exceptions defined in @w{IEEE 754} some
307 functions are defined which abstract the interface to the FPU.  The
308 actual implementation can be very different, depending on the underlying
309 hardware or software.
311 To address the single exception the @file{fenv.h} headers defines a
312 number of macros:
314 @vtable @code
315 @comment fenv.h
316 @comment ISO
317 @item FE_INEXACT
318 Represents the inexact exception iff the FPU supports this exception.
319 @comment fenv.h
320 @comment ISO
321 @item FE_DIVBYZERO
322 Represents the divide by zero exception iff the FPU supports this exception.
323 @comment fenv.h
324 @comment ISO
325 @item FE_UNDERFLOW
326 Represents the underflow exception iff the FPU supports this exception.
327 @comment fenv.h
328 @comment ISO
329 @item FE_OVERFLOW
330 Represents the overflow exception iff the FPU supports this exception.
331 @comment fenv.h
332 @comment ISO
333 @item FE_INVALID
334 Represents the invalid exception iff the FPU supports this exception.
335 @end vtable
337 The macro @code{FE_ALL_EXCEPT} is the bitwise OR of all exception macros
338 which are supported by the FP implementation.
340 Each of the supported exception flags can either be set or unset.  The
341 @w{ISO C 9X} standard defines functions to set, unset and test the
342 status of the flags.
344 @comment fenv.h
345 @comment ISO
346 @deftypefun void feclearexcept (int @var{excepts})
347 This function clears all of the supported exception flags denoted by
348 @var{excepts} in the status word.
349 @end deftypefun
351 To safe the current status of the flags in the status word @file{fenv.h}
352 defines the type @code{fexcept_t} which can hold all the information.
353 The following function can be used to retrieve the current setting.
355 @comment fenv.h
356 @comment ISO
357 @deftypefun void fegetexceptflag (fexcept_t *@var{flagp}, int @var{excepts})
358 Store in the variable pointed to by @var{flagp} an
359 implementation-defined value representing the current setting of the
360 exception flags indicated by the parameter @var{excepts}.
361 @end deftypefun
363 @noindent
364 To restore the previously saved values one can use this function:
366 @comment fenv.h
367 @comment ISO
368 @deftypefun void fesetexceptflag (const fexcept_t *@var{flagp}, int @var{excepts})
369 Restore from the variable pointed to by @var{flagp} the setting of the
370 flags for the exceptions denoted by the value of the parameter
371 @var{excepts}.
372 @end deftypefun
374 The last function allows to query the current status of the flags.  The
375 flags can be set either explicitely (using @code{fesetexceptflag} or
376 @code{feclearexcept}) or by a floating-point operation which happened
377 before.  Since the flags are accumulative, the flags must be explicitely
378 reset using @code{feclearexcept} if one wants to test for a certain
379 exceptions raised by a specific piece of code.
381 @comment fenv.h
382 @comment ISO
383 @deftypefun int fetestexcept (int @var{excepts})
384 Test whether a subset of the flags indicated by the parameter
385 @var{except} is currently set.  If yes, a nonzero value is returned
386 which specifies which exceptions are set.  Otherwise the result is zero.
387 @end deftypefun
389 @noindent
390 Code which uses the @code{fetestexcept} function could look like this:
392 @smallexample
394   double f;
395   int raised;
396   feclearexcept (FE_ALL_EXCEPT);
397   f = compute ();
398   raised = fetestexcept (FE_OVERFLOW | FE_INVALID);
399   if (raised & FE_OVERFLOW) @{ /* ... */ @}
400   if (raised & FE_INVALID) @{ /* ... */ @}
401   /* ... */
403 @end smallexample
405 Please note that the return value of @code{fetestexcept} is @code{int}
406 but this does not mean that the @code{fexcept_t} type is generally
407 representable as an integer.  These are completely independent types.
410 @node FPU environment
411 @subsection Controlling the Floating-Point environment
413 It is sometimes necessary so save the complete status of the
414 floating-point unit for a certain time to perform some completely
415 different actions.  Beside the status of the exception flags, the
416 control word for the exceptions and the rounding mode can be saved.
418 The file @file{fenv.h} defines the type @code{fenv_t}.  The layout of a
419 variable of this type is implementation defined but the variable is able
420 to contain the complete status information.  To fill a variable of this
421 type one can use this function:
423 @comment fenv.h
424 @comment ISO
425 @deftypefun void fegetenv (fenv_t *@var{envp})
426 Store the current floating-point environment in the object pointed to by
427 @var{envp}.
428 @end deftypefun
430 @noindent
431 Another possibility which is useful in several situations is
433 @comment fenv.h
434 @comment ISO
435 @deftypefun int feholdexcept (fenv_t *@var{envp})
436 Store the current floating-point environment in the object pointed to by
437 @var{envp}.  Afterwards, all exception flags are cleared and if
438 available a mode is installed which continues on all exceptions and does
439 not cause a trap to occur.  In this case a nonzero value is returned.
441 If the floating-point implementation does not support such a non-stop
442 mode, the return value is zero.
443 @end deftypefun
445 The functions which allow a state of the floating-point unit to be
446 restored can take two kinds of arguments:
448 @itemize @bullet
449 @item
450 Pointers to @code{fenv_t} objects which were initialized previously by a
451 call to @code{fegetenv} or @code{feholdexcept}.
452 @item
453 @vindex FE_DFL_ENV
454 The special macro @code{FE_DFL_ENV} which represents the floating-point
455 environment as it was available at program start.
456 @item
457 Implementation defined macros with names starting with @code{FE_}.
459 @vindex FE_NOMASK_ENV
460 If possible, the GNU C Library defines a macro @code{FE_NOMASK_ENV}
461 which represents an environment where no exceptions are masked, so every
462 exception raised causes a trap to occur.  You can test for this macro
463 using @code{#ifdef}.
465 Some platforms might define other predefined environments.
466 @end itemize
468 @noindent
469 To set any of the environments there are two functions defined.
471 @deftypefun void fesetenv (const fenv_t *@var{envp})
472 Establish the floating-point environment described in the object pointed
473 to by @var{envp}.  Even if one or more exceptions flags in the restored
474 environment are set no exception is raised.
475 @end deftypefun
477 In some situations the previous status of the exception flags must not
478 simply be discarded and so this function is useful:
480 @deftypefun void feupdateenv (const fenv_t *@var{envp})
481 The current status of the floating-point unit is preserved in some
482 automatic storage before the environment described by the object pointed
483 to by @var{envp} is installed.  Once this is finished all exceptions set
484 in the original environment which is saved in the automatic storage, is
485 raised.
486 @end deftypefun
488 This function can be used to execute a part of the program with an
489 environment which masks all exceptions and before switching back remove
490 unwanted exception and raise the remaining exceptions.
493 @node Rounding Modes
494 @subsection Rounding modes of the Floating-Point Unit
496 @w{IEEE 754} defines four different rounding modes.  If the rounding
497 mode is supported by the floating-point implementation the corresponding
498 of the following macros is defined:
500 @table @code
501 @comment fenv.h
502 @comment ISO
503 @vindex FE_TONEAREST
504 @item FE_TONEAREST
505 Round to nearest.  This is the default mode and should always be used
506 except when a different mode is explicitely required.  Only rounding to
507 nearest guarantees numeric stability of the computations.
509 @comment fenv.h
510 @comment ISO
511 @vindex FE_UPWARD
512 @item FE_UPWARD
513 Round toward @math{+@infinity{}}.
515 @comment fenv.h
516 @comment ISO
517 @vindex FE_DOWNWARD
518 @item FE_DOWNWARD
519 Round toward @math{-@infinity{}}.
521 @comment fenv.h
522 @comment ISO
523 @vindex FE_TOWARDZERO
524 @item FE_TOWARDZERO
525 Round toward zero.
526 @end table
528 At any time one of the above four rounding modes is selected.  To get
529 information about the currently selected mode one can use this function:
531 @comment fenv.h
532 @comment ISO
533 @deftypefun int fegetround (void)
534 Return the currently selected rounding mode, represented by one of the
535 values of the defined rounding mode macros.
536 @end deftypefun
538 @noindent
539 To set a specific rounding mode the next function can be used.
541 @comment fenv.h
542 @comment ISO
543 @deftypefun int fesetround (int @var{round})
544 Change the currently selected rounding mode to the mode described by the
545 parameter @var{round}.  If @var{round} does not correspond to one of the
546 supported rounding modes nothing is changed.
548 The function returns a nonzero value iff the requested rounding mode can
549 be established.  Otherwise zero is returned.
550 @end deftypefun
552 Changing the rounding mode might be necessary for various reasons.  But
553 changing the mode only to round a given number normally is no good idea.
554 The standard defines a set of functions which can be used to round an
555 argument according to some rules and for all of the rounding modes there
556 is a corresponding function.
558 If a large set of number has to be rounded it might be good to change
559 the rounding mode and to not use the function the library provides.  So
560 the perhaps necessary switching of the rounding mode in the library
561 function can be avoided.  But since not all rounding modes are
562 guaranteed to exist on any platform this possible implementation cannot
563 be portably used.  A default method has to be implemented as well.
566 @node Mathematical Constants
567 @section Predefined Mathematical Constants
568 @cindex constants
569 @cindex mathematical constants
571 The header @file{math.h} defines a series of mathematical constants if
572 @code{_BSD_SOURCE} or a more general feature select macro is defined
573 before including this file.  All values are defined as preprocessor
574 macros starting with @code{M_}.  The collection includes:
576 @vtable @code
577 @item M_E
578 The value is that of the base of the natural logarithm.
579 @item M_LOG2E
580 The value is computed as the logarithm to base @code{2} of @code{M_E}.
581 @item M_LOG10E
582 The value is computed as the logarithm to base @code{10} of @code{M_E}.
583 @item M_LN2
584 The value is computed as the natural logarithm of @code{2}.
585 @item M_LN10
586 The value is computed as the natural logarithm of @code{10}.
587 @item M_PI
588 The value is those of the number pi.
589 @item M_PI_2
590 The value is those of the number pi divided by two.
591 @item M_PI_4
592 The value is those of the number pi divided by four.
593 @item M_1_PI
594 The value is the reziprocal of the value of the number pi.
595 @item M_2_PI
596 The value is two times the reziprocal of the value of the number pi.
597 @item M_2_SQRTPI
598 The value is two times the reziprocal of the square root of the number pi.
599 @item M_SQRT2
600 The value is the square root of the value of the number pi.
601 @item M_SQRT1_2
602 The value is the reziprocal of the square root of the value of the number pi.
603 @end vtable
605 All values are defined as @code{long double} values unless the compiler
606 does not support this type or @code{__STDC__} is not defined (both is
607 unlikely).  Historically the numbers were @code{double} values and some
608 old code still relies on this so you might want to add explicit casts if
609 the extra precision of the @code{long double} value is not needed.  One
610 critical case are functions with a variable number of arguments, such as
611 @code{printf}.
613 @vindex PI
614 @emph{Note:} Some programs use a constant named @code{PI} which has the
615 same value as @code{M_PI}.  This probably derives from Stroustroup's
616 book about his C++ programming language where this value is used in
617 examples (and perhaps some AT&T headers contain this value).  But due to
618 possible name space problems (@code{PI} is a quite frequently used name)
619 this value is not added to @file{math.h}.  Every program should use
620 @code{M_PI} instead or add on the compiler command line
621 @code{-DPI=M_PI}.
624 @node FP Comparison Functions
625 @section Floating-Point Comparison Functions
626 @cindex unordered comparison
628 The @w{IEEE 754} standards defines a set of functions which allows to
629 compare even those numbers which normally would cause an exception to be
630 raised since they are unordered.  E.g., the expression
632 @smallexample
633 int v = a < 1.0;
634 @end smallexample
636 @noindent
637 would raise an exception if @var{a} would be a NaN.  Functions to
638 compare unordered numbers are part of the FORTRAN language for a long
639 time and the extensions in @w{ISO C 9X} finally introduce them as well
640 for the C programming language.
642 All of the operations are implemented as macros which allow their
643 arguments to be of either @code{float}, @code{double}, or @code{long
644 double} type.
646 @comment math.h
647 @comment ISO
648 @deftypefn {Macro} int isgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
649 This macro determines whether the argument @var{x} is greater than
650 @var{y}.  This is equivalent to @code{(@var{x}) > (@var{y})} but no
651 exception is raised if @var{x} or @var{y} are unordered.
652 @end deftypefn
654 @comment math.h
655 @comment ISO
656 @deftypefn {Macro} int isgreaterequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
657 This macro determines whether the argument @var{x} is greater than or
658 equal to @var{y}.  This is equivalent to @code{(@var{x}) >= (@var{y})} but no
659 exception is raised if @var{x} or @var{y} are unordered.
660 @end deftypefn
662 @comment math.h
663 @comment ISO
664 @deftypefn {Macro} int isless (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
665 This macro determines whether the argument @var{x} is less than @var{y}.
666 This is equivalent @code{(@var{x}) < (@var{y})} but no exception is raised if
667 @var{x} or @var{y} are unordered.
668 @end deftypefn
670 @comment math.h
671 @comment ISO
672 @deftypefn {Macro} int islessequal (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
673 This macro determines whether the argument @var{x} is less than or equal
674 to @var{y}.  This is equivalent to @code{(@var{x}) <= (@var{y})} but no
675 exception is raised if @var{x} or @var{y} are unordered.
676 @end deftypefn
678 @comment math.h
679 @comment ISO
680 @deftypefn {Macro} int islessgreater (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
681 This macro determines whether the argument @var{x} is less or greater
682 than @var{y}.  This is equivalent to @code{(@var{x}) < (@var{y}) ||
683 (@var{x}) > (@var{y})} (except that @var{x} and @var{y} are only
684 evaluated once) but no exception is raised if @var{x} or @var{y} are
685 unordered.
686 @end deftypefn
688 @comment math.h
689 @comment ISO
690 @deftypefn {Macro} int isunordered (@emph{real-floating} @var{x}, @emph{real-floating} @var{y})
691 This macro determines whether its arguments are unordered.
692 @end deftypefn
694 All the macros are defined in a way to ensure that both arguments are
695 evaluated exactly once and so they can be used exactly like the builtin
696 operators.
698 On several platform these macros are mapped to efficient instructions
699 the processor understands.  But on machines missing these functions, the
700 macros above might be rather slow.  So it is best to use the builtin
701 operators unless it is necessary to use unordered comparisons.
703 @strong{Note:} There are no macros @code{isequal} or @code{isunequal}.
704 These macros are not necessary since the @w{IEEE 754} standard requires
705 that the comparison for equality and unequality do @emph{not} throw an
706 exception if one of the arguments is an unordered value.
709 @node FP Function Optimizations
710 @section Is Fast Code or Small Code preferred?
711 @cindex Optimization
713 If an application uses many floating point function it is often the case
714 that the costs for the function calls itselfs are not neglectable.
715 Modern processor implementation often can execute the operation itself
716 very fast but the call means a disturbance of the control flow.
718 For this reason the GNU C Library provides optimizations for many of the
719 frequently used math functions.  When the GNU CC is used and the user
720 activates the optimizer several new inline functions and macros get
721 defined.  These new functions and macros have the same names as the
722 library function and so get used instead of the later.  In case of
723 inline functions the compiler will decide whether it is reasonable to
724 use the inline function and this decision is usually correct.
726 For the generated code this means that no calls to the library functions
727 are necessary.  This increases the speed significantly.  But the
728 drawback is that the code size increases and this increase is not always
729 neglectable.
731 In cases where the inline functions and macros are not wanted the symbol
732 @code{__NO_MATH_INLINES} should be defined before any system header is
733 included.  This will make sure only library functions are used.  Of
734 course it can be determined for each single file in the project whether
735 giving this option is preferred or not.
738 @node Trig Functions
739 @section Trigonometric Functions
740 @cindex trigonometric functions
742 These are the familiar @code{sin}, @code{cos}, and @code{tan} functions.
743 The arguments to all of these functions are in units of radians; recall
744 that pi radians equals 180 degrees.
746 @cindex pi (trigonometric constant)
747 The math library does define a symbolic constant for pi in @file{math.h}
748 (@pxref{Mathematical Constants}) when BSD compliance is required
749 (@pxref{Feature Test Macros}).  In case it is not possible to use this
750 predefined macro one easily can define it:
752 @smallexample
753 #define M_PI 3.14159265358979323846264338327
754 @end smallexample
756 @noindent
757 You can also compute the value of pi with the expression @code{acos
758 (-1.0)}.
761 @comment math.h
762 @comment ISO
763 @deftypefun double sin (double @var{x})
764 @deftypefunx float sinf (float @var{x})
765 @deftypefunx {long double} sinl (long double @var{x})
766 These functions return the sine of @var{x}, where @var{x} is given in
767 radians.  The return value is in the range @code{-1} to @code{1}.
768 @end deftypefun
770 @comment math.h
771 @comment ISO
772 @deftypefun double cos (double @var{x})
773 @deftypefunx float cosf (float @var{x})
774 @deftypefunx {long double} cosl (long double @var{x})
775 These functions return the cosine of @var{x}, where @var{x} is given in
776 radians.  The return value is in the range @code{-1} to @code{1}.
777 @end deftypefun
779 @comment math.h
780 @comment ISO
781 @deftypefun double tan (double @var{x})
782 @deftypefunx float tanf (float @var{x})
783 @deftypefunx {long double} tanl (long double @var{x})
784 These functions return the tangent of @var{x}, where @var{x} is given in
785 radians.
787 The following @code{errno} error conditions are defined for this function:
789 @table @code
790 @item ERANGE
791 Mathematically, the tangent function has singularities at odd multiples
792 of pi/2.  If the argument @var{x} is too close to one of these
793 singularities, @code{tan} sets @code{errno} to @code{ERANGE} and returns
794 either positive or negative @code{HUGE_VAL}.
795 @end table
796 @end deftypefun
798 In many applications where @code{sin} and @code{cos} are used, the value
799 for the same argument of both of these functions is used at the same
800 time.  Since the algorithm to compute these values is very similar for
801 both functions there is an additional function which computes both values
802 at the same time.
804 @comment math.h
805 @comment GNU
806 @deftypefun void sincos (double @var{x}, double *@var{sinx}, double *@var{cosx})
807 @deftypefunx void sincosf (float @var{x}, float *@var{sinx}, float *@var{cosx})
808 @deftypefunx void sincosl (long double @var{x}, long double *@var{sinx}, long double *@var{cosx})
809 These functions return the sine of @var{x} in @code{*@var{sinx}} and the
810 cosine of @var{x} in @code{*@var{cos}}, where @var{x} is given in
811 radians.  Both values, @code{*@var{sinx}} and @code{*@var{cosx}}, are in
812 the range of @code{-1} to @code{1}.
814 This function is a GNU extension.  It should be used whenever both sine
815 and cosine are needed but in portable applications there should be a
816 fallback method for systems without this function.
817 @end deftypefun
819 @cindex complex trigonometric functions
821 The trigonometric functions are in mathematics not only defined on real
822 numbers.  They can be extended to complex numbers and the @w{ISO C 9X}
823 standard introduces these variants in the standard math library.
825 @comment complex.h
826 @comment ISO
827 @deftypefun {complex double} csin (complex double @var{z})
828 @deftypefunx {complex float} csinf (complex float @var{z})
829 @deftypefunx {complex long double} csinl (complex long double @var{z})
830 These functions return the complex sine of the complex value in @var{z}.
831 The mathematical definition of the complex sine is
833 @ifinfo
834 @math{sin (z) = 1/(2*i) * (exp (z*i) - exp (-z*i))}.
835 @end ifinfo
836 @iftex
837 @tex
838 $$\sin(z) = {1\over 2i} (e^{zi} - e^{-zi})$$
839 @end tex
840 @end iftex
841 @end deftypefun
843 @comment complex.h
844 @comment ISO
845 @deftypefun {complex double} ccos (complex double @var{z})
846 @deftypefunx {complex float} ccosf (complex float @var{z})
847 @deftypefunx {complex long double} ccosl (complex long double @var{z})
848 These functions return the complex cosine of the complex value in @var{z}.
849 The mathematical definition of the complex cosine is
851 @ifinfo
852 @math{cos (z) = 1/2 * (exp (z*i) + exp (-z*i))}
853 @end ifinfo
854 @iftex
855 @tex
856 $$\cos(z) = {1\over 2} (e^{zi} + e^{-zi})$$
857 @end tex
858 @end iftex
859 @end deftypefun
861 @comment complex.h
862 @comment ISO
863 @deftypefun {complex double} ctan (complex double @var{z})
864 @deftypefunx {complex float} ctanf (complex float @var{z})
865 @deftypefunx {complex long double} ctanl (complex long double @var{z})
866 These functions return the complex tangent of the complex value in @var{z}.
867 The mathematical definition of the complex tangent is
869 @ifinfo
870 @math{tan (z) = 1/i * (exp (z*i) - exp (-z*i)) / (exp (z*i) + exp (-z*i))}
871 @end ifinfo
872 @iftex
873 @tex
874 $$\tan(z) = {1\over i} {e^{zi} - e^{-zi}\over e^{zi} + e^{-zi}}$$
875 @end tex
876 @end iftex
877 @end deftypefun
880 @node Inverse Trig Functions
881 @section Inverse Trigonometric Functions
882 @cindex inverse trigonometric functions
884 These are the usual arc sine, arc cosine and arc tangent functions,
885 which are the inverses of the sine, cosine and tangent functions,
886 respectively.
888 @comment math.h
889 @comment ISO
890 @deftypefun double asin (double @var{x})
891 @deftypefunx float asinf (float @var{x})
892 @deftypefunx {long double} asinl (long double @var{x})
893 These functions compute the arc sine of @var{x}---that is, the value whose
894 sine is @var{x}.  The value is in units of radians.  Mathematically,
895 there are infinitely many such values; the one actually returned is the
896 one between @code{-pi/2} and @code{pi/2} (inclusive).
898 @code{asin} fails, and sets @code{errno} to @code{EDOM}, if @var{x} is
899 out of range.  The arc sine function is defined mathematically only
900 over the domain @code{-1} to @code{1}.
901 @end deftypefun
903 @comment math.h
904 @comment ISO
905 @deftypefun double acos (double @var{x})
906 @deftypefunx float acosf (float @var{x})
907 @deftypefunx {long double} acosl (long double @var{x})
908 These functions compute the arc cosine of @var{x}---that is, the value
909 whose cosine is @var{x}.  The value is in units of radians.
910 Mathematically, there are infinitely many such values; the one actually
911 returned is the one between @code{0} and @code{pi} (inclusive).
913 @code{acos} fails, and sets @code{errno} to @code{EDOM}, if @var{x} is
914 out of range.  The arc cosine function is defined mathematically only
915 over the domain @code{-1} to @code{1}.
916 @end deftypefun
919 @comment math.h
920 @comment ISO
921 @deftypefun double atan (double @var{x})
922 @deftypefunx float atanf (float @var{x})
923 @deftypefunx {long double} atanl (long double @var{x})
924 These functions compute the arc tangent of @var{x}---that is, the value
925 whose tangent is @var{x}.  The value is in units of radians.
926 Mathematically, there are infinitely many such values; the one actually
927 returned is the one between @code{-pi/2} and @code{pi/2}
928 (inclusive).
929 @end deftypefun
931 @comment math.h
932 @comment ISO
933 @deftypefun double atan2 (double @var{y}, double @var{x})
934 @deftypefunx float atan2f (float @var{y}, float @var{x})
935 @deftypefunx {long double} atan2l (long double @var{y}, long double @var{x})
936 This is the two argument arc tangent function.  It is similar to computing
937 the arc tangent of @var{y}/@var{x}, except that the signs of both arguments
938 are used to determine the quadrant of the result, and @var{x} is
939 permitted to be zero.  The return value is given in radians and is in
940 the range @code{-pi} to @code{pi}, inclusive.
942 If @var{x} and @var{y} are coordinates of a point in the plane,
943 @code{atan2} returns the signed angle between the line from the origin
944 to that point and the x-axis.  Thus, @code{atan2} is useful for
945 converting Cartesian coordinates to polar coordinates.  (To compute the
946 radial coordinate, use @code{hypot}; see @ref{Exponents and
947 Logarithms}.)
949 The function @code{atan2} sets @code{errno} to @code{EDOM} if both
950 @var{x} and @var{y} are zero; the return value is not defined in this
951 case.
952 @end deftypefun
954 @cindex inverse complex trigonometric functions
956 The inverse trigonometric functions also exist is separate versions
957 which are usable with complex numbers.
959 @comment complex.h
960 @comment ISO
961 @deftypefun {complex double} casin (complex double @var{z})
962 @deftypefunx {complex float} casinf (complex float @var{z})
963 @deftypefunx {complex long double} casinl (complex long double @var{z})
964 These functions compute the complex arc sine of @var{z}---that is, the
965 value whose sine is @var{z}.  The value is in units of radians.
967 Unlike the real version of the arc sine function @code{casin} has no
968 limitation on the argument @var{z}.
969 @end deftypefun
971 @comment complex.h
972 @comment ISO
973 @deftypefun {complex double} cacos (complex double @var{z})
974 @deftypefunx {complex float} cacosf (complex float @var{z})
975 @deftypefunx {complex long double} cacosl (complex long double @var{z})
976 These functions compute the complex arc cosine of @var{z}---that is, the
977 value whose cosine is @var{z}.  The value is in units of radians.
979 Unlike the real version of the arc cosine function @code{cacos} has no
980 limitation on the argument @var{z}.
981 @end deftypefun
984 @comment complex.h
985 @comment ISO
986 @deftypefun {complex double} catan (complex double @var{z})
987 @deftypefunx {complex float} catanf (complex float @var{z})
988 @deftypefunx {complex long double} catanl (complex long double @var{z})
989 These functions compute the complex arc tangent of @var{z}---that is,
990 the value whose tangent is @var{z}.  The value is in units of radians.
991 @end deftypefun
994 @node Exponents and Logarithms
995 @section Exponentiation and Logarithms
996 @cindex exponentiation functions
997 @cindex power functions
998 @cindex logarithm functions
1000 @comment math.h
1001 @comment ISO
1002 @deftypefun double exp (double @var{x})
1003 @deftypefunx float expf (float @var{x})
1004 @deftypefunx {long double} expl (long double @var{x})
1005 These functions return the value of @code{e} (the base of natural
1006 logarithms) raised to power @var{x}.
1008 The function fails, and sets @code{errno} to @code{ERANGE}, if the
1009 magnitude of the result is too large to be representable.
1010 @end deftypefun
1012 @comment math.h
1013 @comment ISO
1014 @deftypefun double exp2 (double @var{x})
1015 @deftypefunx float exp2f (float @var{x})
1016 @deftypefunx {long double} exp2l (long double @var{x})
1017 These functions return the value of @code{2} raised to the power @var{x}.
1018 Mathematically, @code{exp2 (x)} is the same as @code{exp (x * log (2))}.
1020 The function fails, and sets @code{errno} to @code{ERANGE}, if the
1021 magnitude of the result is too large to be representable.
1022 @end deftypefun
1024 @comment math.h
1025 @comment GNU
1026 @deftypefun double exp10 (double @var{x})
1027 @deftypefunx float exp10f (float @var{x})
1028 @deftypefunx {long double} exp10l (long double @var{x})
1029 @deftypefunx double pow10 (double @var{x})
1030 @deftypefunx float pow10f (float @var{x})
1031 @deftypefunx {long double} pow10l (long double @var{x})
1032 These functions return the value of @code{10} raised to the power @var{x}.
1033 Mathematically, @code{exp2 (x)} is the same as @code{exp (x * log (2))}.
1035 The function fails, and sets @code{errno} to @code{ERANGE}, if the
1036 magnitude of the result is too large to be representable.
1038 All these functions are GNU extensions.  The name @code{pow10} is used
1039 in some old code but the name @code{exp10} clearly is more in the sense
1040 of the ISO library designers and therefore should probably be preferred.
1041 @end deftypefun
1044 @comment math.h
1045 @comment ISO
1046 @deftypefun double log (double @var{x})
1047 @deftypefunx float logf (float @var{x})
1048 @deftypefunx {long double} logl (long double @var{x})
1049 These functions return the natural logarithm of @var{x}.  @code{exp (log
1050 (@var{x}))} equals @var{x}, exactly in mathematics and approximately in
1053 The following @code{errno} error conditions are defined for this function:
1055 @table @code
1056 @item EDOM
1057 The argument @var{x} is negative.  The log function is defined
1058 mathematically to return a real result only on positive arguments.
1060 @item ERANGE
1061 The argument is zero.  The log of zero is not defined.
1062 @end table
1063 @end deftypefun
1065 @comment math.h
1066 @comment ISO
1067 @deftypefun double log10 (double @var{x})
1068 @deftypefunx float log10f (float @var{x})
1069 @deftypefunx {long double} log10l (long double @var{x})
1070 These functions return the base-10 logarithm of @var{x}.  Except for the
1071 different base, it is similar to the @code{log} function.  In fact,
1072 @code{log10 (@var{x})} equals @code{log (@var{x}) / log (10)}.
1073 @end deftypefun
1075 @comment math.h
1076 @comment ISO
1077 @deftypefun double log2 (double @var{x})
1078 @deftypefunx float log2f (float @var{x})
1079 @deftypefunx {long double} log2l (long double @var{x})
1080 These functions return the base-2 logarithm of @var{x}.  Except for the
1081 different base, it is similar to the @code{log} function.  In fact,
1082 @code{log2 (@var{x})} equals @code{log (@var{x}) / log (2)}.
1083 @end deftypefun
1085 @comment math.h
1086 @comment ISO
1087 @deftypefun double logb (double @var{x})
1088 @deftypefunx float logbf (float @var{x})
1089 @deftypefunx {long double} logbl (long double @var{x})
1090 These functions extract the exponent of @var{x} and return it as a
1091 signed integer value.  If @var{x} is zero, a range error may occur.
1093 A special case are subnormal numbers (if supported by the floating-point
1094 format).  The exponent returned is not the actual value from @var{x}.
1095 Instead the number is first normalized as if the range of the exponent
1096 field is large enough.
1097 @end deftypefun
1099 @comment math.h
1100 @comment ISO
1101 @deftypefun int ilogb (double @var{x})
1102 @deftypefunx int ilogbf (float @var{x})
1103 @deftypefunx int ilogbl (long double @var{x})
1104 These functions are equivalent to the corresponding @code{logb}
1105 functions except that the values are returned as signed integer values.
1106 Since integer values cannot represent infinity and NaN, there are some
1107 special symbols defined to help detect these situations.
1109 @vindex FP_ILOGB0
1110 @vindex FP_ILOGBNAN
1111 @code{ilogb} returns @code{FP_ILOGB0} if @var{x} is @code{0} and it
1112 returns @code{FP_ILOGBNAN} if @var{x} is @code{NaN}.  These values are
1113 system specific and no fixed value is assigned.  More concrete, these
1114 values might even have the same value.  So a piece of code handling the
1115 result of @code{ilogb} could look like this:
1117 @smallexample
1118 i = ilogb (f);
1119 if (i == FP_ILOGB0 || i == FP_ILOGBNAN)
1120   @{
1121     if (isnan (f))
1122       @{
1123         /* @r{Handle NaN.}  */
1124       @}
1125     else if (f  == 0.0)
1126       @{
1127         /* @r{Handle 0.0.}  */
1128       @}
1129     else
1130       @{
1131         /* @r{Some other value with large exponent,}
1132            @r{perhaps +Inf.}  */
1133       @}
1134   @}
1135 @end smallexample
1137 @end deftypefun
1139 @comment math.h
1140 @comment ISO
1141 @deftypefun double pow (double @var{base}, double @var{power})
1142 @deftypefunx float powf (float @var{base}, float @var{power})
1143 @deftypefunx {long double} powl (long double @var{base}, long double @var{power})
1144 These are general exponentiation functions, returning @var{base} raised
1145 to @var{power}.
1147 @need 250
1148 The following @code{errno} error conditions are defined for this function:
1150 @table @code
1151 @item EDOM
1152 The argument @var{base} is negative and @var{power} is not an integral
1153 value.  Mathematically, the result would be a complex number in this case.
1155 @item ERANGE
1156 An underflow or overflow condition was detected in the result.
1157 @end table
1158 @end deftypefun
1160 @cindex square root function
1161 @comment math.h
1162 @comment ISO
1163 @deftypefun double sqrt (double @var{x})
1164 @deftypefunx float sqrtf (float @var{x})
1165 @deftypefunx {long double} sqrtl (long double @var{x})
1166 These functions return the nonnegative square root of @var{x}.
1168 The @code{sqrt} function fails, and sets @code{errno} to @code{EDOM}, if
1169 @var{x} is negative.  Mathematically, the square root would be a complex
1170 number.
1171 @c (@pxref{csqrt})
1172 @end deftypefun
1174 @cindex cube root function
1175 @comment math.h
1176 @comment BSD
1177 @deftypefun double cbrt (double @var{x})
1178 @deftypefunx float cbrtf (float @var{x})
1179 @deftypefunx {long double} cbrtl (long double @var{x})
1180 These functions return the cube root of @var{x}.  They cannot
1181 fail; every representable real value has a representable real cube root.
1182 @end deftypefun
1184 @comment math.h
1185 @comment ISO
1186 @deftypefun double hypot (double @var{x}, double @var{y})
1187 @deftypefunx float hypotf (float @var{x}, float @var{y})
1188 @deftypefunx {long double} hypotl (long double @var{x}, long double @var{y})
1189 These functions return @code{sqrt (@var{x}*@var{x} +
1190 @var{y}*@var{y})}.  (This is the length of the hypotenuse of a right
1191 triangle with sides of length @var{x} and @var{y}, or the distance
1192 of the point (@var{x}, @var{y}) from the origin.)  Using this function
1193 instead of the direct formula is highly appreciated since the error is
1194 much smaller.  See also the function @code{cabs} in @ref{Absolute Value}.
1195 @end deftypefun
1197 @comment math.h
1198 @comment ISO
1199 @deftypefun double expm1 (double @var{x})
1200 @deftypefunx float expm1f (float @var{x})
1201 @deftypefunx {long double} expm1l (long double @var{x})
1202 These functions return a value equivalent to @code{exp (@var{x}) - 1}.
1203 It is computed in a way that is accurate even if the value of @var{x} is
1204 near zero---a case where @code{exp (@var{x}) - 1} would be inaccurate due
1205 to subtraction of two numbers that are nearly equal.
1206 @end deftypefun
1208 @comment math.h
1209 @comment ISO
1210 @deftypefun double log1p (double @var{x})
1211 @deftypefunx float log1pf (float @var{x})
1212 @deftypefunx {long double} log1pl (long double @var{x})
1213 This function returns a value equivalent to @w{@code{log (1 + @var{x})}}.
1214 It is computed in a way that is accurate even if the value of @var{x} is
1215 near zero.
1216 @end deftypefun
1218 @cindex complex exponentiation functions
1219 @cindex complex logarithm functions
1221 @w{ISO C 9X} defines variants of some of the exponentiation and
1222 logarithm functions.  As for the other functions handling complex
1223 numbers these functions are perhaps better optimized and provide better
1224 error checking than a direct use of the formulas of the mathematical
1225 definition.
1227 @comment complex.h
1228 @comment ISO
1229 @deftypefun {complex double} cexp (complex double @var{z})
1230 @deftypefunx {complex float} cexpf (complex float @var{z})
1231 @deftypefunx {complex long double} cexpl (complex long double @var{z})
1232 These functions return the value of @code{e} (the base of natural
1233 logarithms) raised to power of the complex value @var{z}.
1235 @noindent
1236 Mathematically this corresponds to the value
1238 @ifinfo
1239 @math{exp (z) = exp (creal (z)) * (cos (cimag (z)) + I * sin (cimag (z)))}
1240 @end ifinfo
1241 @iftex
1242 @tex
1243 $$\exp(z) = e^z = e^{{\rm Re} z} (\cos ({\rm Im} z) + i \sin ({\rm Im} z))$$
1244 @end tex
1245 @end iftex
1246 @end deftypefun
1248 @comment complex.h
1249 @comment ISO
1250 @deftypefun {complex double} clog (complex double @var{z})
1251 @deftypefunx {complex float} clogf (complex float @var{z})
1252 @deftypefunx {complex long double} clogl (complex long double @var{z})
1253 These functions return the natural logarithm of the complex value
1254 @var{z}.  Unlike the real value version @code{log} and its variants,
1255 @code{clog} has no limit for the range of its argument @var{z}.
1257 @noindent
1258 Mathematically this corresponds to the value
1260 @ifinfo
1261 @math{log (z) = log (cabs (z)) + I * carg (z)}
1262 @end ifinfo
1263 @iftex
1264 @tex
1265 $$\log(z) = \log(|z|) + i \arg(z)$$
1266 @end tex
1267 @end iftex
1268 @end deftypefun
1271 @comment complex.h
1272 @comment GNU
1273 @deftypefun {complex double} clog10 (complex double @var{z})
1274 @deftypefunx {complex float} clog10f (complex float @var{z})
1275 @deftypefunx {complex long double} clog10l (complex long double @var{z})
1276 These functions return the base 10 logarithm of the complex value
1277 @var{z}.  Unlike the real value version @code{log} and its variants,
1278 @code{clog} has no limit for the range of its argument @var{z}.
1280 @noindent
1281 Mathematically this corresponds to the value
1283 @ifinfo
1284 @math{log (z) = log10 (cabs (z)) + I * carg (z)}
1285 @end ifinfo
1286 @iftex
1287 @tex
1288 $$\log_{10}(z) = \log_{10}(|z|) + i \arg(z)$$
1289 @end tex
1290 @end iftex
1292 This function is a GNU extension.
1293 @end deftypefun
1295 @comment complex.h
1296 @comment ISO
1297 @deftypefun {complex double} csqrt (complex double @var{z})
1298 @deftypefunx {complex float} csqrtf (complex float @var{z})
1299 @deftypefunx {complex long double} csqrtl (complex long double @var{z})
1300 These functions return the complex root of the argument @var{z}.  Unlike
1301 the @code{sqrt} function these functions do not have any restriction on
1302 the value of the argument.
1303 @end deftypefun
1305 @comment complex.h
1306 @comment ISO
1307 @deftypefun {complex double} cpow (complex double @var{base}, complex double @var{power})
1308 @deftypefunx {complex float} cpowf (complex float @var{base}, complex float @var{power})
1309 @deftypefunx {complex long double} cpowl (complex long double @var{base}, complex long double @var{power})
1310 These functions return the complex value @var{base} raised to the power of
1311 @var{power}.  This is computed as
1313 @ifinfo
1314 @math{cpow (x, y) = cexp (y * clog (x))}
1315 @end ifinfo
1316 @iftex
1317 @tex
1318 $${\rm cpow}(x, y) = e^{y \log(x)}$$
1319 @end tex
1320 @end iftex
1321 @end deftypefun
1324 @node Hyperbolic Functions
1325 @section Hyperbolic Functions
1326 @cindex hyperbolic functions
1328 The functions in this section are related to the exponential functions;
1329 see @ref{Exponents and Logarithms}.
1331 @comment math.h
1332 @comment ISO
1333 @deftypefun double sinh (double @var{x})
1334 @deftypefunx float sinhf (float @var{x})
1335 @deftypefunx {long double} sinhl (long double @var{x})
1336 These functions return the hyperbolic sine of @var{x}, defined
1337 mathematically as @w{@code{(exp (@var{x}) - exp (-@var{x})) / 2}}.  The
1338 function fails, and sets @code{errno} to @code{ERANGE}, if the value of
1339 @var{x} is too large; that is, if overflow occurs.
1340 @end deftypefun
1342 @comment math.h
1343 @comment ISO
1344 @deftypefun double cosh (double @var{x})
1345 @deftypefunx float coshf (float @var{x})
1346 @deftypefunx {long double} coshl (long double @var{x})
1347 These function return the hyperbolic cosine of @var{x},
1348 defined mathematically as @w{@code{(exp (@var{x}) + exp (-@var{x})) / 2}}.
1349 The function fails, and sets @code{errno} to @code{ERANGE}, if the value
1350 of @var{x} is too large; that is, if overflow occurs.
1351 @end deftypefun
1353 @comment math.h
1354 @comment ISO
1355 @deftypefun double tanh (double @var{x})
1356 @deftypefunx float tanhf (float @var{x})
1357 @deftypefunx {long double} tanhl (long double @var{x})
1358 These functions return the hyperbolic tangent of @var{x}, whose
1359 mathematical definition is @w{@code{sinh (@var{x}) / cosh (@var{x})}}.
1360 @end deftypefun
1362 @cindex hyperbolic functions
1364 There are counterparts for these hyperbolic functions which work with
1365 complex valued arguments.  They should always be used instead of the
1366 obvious mathematical formula since the implementations in the math
1367 library are optimized for accuracy and speed.
1369 @comment complex.h
1370 @comment ISO
1371 @deftypefun {complex double} csinh (complex double @var{z})
1372 @deftypefunx {complex float} csinhf (complex float @var{z})
1373 @deftypefunx {complex long double} csinhl (complex long double @var{z})
1374 These functions return the complex hyperbolic sine of @var{z}, defined
1375 mathematically as @w{@code{(exp (@var{z}) - exp (-@var{z})) / 2}}.  The
1376 function fails, and sets @code{errno} to @code{ERANGE}, if the value of
1377 result is too large.
1378 @end deftypefun
1380 @comment complex.h
1381 @comment ISO
1382 @deftypefun {complex double} ccosh (complex double @var{z})
1383 @deftypefunx {complex float} ccoshf (complex float @var{z})
1384 @deftypefunx {complex long double} ccoshl (complex long double @var{z})
1385 These functions return the complex hyperbolic cosine of @var{z}, defined
1386 mathematically as @w{@code{(exp (@var{z}) + exp (-@var{z})) / 2}}.  The
1387 function fails, and sets @code{errno} to @code{ERANGE}, if the value of
1388 result is too large.
1389 @end deftypefun
1391 @comment complex.h
1392 @comment ISO
1393 @deftypefun {complex double} ctanh (complex double @var{z})
1394 @deftypefunx {complex float} ctanhf (complex float @var{z})
1395 @deftypefunx {complex long double} ctanhl (complex long double @var{z})
1396 These functions return the complex hyperbolic tangent of @var{z}, whose
1397 mathematical definition is @w{@code{csinh (@var{z}) / ccosh (@var{z})}}.
1398 @end deftypefun
1401 @cindex inverse hyperbolic functions
1403 @comment math.h
1404 @comment ISO
1405 @deftypefun double asinh (double @var{x})
1406 @deftypefunx float asinhf (float @var{x})
1407 @deftypefunx {long double} asinhl (long double @var{x})
1408 These functions return the inverse hyperbolic sine of @var{x}---the
1409 value whose hyperbolic sine is @var{x}.
1410 @end deftypefun
1412 @comment math.h
1413 @comment ISO
1414 @deftypefun double acosh (double @var{x})
1415 @deftypefunx float acoshf (float @var{x})
1416 @deftypefunx {long double} acoshl (long double @var{x})
1417 These functions return the inverse hyperbolic cosine of @var{x}---the
1418 value whose hyperbolic cosine is @var{x}.  If @var{x} is less than
1419 @code{1}, @code{acosh} returns @code{HUGE_VAL}.
1420 @end deftypefun
1422 @comment math.h
1423 @comment ISO
1424 @deftypefun double atanh (double @var{x})
1425 @deftypefunx float atanhf (float @var{x})
1426 @deftypefunx {long double} atanhl (long double @var{x})
1427 These functions return the inverse hyperbolic tangent of @var{x}---the
1428 value whose hyperbolic tangent is @var{x}.  If the absolute value of
1429 @var{x} is greater than or equal to @code{1}, @code{atanh} returns
1430 @code{HUGE_VAL}.
1431 @end deftypefun
1433 @cindex inverse complex hyperbolic functions
1435 @comment complex.h
1436 @comment ISO
1437 @deftypefun {complex double} casinh (complex double @var{z})
1438 @deftypefunx {complex float} casinhf (complex float @var{z})
1439 @deftypefunx {complex long double} casinhl (complex long double @var{z})
1440 These functions return the inverse complex hyperbolic sine of
1441 @var{z}---the value whose complex hyperbolic sine is @var{z}.
1442 @end deftypefun
1444 @comment complex.h
1445 @comment ISO
1446 @deftypefun {complex double} cacosh (complex double @var{z})
1447 @deftypefunx {complex float} cacoshf (complex float @var{z})
1448 @deftypefunx {complex long double} cacoshl (complex long double @var{z})
1449 These functions return the inverse complex hyperbolic cosine of
1450 @var{z}---the value whose complex hyperbolic cosine is @var{z}.  Unlike
1451 the real valued function @code{acosh} there is not limit for the range
1452 of the argument.
1453 @end deftypefun
1455 @comment complex.h
1456 @comment ISO
1457 @deftypefun {complex double} catanh (complex double @var{z})
1458 @deftypefunx {complex float} catanhf (complex float @var{z})
1459 @deftypefunx {complex long double} catanhl (complex long double @var{z})
1460 These functions return the inverse complex hyperbolic tangent of
1461 @var{z}---the value whose complex hyperbolic tangent is @var{z}.  Unlike
1462 the real valued function @code{atanh} there is not limit for the range
1463 of the argument.
1464 @end deftypefun
1467 @node Pseudo-Random Numbers
1468 @section Pseudo-Random Numbers
1469 @cindex random numbers
1470 @cindex pseudo-random numbers
1471 @cindex seed (for random numbers)
1473 This section describes the GNU facilities for generating a series of
1474 pseudo-random numbers.  The numbers generated are not truly random;
1475 typically, they form a sequence that repeats periodically, with a
1476 period so large that you can ignore it for ordinary purposes.  The
1477 random number generator works by remembering at all times a @dfn{seed}
1478 value which it uses to compute the next random number and also to
1479 compute a new seed.
1481 Although the generated numbers look unpredictable within one run of a
1482 program, the sequence of numbers is @emph{exactly the same} from one run
1483 to the next.  This is because the initial seed is always the same.  This
1484 is convenient when you are debugging a program, but it is unhelpful if
1485 you want the program to behave unpredictably.  If you want truly random
1486 numbers, not just pseudo-random, specify a seed based on the current
1487 time.
1489 You can get repeatable sequences of numbers on a particular machine type
1490 by specifying the same initial seed value for the random number
1491 generator.  There is no standard meaning for a particular seed value;
1492 the same seed, used in different C libraries or on different CPU types,
1493 will give you different random numbers.
1495 The GNU library supports the standard @w{ISO C} random number functions
1496 plus two other sets derived from BSD and SVID.  We recommend you use the
1497 standard ones, @code{rand} and @code{srand} if only a small number of
1498 random bits are required.  The SVID functions provide an interface which
1499 allows better random number generator algorithms and they return up to
1500 48 random bits in one calls and they also return random floating-point
1501 numbers if wanted.  The SVID function might not be available on some BSD
1502 derived systems but since they are required in the XPG they are
1503 available on all Unix-conformant systems.
1505 @menu
1506 * ISO Random::       @code{rand} and friends.
1507 * BSD Random::       @code{random} and friends.
1508 * SVID Random::      @code{drand48} and friends.
1509 @end menu
1511 @node ISO Random
1512 @subsection ISO C Random Number Functions
1514 This section describes the random number functions that are part of
1515 the @w{ISO C} standard.
1517 To use these facilities, you should include the header file
1518 @file{stdlib.h} in your program.
1519 @pindex stdlib.h
1521 @comment stdlib.h
1522 @comment ISO
1523 @deftypevr Macro int RAND_MAX
1524 The value of this macro is an integer constant expression that
1525 represents the maximum possible value returned by the @code{rand}
1526 function.  In the GNU library, it is @code{037777777}, which is the
1527 largest signed integer representable in 32 bits.  In other libraries, it
1528 may be as low as @code{32767}.
1529 @end deftypevr
1531 @comment stdlib.h
1532 @comment ISO
1533 @deftypefun int rand (void)
1534 The @code{rand} function returns the next pseudo-random number in the
1535 series.  The value is in the range from @code{0} to @code{RAND_MAX}.
1536 @end deftypefun
1538 @comment stdlib.h
1539 @comment ISO
1540 @deftypefun void srand (unsigned int @var{seed})
1541 This function establishes @var{seed} as the seed for a new series of
1542 pseudo-random numbers.  If you call @code{rand} before a seed has been
1543 established with @code{srand}, it uses the value @code{1} as a default
1544 seed.
1546 To produce truly random numbers (not just pseudo-random), do @code{srand
1547 (time (0))}.
1548 @end deftypefun
1550 A completely broken interface was designed by the POSIX.1 committee to
1551 support reproducible random numbers in multi-threaded programs.
1553 @comment stdlib.h
1554 @comment POSIX.1
1555 @deftypefun int rand_r (unsigned int *@var{seed})
1556 This function returns a random number in the range 0 to @code{RAND_MAX}
1557 just as @code{rand} does.  But this function does not keep an internal
1558 state for the RNG.  Instead the @code{unsigned int} variable pointed to
1559 by the argument @var{seed} is the only state.  Before the value is
1560 returned the state will be updated so that the next call will return a
1561 new number.
1563 I.e., the state of the RNG can only have as much bits as the type
1564 @code{unsigned int} has.  This is far too few to provide a good RNG.
1565 This interface is broken by design.
1567 If the program requires reproducible random numbers in multi-threaded
1568 programs the reentrant SVID functions are probably a better choice.  But
1569 these functions are GNU extensions and therefore @code{rand_r}, as being
1570 standardized in POSIX.1, should always be kept as a default method.
1571 @end deftypefun
1574 @node BSD Random
1575 @subsection BSD Random Number Functions
1577 This section describes a set of random number generation functions that
1578 are derived from BSD.  There is no advantage to using these functions
1579 with the GNU C library; we support them for BSD compatibility only.
1581 The prototypes for these functions are in @file{stdlib.h}.
1582 @pindex stdlib.h
1584 @comment stdlib.h
1585 @comment BSD
1586 @deftypefun {int32_t} random (void)
1587 This function returns the next pseudo-random number in the sequence.
1588 The range of values returned is from @code{0} to @code{RAND_MAX}.
1590 @strong{Please note:} Historically this function returned a @code{long
1591 int} value.  But with the appearance of 64bit machines this could lead
1592 to severe compatibility problems and therefore the type now explicitly
1593 limits the return value to 32bit.
1594 @end deftypefun
1596 @comment stdlib.h
1597 @comment BSD
1598 @deftypefun void srandom (unsigned int @var{seed})
1599 The @code{srandom} function sets the seed for the current random number
1600 state based on the integer @var{seed}.  If you supply a @var{seed} value
1601 of @code{1}, this will cause @code{random} to reproduce the default set
1602 of random numbers.
1604 To produce truly random numbers (not just pseudo-random), do
1605 @code{srandom (time (0))}.
1606 @end deftypefun
1608 @comment stdlib.h
1609 @comment BSD
1610 @deftypefun {void *} initstate (unsigned int @var{seed}, void *@var{state}, size_t @var{size})
1611 The @code{initstate} function is used to initialize the random number
1612 generator state.  The argument @var{state} is an array of @var{size}
1613 bytes, used to hold the state information.  The size must be at least 8
1614 bytes, and optimal sizes are 8, 16, 32, 64, 128, and 256.  The bigger
1615 the @var{state} array, the better.
1617 The return value is the previous value of the state information array.
1618 You can use this value later as an argument to @code{setstate} to
1619 restore that state.
1620 @end deftypefun
1622 @comment stdlib.h
1623 @comment BSD
1624 @deftypefun {void *} setstate (void *@var{state})
1625 The @code{setstate} function restores the random number state
1626 information @var{state}.  The argument must have been the result of
1627 a previous call to @var{initstate} or @var{setstate}.
1629 The return value is the previous value of the state information array.
1630 You can use this value later as an argument to @code{setstate} to
1631 restore that state.
1632 @end deftypefun
1635 @node SVID Random
1636 @subsection SVID Random Number Function
1638 The C library on SVID systems contains yet another kind of random number
1639 generator functions.  They use a state of 48 bits of data.  The user can
1640 choose among a collection of functions which all return the random bits
1641 in different forms.
1643 Generally there are two kinds of functions: those which use a state of
1644 the random number generator which is shared among several functions and
1645 by all threads of the process.  The second group of functions require
1646 the user to handle the state.
1648 All functions have in common that they use the same congruential
1649 formula with the same constants.  The formula is
1651 @smallexample
1652 Y = (a * X + c) mod m
1653 @end smallexample
1655 @noindent
1656 where @var{X} is the state of the generator at the beginning and
1657 @var{Y} the state at the end.  @code{a} and @code{c} are constants
1658 determining the way the generator work.  By default they are
1660 @smallexample
1661 a = 0x5DEECE66D = 25214903917
1662 c = 0xb = 11
1663 @end smallexample
1665 @noindent
1666 but they can also be changed by the user.  @code{m} is of course 2^48
1667 since the state consists of a 48 bit array.
1670 @comment stdlib.h
1671 @comment SVID
1672 @deftypefun double drand48 (void)
1673 This function returns a @code{double} value in the range of @code{0.0}
1674 to @code{1.0} (exclusive).  The random bits are determined by the global
1675 state of the random number generator in the C library.
1677 Since the @code{double} type according to @w{IEEE 754} has a 52 bit
1678 mantissa this means 4 bits are not initialized by the random number
1679 generator.  These are (of course) chosen to be the least significant
1680 bits and they are initialized to @code{0}.
1681 @end deftypefun
1683 @comment stdlib.h
1684 @comment SVID
1685 @deftypefun double erand48 (unsigned short int @var{xsubi}[3])
1686 This function returns a @code{double} value in the range of @code{0.0}
1687 to @code{1.0} (exclusive), similar to @code{drand48}.  The argument is
1688 an array describing the state of the random number generator.
1690 This function can be called subsequently since it updates the array to
1691 guarantee random numbers.  The array should have been initialized before
1692 using to get reproducible results.
1693 @end deftypefun
1695 @comment stdlib.h
1696 @comment SVID
1697 @deftypefun {long int} lrand48 (void)
1698 The @code{lrand48} functions return an integer value in the range of
1699 @code{0} to @code{2^31} (exclusive).  Even if the size of the @code{long
1700 int} type can take more than 32 bits no higher numbers are returned.
1701 The random bits are determined by the global state of the random number
1702 generator in the C library.
1703 @end deftypefun
1705 @comment stdlib.h
1706 @comment SVID
1707 @deftypefun {long int} nrand48 (unsigned short int @var{xsubi}[3])
1708 This function is similar to the @code{lrand48} function in that it
1709 returns a number in the range of @code{0} to @code{2^31} (exclusive) but
1710 the state of the random number generator used to produce the random bits
1711 is determined by the array provided as the parameter to the function.
1713 The numbers in the array are afterwards updated so that subsequent calls
1714 to this function yield to different results (as it is expected by a
1715 random number generator).  The array should have been initialized before
1716 the first call to get reproducible results.
1717 @end deftypefun
1719 @comment stdlib.h
1720 @comment SVID
1721 @deftypefun {long int} mrand48 (void)
1722 The @code{mrand48} function is similar to @code{lrand48}.  The only
1723 difference is that the numbers returned are in the range @code{-2^31} to
1724 @code{2^31} (exclusive).
1725 @end deftypefun
1727 @comment stdlib.h
1728 @comment SVID
1729 @deftypefun {long int} jrand48 (unsigned short int @var{xsubi}[3])
1730 The @code{jrand48} function is similar to @code{nrand48}.  The only
1731 difference is that the numbers returned are in the range @code{-2^31} to
1732 @code{2^31} (exclusive).  For the @code{xsubi} parameter the same
1733 requirements are necessary.
1734 @end deftypefun
1736 The internal state of the random number generator can be initialized in
1737 several ways.  The functions differ in the completeness of the
1738 information provided.
1740 @comment stdlib.h
1741 @comment SVID
1742 @deftypefun void srand48 (long int @var{seedval}))
1743 The @code{srand48} function sets the most significant 32 bits of the
1744 state internal state of the random number generator to the least
1745 significant 32 bits of the @var{seedval} parameter.  The lower 16 bits
1746 are initialized to the value @code{0x330E}.  Even if the @code{long
1747 int} type contains more the 32 bits only the lower 32 bits are used.
1749 Due to this limitation the initialization of the state using this
1750 function of not very useful.  But it makes it easy to use a construct
1751 like @code{srand48 (time (0))}.
1753 A side-effect of this function is that the values @code{a} and @code{c}
1754 from the internal state, which are used in the congruential formula,
1755 are reset to the default values given above.  This is of importance once
1756 the user called the @code{lcong48} function (see below).
1757 @end deftypefun
1759 @comment stdlib.h
1760 @comment SVID
1761 @deftypefun {unsigned short int *} seed48 (unsigned short int @var{seed16v}[3])
1762 The @code{seed48} function initializes all 48 bits of the state of the
1763 internal random number generator from the content of the parameter
1764 @var{seed16v}.  Here the lower 16 bits of the first element of
1765 @var{see16v} initialize the least significant 16 bits of the internal
1766 state, the lower 16 bits of @code{@var{seed16v}[1]} initialize the mid-order
1767 16 bits of the state and the 16 lower bits of @code{@var{seed16v}[2]}
1768 initialize the most significant 16 bits of the state.
1770 Unlike @code{srand48} this function lets the user initialize all 48 bits
1771 of the state.
1773 The value returned by @code{seed48} is a pointer to an array containing
1774 the values of the internal state before the change.  This might be
1775 useful to restart the random number generator at a certain state.
1776 Otherwise, the value can simply be ignored.
1778 As for @code{srand48}, the values @code{a} and @code{c} from the
1779 congruential formula are reset to the default values.
1780 @end deftypefun
1782 There is one more function to initialize the random number generator
1783 which allows to specify even more information by allowing to change the
1784 parameters in the congruential formula.
1786 @comment stdlib.h
1787 @comment SVID
1788 @deftypefun void lcong48 (unsigned short int @var{param}[7])
1789 The @code{lcong48} function allows the user to change the complete state
1790 of the random number generator.  Unlike @code{srand48} and
1791 @code{seed48}, this function also changes the constants in the
1792 congruential formula.
1794 From the seven elements in the array @var{param} the least significant
1795 16 bits of the entries @code{@var{param}[0]} to @code{@var{param}[2]}
1796 determine the initial state, the least 16 bits of
1797 @code{@var{param}[3]} to @code{@var{param}[5]} determine the 48 bit
1798 constant @code{a} and @code{@var{param}[6]} determines the 16 bit value
1799 @code{c}.
1800 @end deftypefun
1802 All the above functions have in common that they use the global
1803 parameters for the congruential formula.  In multi-threaded programs it
1804 might sometimes be useful to have different parameters in different
1805 threads.  For this reason all the above functions have a counterpart
1806 which works on a description of the random number generator in the
1807 user-supplied buffer instead of the global state.
1809 Please note that it is no problem if several threads use the global
1810 state if all threads use the functions which take a pointer to an array
1811 containing the state.  The random numbers are computed following the
1812 same loop but if the state in the array is different all threads will
1813 get an individual random number generator.
1815 The user supplied buffer must be of type @code{struct drand48_data}.
1816 This type should be regarded as opaque and no member should be used
1817 directly.
1819 @comment stdlib.h
1820 @comment GNU
1821 @deftypefun int drand48_r (struct drand48_data *@var{buffer}, double *@var{result})
1822 This function is equivalent to the @code{drand48} function with the
1823 difference it does not modify the global random number generator
1824 parameters but instead the parameters is the buffer supplied by the
1825 buffer through the pointer @var{buffer}.  The random number is return in
1826 the variable pointed to by @var{result}.
1828 The return value of the function indicate whether the call succeeded.
1829 If the value is less than @code{0} an error occurred and @var{errno} is
1830 set to indicate the problem.
1832 This function is a GNU extension and should not be used in portable
1833 programs.
1834 @end deftypefun
1836 @comment stdlib.h
1837 @comment GNU
1838 @deftypefun int erand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, double *@var{result})
1839 The @code{erand48_r} function works like the @code{erand48} and it takes
1840 an argument @var{buffer} which describes the random number generator.
1841 The state of the random number generator is taken from the @code{xsubi}
1842 array, the parameters for the congruential formula from the global
1843 random number generator data.  The random number is return in the
1844 variable pointed to by @var{result}.
1846 The return value is non-negative is the call succeeded.
1848 This function is a GNU extension and should not be used in portable
1849 programs.
1850 @end deftypefun
1852 @comment stdlib.h
1853 @comment GNU
1854 @deftypefun int lrand48_r (struct drand48_data *@var{buffer}, double *@var{result})
1855 This function is similar to @code{lrand48} and it takes a pointer to a
1856 buffer describing the state of the random number generator as a
1857 parameter just like @code{drand48}.
1859 If the return value of the function is non-negative the variable pointed
1860 to by @var{result} contains the result.  Otherwise an error occurred.
1862 This function is a GNU extension and should not be used in portable
1863 programs.
1864 @end deftypefun
1866 @comment stdlib.h
1867 @comment GNU
1868 @deftypefun int nrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
1869 The @code{nrand48_r} function works like @code{nrand48} in that it
1870 produces a random number in range @code{0} to @code{2^31}.  But instead
1871 of using the global parameters for the congruential formula it uses the
1872 information from the buffer pointed to by @var{buffer}.  The state is
1873 described by the values in @var{xsubi}.
1875 If the return value is non-negative the variable pointed to by
1876 @var{result} contains the result.
1878 This function is a GNU extension and should not be used in portable
1879 programs.
1880 @end deftypefun
1882 @comment stdlib.h
1883 @comment GNU
1884 @deftypefun int mrand48_r (struct drand48_data *@var{buffer}, double *@var{result})
1885 This function is similar to @code{mrand48} but as the other reentrant
1886 function it uses the random number generator described by the value in
1887 the buffer pointed to by @var{buffer}.
1889 If the return value is non-negative the variable pointed to by
1890 @var{result} contains the result.
1892 This function is a GNU extension and should not be used in portable
1893 programs.
1894 @end deftypefun
1896 @comment stdlib.h
1897 @comment GNU
1898 @deftypefun int jrand48_r (unsigned short int @var{xsubi}[3], struct drand48_data *@var{buffer}, long int *@var{result})
1899 The @code{jrand48_r} function is similar to @code{jrand48}.  But as the
1900 other reentrant functions of this function family it uses the
1901 congruential formula parameters from the buffer pointed to by
1902 @var{buffer}.
1904 If the return value is non-negative the variable pointed to by
1905 @var{result} contains the result.
1907 This function is a GNU extension and should not be used in portable
1908 programs.
1909 @end deftypefun
1911 Before any of the above functions should be used the buffer of type
1912 @code{struct drand48_data} should initialized.  The easiest way is to
1913 fill the whole buffer with null bytes, e.g., using
1915 @smallexample
1916 memset (buffer, '\0', sizeof (struct drand48_data));
1917 @end smallexample
1919 @noindent
1920 Using any of the reentrant functions of this family now will
1921 automatically initialize the random number generator to the default
1922 values for the state and the parameters of the congruential formula.
1924 The other possibility is too use any of the functions which explicitely
1925 initialize the buffer.  Though it might be obvious how to initialize the
1926 buffer from the data given as parameter from the function it is highly
1927 recommended to use these functions since the result might not always be
1928 what you expect.
1930 @comment stdlib.h
1931 @comment GNU
1932 @deftypefun int srand48_r (long int @var{seedval}, struct drand48_data *@var{buffer})
1933 The description of the random number generator represented by the
1934 information in @var{buffer} is initialized similar to what the function
1935 @code{srand48} does.  The state is initialized from the parameter
1936 @var{seedval} and the parameters for the congruential formula are
1937 initialized to the default values.
1939 If the return value is non-negative the function call succeeded.
1941 This function is a GNU extension and should not be used in portable
1942 programs.
1943 @end deftypefun
1945 @comment stdlib.h
1946 @comment GNU
1947 @deftypefun int seed48_r (unsigned short int @var{seed16v}[3], struct drand48_data *@var{buffer})
1948 This function is similar to @code{srand48_r} but like @code{seed48} it
1949 initializes all 48 bits of the state from the parameter @var{seed16v}.
1951 If the return value is non-negative the function call succeeded.  It
1952 does not return a pointer to the previous state of the random number
1953 generator like the @code{seed48} function does.  if the user wants to
1954 preserve the state for a later rerun s/he can copy the whole buffer
1955 pointed to by @var{buffer}.
1957 This function is a GNU extension and should not be used in portable
1958 programs.
1959 @end deftypefun
1961 @comment stdlib.h
1962 @comment GNU
1963 @deftypefun int lcong48_r (unsigned short int @var{param}[7], struct drand48_data *@var{buffer})
1964 This function initializes all aspects of the random number generator
1965 described in @var{buffer} by the data in @var{param}.  Here it is
1966 especially true the function does more than just copying the contents of
1967 @var{param} of @var{buffer}.  Some more actions are required and
1968 therefore it is important to use this function and not initialized the
1969 random number generator directly.
1971 If the return value is non-negative the function call succeeded.
1973 This function is a GNU extension and should not be used in portable
1974 programs.
1975 @end deftypefun