math.h revision 1.49
1/* $NetBSD: math.h,v 1.49 2009/10/04 22:59:25 christos Exp $ */ 2 3/* 4 * ==================================================== 5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 6 * 7 * Developed at SunPro, a Sun Microsystems, Inc. business. 8 * Permission to use, copy, modify, and distribute this 9 * software is freely granted, provided that this notice 10 * is preserved. 11 * ==================================================== 12 */ 13 14/* 15 * @(#)fdlibm.h 5.1 93/09/24 16 */ 17 18#ifndef _MATH_H_ 19#define _MATH_H_ 20 21#include <sys/cdefs.h> 22#include <sys/featuretest.h> 23 24union __float_u { 25 unsigned char __dummy[sizeof(float)]; 26 float __val; 27}; 28 29union __double_u { 30 unsigned char __dummy[sizeof(double)]; 31 double __val; 32}; 33 34union __long_double_u { 35 unsigned char __dummy[sizeof(long double)]; 36 long double __val; 37}; 38 39#include <machine/math.h> /* may use __float_u, __double_u, 40 or __long_double_u */ 41 42#ifdef __HAVE_LONG_DOUBLE 43#define __fpmacro_unary_floating(__name, __arg0) \ 44 /* LINTED */ \ 45 ((sizeof (__arg0) == sizeof (float)) \ 46 ? __ ## __name ## f (__arg0) \ 47 : (sizeof (__arg0) == sizeof (double)) \ 48 ? __ ## __name ## d (__arg0) \ 49 : __ ## __name ## l (__arg0)) 50#else 51#define __fpmacro_unary_floating(__name, __arg0) \ 52 /* LINTED */ \ 53 ((sizeof (__arg0) == sizeof (float)) \ 54 ? __ ## __name ## f (__arg0) \ 55 : __ ## __name ## d (__arg0)) 56#endif /* __HAVE_LONG_DOUBLE */ 57 58/* 59 * ANSI/POSIX 60 */ 61/* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */ 62extern const union __double_u __infinity; 63#define HUGE_VAL __infinity.__val 64 65/* 66 * ISO C99 67 */ 68#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \ 69 !defined(_XOPEN_SOURCE) || \ 70 ((__STDC_VERSION__ - 0) >= 199901L) || \ 71 ((_POSIX_C_SOURCE - 0) >= 200112L) || \ 72 ((_XOPEN_SOURCE - 0) >= 600) || \ 73 defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE) 74/* 7.12#3 HUGE_VAL, HUGELF, HUGE_VALL */ 75extern const union __float_u __infinityf; 76#define HUGE_VALF __infinityf.__val 77 78extern const union __long_double_u __infinityl; 79#define HUGE_VALL __infinityl.__val 80 81/* 7.12#4 INFINITY */ 82#ifdef __INFINITY 83#define INFINITY __INFINITY /* float constant which overflows */ 84#else 85#define INFINITY HUGE_VALF /* positive infinity */ 86#endif /* __INFINITY */ 87 88/* 7.12#5 NAN: a quiet NaN, if supported */ 89#ifdef __HAVE_NANF 90#if __GNUC_PREREQ__(3,3) 91#define NAN __builtin_nanf("") 92#else 93extern const union __float_u __nanf; 94#define NAN __nanf.__val 95#endif 96#endif /* __HAVE_NANF */ 97 98/* 7.12#6 number classification macros */ 99#define FP_INFINITE 0x00 100#define FP_NAN 0x01 101#define FP_NORMAL 0x02 102#define FP_SUBNORMAL 0x03 103#define FP_ZERO 0x04 104/* NetBSD extensions */ 105#define _FP_LOMD 0x80 /* range for machine-specific classes */ 106#define _FP_HIMD 0xff 107 108#endif /* !_ANSI_SOURCE && ... */ 109 110/* 111 * XOPEN/SVID 112 */ 113#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) 114#define M_E 2.7182818284590452354 /* e */ 115#define M_LOG2E 1.4426950408889634074 /* log 2e */ 116#define M_LOG10E 0.43429448190325182765 /* log 10e */ 117#define M_LN2 0.69314718055994530942 /* log e2 */ 118#define M_LN10 2.30258509299404568402 /* log e10 */ 119#define M_PI 3.14159265358979323846 /* pi */ 120#define M_PI_2 1.57079632679489661923 /* pi/2 */ 121#define M_PI_4 0.78539816339744830962 /* pi/4 */ 122#define M_1_PI 0.31830988618379067154 /* 1/pi */ 123#define M_2_PI 0.63661977236758134308 /* 2/pi */ 124#define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ 125#define M_SQRT2 1.41421356237309504880 /* sqrt(2) */ 126#define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ 127 128#define MAXFLOAT ((float)3.40282346638528860e+38) 129extern int signgam; 130#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */ 131 132#if defined(_NETBSD_SOURCE) 133enum fdversion {fdlibm_ieee = -1, fdlibm_svid, fdlibm_xopen, fdlibm_posix}; 134 135#define _LIB_VERSION_TYPE enum fdversion 136#define _LIB_VERSION _fdlib_version 137 138/* if global variable _LIB_VERSION is not desirable, one may 139 * change the following to be a constant by: 140 * #define _LIB_VERSION_TYPE const enum version 141 * In that case, after one initializes the value _LIB_VERSION (see 142 * s_lib_version.c) during compile time, it cannot be modified 143 * in the middle of a program 144 */ 145extern _LIB_VERSION_TYPE _LIB_VERSION; 146 147#define _IEEE_ fdlibm_ieee 148#define _SVID_ fdlibm_svid 149#define _XOPEN_ fdlibm_xopen 150#define _POSIX_ fdlibm_posix 151 152#ifndef __cplusplus 153struct exception { 154 int type; 155 const char *name; 156 double arg1; 157 double arg2; 158 double retval; 159}; 160#endif 161 162#define HUGE MAXFLOAT 163 164/* 165 * set X_TLOSS = pi*2**52, which is possibly defined in <values.h> 166 * (one may replace the following line by "#include <values.h>") 167 */ 168 169#define X_TLOSS 1.41484755040568800000e+16 170 171#define DOMAIN 1 172#define SING 2 173#define OVERFLOW 3 174#define UNDERFLOW 4 175#define TLOSS 5 176#define PLOSS 6 177 178#endif /* _NETBSD_SOURCE */ 179 180__BEGIN_DECLS 181/* 182 * ANSI/POSIX 183 */ 184double acos(double); 185double asin(double); 186double atan(double); 187double atan2(double, double); 188double cos(double); 189double sin(double); 190double tan(double); 191 192double cosh(double); 193double sinh(double); 194double tanh(double); 195 196double exp(double); 197double frexp(double, int *); 198double ldexp(double, int); 199double log(double); 200double log2(double); 201double log10(double); 202double modf(double, double *); 203 204double pow(double, double); 205double sqrt(double); 206 207double ceil(double); 208double fabs(double); 209double floor(double); 210double fmod(double, double); 211 212#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE) 213double erf(double); 214double erfc(double); 215double gamma(double); 216double hypot(double, double); 217int finite(double); 218double j0(double); 219double j1(double); 220double jn(int, double); 221double lgamma(double); 222double y0(double); 223double y1(double); 224double yn(int, double); 225 226#if (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE) 227double acosh(double); 228double asinh(double); 229double atanh(double); 230double cbrt(double); 231double expm1(double); 232int ilogb(double); 233double log1p(double); 234double logb(double); 235double nextafter(double, double); 236double remainder(double, double); 237double rint(double); 238double scalb(double, double); 239#endif /* (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)*/ 240#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */ 241 242/* 243 * ISO C99 244 */ 245#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \ 246 !defined(_XOPEN_SOURCE) || \ 247 ((__STDC_VERSION__ - 0) >= 199901L) || \ 248 ((_POSIX_C_SOURCE - 0) >= 200112L) || \ 249 ((_XOPEN_SOURCE - 0) >= 600) || \ 250 defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE) 251/* 7.12.3.1 int fpclassify(real-floating x) */ 252#define fpclassify(__x) __fpmacro_unary_floating(fpclassify, __x) 253 254/* 7.12.3.2 int isfinite(real-floating x) */ 255#define isfinite(__x) __fpmacro_unary_floating(isfinite, __x) 256 257/* 7.12.3.5 int isnormal(real-floating x) */ 258#define isnormal(__x) (fpclassify(__x) == FP_NORMAL) 259 260/* 7.12.3.6 int signbit(real-floating x) */ 261#define signbit(__x) __fpmacro_unary_floating(signbit, __x) 262 263/* 7.12.4 trigonometric */ 264 265float acosf(float); 266float asinf(float); 267float atanf(float); 268float atan2f(float, float); 269float cosf(float); 270float sinf(float); 271float tanf(float); 272 273/* 7.12.5 hyperbolic */ 274 275float acoshf(float); 276float asinhf(float); 277float atanhf(float); 278float coshf(float); 279float sinhf(float); 280float tanhf(float); 281 282/* 7.12.6 exp / log */ 283 284float expf(float); 285float expm1f(float); 286float frexpf(float, int *); 287int ilogbf(float); 288float ldexpf(float, int); 289float logf(float); 290float log2f(float); 291float log10f(float); 292float log1pf(float); 293float logbf(float); 294float modff(float, float *); 295float scalbnf(float, int); 296 297/* 7.12.7 power / absolute */ 298 299float cbrtf(float); 300float fabsf(float); 301float hypotf(float, float); 302float powf(float, float); 303float sqrtf(float); 304 305/* 7.12.8 error / gamma */ 306 307float erff(float); 308float erfcf(float); 309float lgammaf(float); 310 311/* 7.12.9 nearest integer */ 312 313float ceilf(float); 314float floorf(float); 315float rintf(float); 316double round(double); 317float roundf(float); 318double trunc(double); 319float truncf(float); 320long int lrint(double); 321long int lrintf(float); 322/* LONGLONG */ 323long long int llrint(double); 324/* LONGLONG */ 325long long int llrintf(float); 326long int lround(double); 327long int lroundf(float); 328/* LONGLONG */ 329long long int llround(double); 330/* LONGLONG */ 331long long int llroundf(float); 332 333/* 7.12.10 remainder */ 334 335float fmodf(float, float); 336float remainderf(float, float); 337 338/* 7.12.11 manipulation */ 339 340float copysignf(float, float); 341double nan(const char *); 342float nanf(const char *); 343long double nanl(const char *); 344float nextafterf(float, float); 345 346/* 7.12.14 comparision */ 347 348#define isunordered(x, y) (isnan(x) || isnan(y)) 349#define isgreater(x, y) (!isunordered((x), (y)) && (x) > (y)) 350#define isgreaterequal(x, y) (!isunordered((x), (y)) && (x) >= (y)) 351#define isless(x, y) (!isunordered((x), (y)) && (x) < (y)) 352#define islessequal(x, y) (!isunordered((x), (y)) && (x) <= (y)) 353#define islessgreater(x, y) (!isunordered((x), (y)) && \ 354 ((x) > (y) || (y) > (x))) 355double fdim(double, double); 356double fmax(double, double); 357double fmin(double, double); 358float fdimf(float, float); 359float fmaxf(float, float); 360float fminf(float, float); 361long double fdiml(long double, long double); 362long double fmaxl(long double, long double); 363long double fminl(long double, long double); 364 365#endif /* !_ANSI_SOURCE && ... */ 366 367#if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) || \ 368 !defined(_XOPEN_SOURCE) || \ 369 ((__STDC_VERSION__ - 0) >= 199901L) || \ 370 ((_POSIX_C_SOURCE - 0) >= 200112L) || \ 371 defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE) 372/* 7.12.3.3 int isinf(real-floating x) */ 373#ifdef __isinf 374#define isinf(__x) __isinf(__x) 375#else 376#define isinf(__x) __fpmacro_unary_floating(isinf, __x) 377#endif 378 379/* 7.12.3.4 int isnan(real-floating x) */ 380#ifdef __isnan 381#define isnan(__x) __isnan(__x) 382#else 383#define isnan(__x) __fpmacro_unary_floating(isnan, __x) 384#endif 385#endif /* !_ANSI_SOURCE && ... */ 386 387#if defined(_NETBSD_SOURCE) 388#ifndef __cplusplus 389int matherr(struct exception *); 390#endif 391 392/* 393 * IEEE Test Vector 394 */ 395double significand(double); 396 397/* 398 * Functions callable from C, intended to support IEEE arithmetic. 399 */ 400double copysign(double, double); 401double scalbn(double, int); 402 403/* 404 * BSD math library entry points 405 */ 406double drem(double, double); 407 408#endif /* _NETBSD_SOURCE */ 409 410#if defined(_NETBSD_SOURCE) || defined(_REENTRANT) 411/* 412 * Reentrant version of gamma & lgamma; passes signgam back by reference 413 * as the second argument; user must allocate space for signgam. 414 */ 415double gamma_r(double, int *); 416double lgamma_r(double, int *); 417#endif /* _NETBSD_SOURCE || _REENTRANT */ 418 419 420#if defined(_NETBSD_SOURCE) 421 422/* float versions of ANSI/POSIX functions */ 423 424float gammaf(float); 425int isinff(float); 426int isnanf(float); 427int finitef(float); 428float j0f(float); 429float j1f(float); 430float jnf(int, float); 431float y0f(float); 432float y1f(float); 433float ynf(int, float); 434 435float scalbf(float, float); 436 437/* 438 * float version of IEEE Test Vector 439 */ 440float significandf(float); 441 442/* 443 * float versions of BSD math library entry points 444 */ 445float dremf(float, float); 446#endif /* _NETBSD_SOURCE */ 447 448#if defined(_NETBSD_SOURCE) || defined(_REENTRANT) 449/* 450 * Float versions of reentrant version of gamma & lgamma; passes 451 * signgam back by reference as the second argument; user must 452 * allocate space for signgam. 453 */ 454float gammaf_r(float, int *); 455float lgammaf_r(float, int *); 456#endif /* !... || _REENTRANT */ 457 458/* 459 * Library implementation 460 */ 461int __fpclassifyf(float); 462int __fpclassifyd(double); 463int __isfinitef(float); 464int __isfinited(double); 465int __isinff(float); 466int __isinfd(double); 467int __isnanf(float); 468int __isnand(double); 469int __signbitf(float); 470int __signbitd(double); 471 472#ifdef __HAVE_LONG_DOUBLE 473int __fpclassifyl(long double); 474int __isfinitel(long double); 475int __isinfl(long double); 476int __isnanl(long double); 477int __signbitl(long double); 478#endif 479__END_DECLS 480 481#endif /* _MATH_H_ */ 482