3 * ISO/IEC 9899:1999 -- Programming Languages C: 7.12 Mathematics
4 * Derived from the Cephes Math Library Release 2.3
5 * Copyright 1984, 1987, 1989, 1995 by Stephen L. Moshier
10 * The file also includes a conditional assembly definition
11 * for the type of computer arithmetic (IEEE, DEC, Motorola
14 * For Digital Equipment PDP-11 and VAX computers, certain
15 * IBM systems, and others that use numbers with a 56-bit
16 * significand, the symbol DEC should be defined. In this
17 * mode, most floating point constants are given as arrays
18 * of octal integers to eliminate decimal to binary conversion
19 * errors that might be introduced by the compiler.
21 * For little-endian computers, such as IBM PC, that follow the
22 * IEEE Standard for Binary Floating Point Arithmetic (ANSI/IEEE
23 * Std 754-1985), the symbol IBMPC should be defined. These
24 * numbers have 53-bit significands. In this mode, constants
25 * are provided as arrays of hexadecimal 16 bit integers.
27 * Big-endian IEEE format is denoted MIEEE. On some RISC
28 * systems such as Sun SPARC, double precision constants
29 * must be stored on 8-byte address boundaries. Since integer
30 * arrays may be aligned differently, the MIEEE configuration
31 * may fail on such machines.
33 * To accommodate other types of computer arithmetic, all
34 * constants are also provided in a normal decimal radix
35 * which one can hope are correctly converted to a suitable
36 * format by the available C language compiler. To invoke
37 * this mode, define the symbol UNK.
39 * An important difference among these modes is a predefined
40 * set of machine arithmetic constants for each. The numbers
41 * MACHEP (the machine roundoff error), MAXNUM (largest number
42 * represented), and several other parameters are preset by
43 * the configuration symbol. Check the file const.c to
44 * ensure that these values are correct for your computer.
46 * Configurations NANS, INFINITIES, MINUSZERO, and DENORMAL
47 * may fail on many systems. Verify that they are supposed
48 * to work on your computer.
56 #include <bits/huge_val.h>
58 /* Type of computer arithmetic */
60 /* PDP-11, Pro350, VAX:
64 /* Intel IEEE, low order words come first:
68 /* Motorola IEEE, high order words come first
69 * (Sun 680x0 workstation):
73 /* UNKnown arithmetic, invokes coefficients given in
74 * normal decimal format. Beware of range boundary
75 * problems (MACHEP, MAXLOG, etc. in const.c) and
76 * roundoff problems in pow.c:
82 /* Define if the `long double' type works. */
83 #define HAVE_LONG_DOUBLE 1
85 /* Define as the return type of signal handlers (int or void). */
86 #define RETSIGTYPE void
88 /* Define if you have the ANSI C header files. */
89 #define STDC_HEADERS 1
91 /* Define if your processor stores words with the most significant
92 byte first (like Motorola and SPARC, unlike Intel and VAX). */
93 /* #undef WORDS_BIGENDIAN */
95 /* Define if floating point words are bigendian. */
96 /* #undef FLOAT_WORDS_BIGENDIAN */
98 /* The number of bytes in a int. */
101 /* Define if you have the <string.h> header file. */
102 #define HAVE_STRING_H 1
105 /* Define this `volatile' if your compiler thinks
106 * that floating point arithmetic obeys the associative
107 * and distributive laws. It will defeat some optimizations
108 * (but probably not enough of them).
110 * #define VOLATILE volatile
114 /* For 12-byte long doubles on an i386, pad a 16-bit short 0
115 * to the end of real constants initialized by integer arrays.
119 * Otherwise, the type is 10 bytes long and XPD should be
120 * defined blank (e.g., Microsoft C).
126 /* Define to support tiny denormal numbers, else undefine. */
129 /* Define to ask for infinity support, else undefine. */
132 /* Define to ask for support of numbers that are Not-a-Number,
133 else undefine. This may automatically define INFINITIES in some files. */
136 /* Define to distinguish between -0.0 and +0.0. */
139 /* Define 1 for ANSI C atan2() function
140 and ANSI prototypes for float arguments.
141 See atan.c and clog.c. */
146 /* Constant definitions for math error conditions */
148 #define DOMAIN 1 /* argument domain error */
149 #define SING 2 /* argument singularity */
150 #define OVERFLOW 3 /* overflow range error */
151 #define UNDERFLOW 4 /* underflow range error */
152 #define TLOSS 5 /* total loss of precision */
153 #define PLOSS 6 /* partial loss of precision */
158 /* Complex numeral. */
159 #ifdef __UCLIBC_HAS_LIBM_DOUBLE__
167 #ifdef __UCLIBC_HAS_LIBM_FLOAT__
175 #ifdef __UCLIBC_HAS_LIBM_LONG_DOUBLE__
176 /* Long double complex numeral. */
186 /* Variable for error reporting. See mtherr.c. */
188 extern int mtherr(char *name, int code);
193 /* If you define UNK, then be sure to set BIGENDIAN properly. */
195 #if __BYTE_ORDER == __BIG_ENDIAN
197 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
204 /* Get general and ISO C 9X specific information. */
205 #include <bits/mathdef.h>
211 /* Get the architecture specific values describing the floating-point
212 evaluation. The following symbols will get defined:
214 float_t floating-point type at least as wide as `float' used
215 to evaluate `float' expressions
216 double_t floating-point type at least as wide as `double' used
217 to evaluate `double' expressions
221 0 if `float_t' is `float' and `double_t' is `double'
222 1 if `float_t' and `double_t' are `double'
223 2 if `float_t' and `double_t' are `long double'
224 else `float_t' and `double_t' are unspecified
226 INFINITY representation of the infinity value of type `float'
231 If defined it indicates that the `fma' function
232 generally executes about as fast as a multiply and an add.
233 This macro is defined only iff the `fma' function is
234 implemented directly with a hardware multiply-add instructions.
236 FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
237 FP_ILOGBNAN Expands to a value returned by `ilogb (NAN)'.
239 DECIMAL_DIG Number of decimal digits supported by conversion between
240 decimal and all internal floating-point formats.
244 /* All floating-point numbers can be put in one of these categories. */
248 # define FP_NAN FP_NAN
250 # define FP_INFINITE FP_INFINITE
252 # define FP_ZERO FP_ZERO
254 # define FP_SUBNORMAL FP_SUBNORMAL
256 # define FP_NORMAL FP_NORMAL
259 /* Return number of classification appropriate for X. */
260 #ifdef __UCLIBC_HAS_LIBM_DOUBLE__
261 # define fpclassify(x) \
262 (sizeof (x) == sizeof (float) ? \
264 : sizeof (x) == sizeof (double) ? \
265 __fpclassify (x) : __fpclassifyl (x))
267 # define fpclassify(x) \
268 (sizeof (x) == sizeof (float) ? __fpclassifyf (x) : __fpclassify (x))
273 #ifdef __UCLIBC_HAS_LIBM_DOUBLE__
274 /* Return nonzero value if sign of X is negative. */
275 extern int signbit(double x);
276 /* Return nonzero value if X is not +-Inf or NaN. */
277 extern int isfinite(double x);
278 /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */
279 # define isnormal(x) (fpclassify (x) == FP_NORMAL)
280 /* Return nonzero value if X is a NaN */
281 extern int isnan(double x);
283 (sizeof (x) == sizeof (float) ? \
285 : sizeof (x) == sizeof (double) ? \
286 __isinf (x) : __isinfl (x))
289 (sizeof (x) == sizeof (float) ? __isinff (x) : __isinf (x))
293 #ifdef __UCLIBC_HAS_LIBM_LONG_DOUBLE__
294 /* Return nonzero value if sign of X is negative. */
295 extern int signbitl(long double x);
296 /* Return nonzero value if X is not +-Inf or NaN. */
297 extern int isfinitel(long double x);
298 /* Return nonzero value if X is a NaN */
299 extern int isnanl(long double x);
303 /* Some useful constants. */
304 #if defined __USE_BSD || defined __USE_XOPEN
305 # define M_E 2.7182818284590452354 /* e */
306 # define M_LOG2E 1.4426950408889634074 /* log_2 e */
307 # define M_LOG10E 0.43429448190325182765 /* log_10 e */
308 # define M_LN2 0.69314718055994530942 /* log_e 2 */
309 # define M_LN10 2.30258509299404568402 /* log_e 10 */
310 # define M_PI 3.14159265358979323846 /* pi */
311 # define M_PI_2 1.57079632679489661923 /* pi/2 */
312 # define M_PI_4 0.78539816339744830962 /* pi/4 */
313 # define M_1_PI 0.31830988618379067154 /* 1/pi */
314 # define M_2_PI 0.63661977236758134308 /* 2/pi */
315 # define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
316 # define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
317 # define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
321 # define M_LOG2El M_LOG2E
322 # define M_LOG10El M_LOG10E
323 # define M_LN2l M_LN2
324 # define M_LN10l M_LN10
326 # define M_PI_2l M_PI_2
327 # define M_PI_4l M_PI_4
328 # define M_1_PIl M_1_PI
329 # define M_2_PIl M_2_PI
330 # define M_2_SQRTPIl M_2_SQRTPI
331 # define M_SQRT2l M_SQRT2
332 # define M_SQRT1_2l M_SQRT1_2
337 #ifdef __UCLIBC_HAS_LIBM_DOUBLE__
338 /* 7.12.4 Trigonometric functions */
339 extern double acos(double x);
340 extern double asin(double x);
341 extern double atan(double x);
342 extern double atan2(double y, double x);
343 extern double cos(double x);
344 extern double sin(double x);
345 extern double tan(double x);
347 /* 7.12.5 Hyperbolic functions */
348 extern double acosh(double x);
349 extern double asinh(double x);
350 extern double atanh(double x);
351 extern double cosh(double x);
352 extern double sinh(double x);
353 extern double tanh(double x);
355 /* 7.12.6 Exponential and logarithmic functions */
356 extern double exp(double x);
357 extern double exp2(double x);
358 extern double expm1(double x);
359 extern double frexp(double value, int *exp);
360 extern int ilogb(double x);
361 extern double ldexp(double x, int exp);
362 extern double log(double x);
363 extern double log10(double x);
364 extern double log1p(double x);
365 extern double log2(double x);
366 extern double logb(double x);
367 extern double modf(double value, double *iptr);
368 extern double scalbn(double x, int n);
369 extern double scalbln(double x, long int n);
371 /* 7.12.7 Power and absolute-value functions */
372 extern double fabs(double x);
373 extern double hypot(double x, double y);
374 extern double pow(double x, double y);
375 extern double sqrt(double x);
377 /* 7.12.8 Error and gamma functions */
378 extern double erf(double x);
379 extern double erfc(double x);
380 extern double lgamma(double x);
381 extern double tgamma(double x);
383 /* 7.12.9 Nearest integer functions */
384 extern double ceil(double x);
385 extern double floor(double x);
386 extern double nearbyint(double x);
387 extern double rint(double x);
388 extern long int lrint(double x);
389 extern long long int llrint(double x);
390 extern double round(double x);
391 extern long int lround(double x);
392 extern long long int llround(double x);
393 extern double trunc(double x);
395 /* 7.12.10 Remainder functions */
396 extern double fmod(double x, double y);
397 extern double remainder(double x, double y);
398 extern double remquo(double x, double y, int *quo);
400 /* 7.12.11 Manipulation functions */
401 extern double copysign(double x, double y);
402 extern double nan(const char *tagp);
403 extern double nextafter(double x, double y);
405 /* 7.12.12 Maximum, minimum, and positive difference functions */
406 extern double fdim(double x, double y);
407 extern double fmax(double x, double y);
408 extern double fmin(double x, double y);
410 /* 7.12.13 Floating multiply-add */
411 extern double fma(double x, double y, double z);
414 #ifdef __UCLIBC_HAS_LIBM_FLOAT__
415 /* 7.12.4 Trigonometric functions */
416 extern float acosf(float x);
417 extern float asinf(float x);
418 extern float atanf(float x);
419 extern float atan2f(float y, float x);
420 extern float cosf(float x);
421 extern float sinf(float x);
422 extern float tanf(float x);
424 /* 7.12.5 Hyperbolic functions */
425 extern float acoshf(float x);
426 extern float asinhf(float x);
427 extern float atanhf(float x);
428 extern float coshf(float x);
429 extern float sinhf(float x);
430 extern float tanhf(float x);
432 /* 7.12.6 Exponential and logarithmic functions */
433 extern float expf(float x);
434 extern float exp2f(float x);
435 extern float expm1f(float x);
436 extern float frexpf(float value, int *exp);
437 extern int ilogbf(float x);
438 extern float ldexpf(float x, int exp);
439 extern float logf(float x);
440 extern float log10f(float x);
441 extern float log1pf(float x);
442 extern float log2f(float x);
443 extern float logbf(float x);
444 extern float modff(float value, float *iptr);
445 extern float scalbnf(float x, int n);
446 extern float scalblnf(float x, long int n);
448 /* 7.12.7 Power and absolute-value functions */
449 extern float fabsf(float x);
450 extern float hypotf(float x, float y);
451 extern float powf(float x, float y);
452 extern float sqrtf(float x);
454 /* 7.12.8 Error and gamma functions */
455 extern float erff(float x);
456 extern float erfcf(float x);
457 extern float lgammaf(float x);
458 extern float tgammaf(float x);
460 /* 7.12.9 Nearest integer functions */
461 extern float ceilf(float x);
462 extern float floorf(float x);
463 extern float nearbyintf(float x);
464 extern float rintf(float x);
465 extern long int lrintf(float x);
466 extern long long int llrintf(float x);
467 extern float roundf(float x);
468 extern long int lroundf(float x);
469 extern long long int llroundf(float x);
470 extern float truncf(float x);
472 /* 7.12.10 Remainder functions */
473 extern float fmodf(float x, float y);
474 extern float remainderf(float x, float y);
475 extern float remquof(float x, float y, int *quo);
477 /* 7.12.11 Manipulation functions */
478 extern float copysignf(float x, float y);
479 extern float nanf(const char *tagp);
480 extern float nextafterf(float x, float y);
482 /* 7.12.12 Maximum, minimum, and positive difference functions */
483 extern float fdimf(float x, float y);
484 extern float fmaxf(float x, float y);
485 extern float fminf(float x, float y);
487 /* 7.12.13 Floating multiply-add */
488 extern float fmaf(float x, float y, float z);
491 #ifdef __UCLIBC_HAS_LIBM_LONG_DOUBLE__
492 /* 7.12.4 Trigonometric functions */
493 extern long double acosl(long double x);
494 extern long double asinl(long double x);
495 extern long double atanl(long double x);
496 extern long double atan2l(long double y, long double x);
497 extern long double cosl(long double x);
498 extern long double sinl(long double x);
499 extern long double tanl(long double x);
501 /* 7.12.5 Hyperbolic functions */
502 extern long double acoshl(long double x);
503 extern long double asinhl(long double x);
504 extern long double atanhl(long double x);
505 extern long double coshl(long double x);
506 extern long double sinhl(long double x);
507 extern long double tanhl(long double x);
509 /* 7.12.6 Exponential and logarithmic functions */
510 extern long double expl(long double x);
511 extern long double exp2l(long double x);
512 extern long double expm1l(long double x);
513 extern long double frexpl(long double value, int *exp);
514 extern int ilogbl(long double x);
515 extern long double ldexpl(long double x, int exp);
516 extern long double logl(long double x);
517 extern long double log10l(long double x);
518 extern long double log1pl(long double x);
519 extern long double log2l(long double x);
520 extern long double logbl(long double x);
521 extern long double modfl(long double value, long double *iptr);
522 extern long double scalbnl(long double x, int n);
523 extern long double scalblnl(long double x, long int n);
525 /* 7.12.7 Power and absolute-value functions */
526 extern long double fabsl(long double x);
527 extern long double hypotl(long double x, long double y);
528 extern long double powl(long double x, long double y);
529 extern long double sqrtl(long double x);
531 /* 7.12.8 Error and gamma functions */
532 extern long double erfl(long double x);
533 extern long double erfcl(long double x);
534 extern long double lgammal(long double x);
535 extern long double tgammal(long double x);
537 /* 7.12.9 Nearest integer functions */
538 extern long double ceill(long double x);
539 extern long double floorl(long double x);
540 extern long double nearbyintl(long double x);
541 extern long double rintl(long double x);
542 extern long int lrintl(long double x);
543 extern long long int llrintl(long double x);
544 extern long double roundl(long double x);
545 extern long int lroundl(long double x);
546 extern long long int llroundl(long double x);
547 extern long double truncl(long double x);
549 /* 7.12.10 Remainder functions */
550 extern long double fmodl(long double x, long double y);
551 extern long double remainderl(long double x, long double y);
552 extern long double remquol(long double x, long double y, int *quo);
554 /* 7.12.11 Manipulation functions */
555 extern long double copysignl(long double x, long double y);
556 extern long double nanl(const char *tagp);
557 extern long double nextafterl(long double x, long double y);
558 extern long double nexttowardl(long double x, long double y);
560 /* 7.12.12 Maximum, minimum, and positive difference functions */
561 extern long double fdiml(long double x, long double y);
562 extern long double fmaxl(long double x, long double y);
563 extern long double fminl(long double x, long double y);
565 /* 7.12.13 Floating multiply-add */
566 extern long double fmal(long double x, long double y, long double z);
569 /* 7.12.14 Comparison macros */
571 # define isgreater(x, y) \
573 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
574 !isunordered (__x, __y) && __x > __y; }))
577 /* Return nonzero value if X is greater than or equal to Y. */
578 # ifndef isgreaterequal
579 # define isgreaterequal(x, y) \
581 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
582 !isunordered (__x, __y) && __x >= __y; }))
585 /* Return nonzero value if X is less than Y. */
587 # define isless(x, y) \
589 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
590 !isunordered (__x, __y) && __x < __y; }))
593 /* Return nonzero value if X is less than or equal to Y. */
595 # define islessequal(x, y) \
597 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
598 !isunordered (__x, __y) && __x <= __y; }))
601 /* Return nonzero value if either X is less than Y or Y is less than X. */
602 # ifndef islessgreater
603 # define islessgreater(x, y) \
605 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
606 !isunordered (__x, __y) && (__x < __y || __y < __x); }))
609 /* Return nonzero value if arguments are unordered. */
611 # define isunordered(u, v) \
613 ({ __typeof__(u) __u = (u); __typeof__(v) __v = (v); \
614 fpclassify (__u) == FP_NAN || fpclassify (__v) == FP_NAN; }))