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