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