xref: /netbsd-src/include/math.h (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: math.h,v 1.34 2004/03/04 23:42:38 kleink 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 extern __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 */
75 extern __const union __float_u __infinityf;
76 #define	HUGE_VALF	__infinityf.__val
77 
78 extern __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 extern __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)
125 extern int signgam;
126 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
127 
128 #if defined(_NETBSD_SOURCE)
129 enum 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  */
141 extern  _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
149 struct exception {
150 	int type;
151 	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  */
180 double	acos __P((double));
181 double	asin __P((double));
182 double	atan __P((double));
183 double	atan2 __P((double, double));
184 double	cos __P((double));
185 double	sin __P((double));
186 double	tan __P((double));
187 
188 double	cosh __P((double));
189 double	sinh __P((double));
190 double	tanh __P((double));
191 
192 double	exp __P((double));
193 double	frexp __P((double, int *));
194 double	ldexp __P((double, int));
195 double	log __P((double));
196 double	log10 __P((double));
197 double	modf __P((double, double *));
198 
199 double	pow __P((double, double));
200 double	sqrt __P((double));
201 
202 double	ceil __P((double));
203 double	fabs __P((double));
204 double	floor __P((double));
205 double	fmod __P((double, double));
206 
207 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
208 double	erf __P((double));
209 double	erfc __P((double));
210 double	gamma __P((double));
211 double	hypot __P((double, double));
212 int	finite __P((double));
213 double	j0 __P((double));
214 double	j1 __P((double));
215 double	jn __P((int, double));
216 double	lgamma __P((double));
217 double	y0 __P((double));
218 double	y1 __P((double));
219 double	yn __P((int, double));
220 
221 #if (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
222 double	acosh __P((double));
223 double	asinh __P((double));
224 double	atanh __P((double));
225 double	cbrt __P((double));
226 double	expm1 __P((double));
227 int	ilogb __P((double));
228 double	log1p __P((double));
229 double	logb __P((double));
230 double	nextafter __P((double, double));
231 double	remainder __P((double, double));
232 double	rint __P((double));
233 double	scalb __P((double, double));
234 #endif /* (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)*/
235 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
236 
237 /*
238  * ISO C99
239  */
240 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) && \
241     !defined(_XOPEN_SOURCE) || \
242     ((__STDC_VERSION__ - 0) >= 199901L) || \
243     ((_POSIX_C_SOURCE - 0) >= 200112L) || \
244     ((_XOPEN_SOURCE  - 0) >= 600) || \
245     defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
246 /* 7.12.3.1 int fpclassify(real-floating x) */
247 #define	fpclassify(__x)	__fpmacro_unary_floating(fpclassify, __x)
248 
249 /* 7.12.3.2 int isfinite(real-floating x) */
250 #define	isfinite(__x)	__fpmacro_unary_floating(isfinite, __x)
251 
252 /* 7.12.3.5 int isnormal(real-floating x) */
253 #define	isnormal(__x)	(fpclassify(__x) == FP_NORMAL)
254 
255 /* 7.12.3.6 int signbit(real-floating x) */
256 #define	signbit(__x)	__fpmacro_unary_floating(signbit, __x)
257 
258 #endif /* !_ANSI_SOURCE && ... */
259 
260 #if !defined(_ANSI_SOURCE) && !defined(_POSIX_C_SOURCE) || \
261     !defined(_XOPEN_SOURCE) || \
262     ((__STDC_VERSION__ - 0) >= 199901L) || \
263     ((_POSIX_C_SOURCE - 0) >= 200112L) || \
264     defined(_ISOC99_SOURCE) || defined(_NETBSD_SOURCE)
265 /* 7.12.3.3 int isinf(real-floating x) */
266 #ifdef __isinf
267 #define	isinf(__x)	__isinf(__x)
268 #else
269 #define	isinf(__x)	__fpmacro_unary_floating(isinf, __x)
270 #endif
271 
272 /* 7.12.3.4 int isnan(real-floating x) */
273 #ifdef __isnan
274 #define	isnan(__x)	__isnan(__x)
275 #else
276 #define	isnan(__x)	__fpmacro_unary_floating(isnan, __x)
277 #endif
278 #endif /* !_ANSI_SOURCE && ... */
279 
280 #if defined(_NETBSD_SOURCE)
281 #ifndef __cplusplus
282 int	matherr __P((struct exception *));
283 #endif
284 
285 /*
286  * IEEE Test Vector
287  */
288 double	significand __P((double));
289 
290 /*
291  * Functions callable from C, intended to support IEEE arithmetic.
292  */
293 double	copysign __P((double, double));
294 double	scalbn __P((double, int));
295 
296 /*
297  * BSD math library entry points
298  */
299 #ifndef __MATH_PRIVATE__
300 double	cabs __P((/* struct complex { double r; double i; } */));
301 #endif
302 double	drem __P((double, double));
303 
304 #endif /* _NETBSD_SOURCE */
305 
306 #if defined(_NETBSD_SOURCE) || defined(_REENTRANT)
307 /*
308  * Reentrant version of gamma & lgamma; passes signgam back by reference
309  * as the second argument; user must allocate space for signgam.
310  */
311 double	gamma_r __P((double, int *));
312 double	lgamma_r __P((double, int *));
313 #endif /* _NETBSD_SOURCE || _REENTRANT */
314 
315 
316 #if defined(_NETBSD_SOURCE)
317 
318 /* float versions of ANSI/POSIX functions */
319 float	acosf __P((float));
320 float	asinf __P((float));
321 float	atanf __P((float));
322 float	atan2f __P((float, float));
323 float	cosf __P((float));
324 float	sinf __P((float));
325 float	tanf __P((float));
326 
327 float	coshf __P((float));
328 float	sinhf __P((float));
329 float	tanhf __P((float));
330 
331 float	expf __P((float));
332 float	frexpf __P((float, int *));
333 float	ldexpf __P((float, int));
334 float	logf __P((float));
335 float	log10f __P((float));
336 float	modff __P((float, float *));
337 
338 float	powf __P((float, float));
339 float	sqrtf __P((float));
340 
341 float	ceilf __P((float));
342 float	fabsf __P((float));
343 float	floorf __P((float));
344 float	fmodf __P((float, float));
345 
346 float	erff __P((float));
347 float	erfcf __P((float));
348 float	gammaf __P((float));
349 float	hypotf __P((float, float));
350 int	isinff __P((float));
351 int	isnanf __P((float));
352 int	finitef __P((float));
353 float	j0f __P((float));
354 float	j1f __P((float));
355 float	jnf __P((int, float));
356 float	lgammaf __P((float));
357 float	y0f __P((float));
358 float	y1f __P((float));
359 float	ynf __P((int, float));
360 
361 float	acoshf __P((float));
362 float	asinhf __P((float));
363 float	atanhf __P((float));
364 float	cbrtf __P((float));
365 float	logbf __P((float));
366 float	nextafterf __P((float, float));
367 float	remainderf __P((float, float));
368 float	scalbf __P((float, float));
369 
370 /*
371  * float version of IEEE Test Vector
372  */
373 float	significandf __P((float));
374 
375 /*
376  * Float versions of functions callable from C, intended to support
377  * IEEE arithmetic.
378  */
379 float	copysignf __P((float, float));
380 int	ilogbf __P((float));
381 float	rintf __P((float));
382 float	scalbnf __P((float, int));
383 
384 /*
385  * float versions of BSD math library entry points
386  */
387 #ifndef __MATH_PRIVATE__
388 float	cabsf __P((/* struct complex { float r; float i; } */));
389 #endif
390 float	dremf __P((float, float));
391 float	expm1f __P((float));
392 float	log1pf __P((float));
393 #endif /* _NETBSD_SOURCE */
394 
395 #if defined(_NETBSD_SOURCE) || defined(_REENTRANT)
396 /*
397  * Float versions of reentrant version of gamma & lgamma; passes
398  * signgam back by reference as the second argument; user must
399  * allocate space for signgam.
400  */
401 float	gammaf_r __P((float, int *));
402 float	lgammaf_r __P((float, int *));
403 #endif /* !... || _REENTRANT */
404 
405 /*
406  * Library implementation
407  */
408 int	__fpclassifyf __P((float));
409 int	__fpclassifyd __P((double));
410 int	__isfinitef __P((float));
411 int	__isfinited __P((double));
412 int	__isinff __P((float));
413 int	__isinfd __P((double));
414 int	__isnanf __P((float));
415 int	__isnand __P((double));
416 int	__signbitf __P((float));
417 int	__signbitd __P((double));
418 
419 #ifdef __HAVE_LONG_DOUBLE
420 int	__fpclassifyl __P((long double));
421 int	__isfinitel __P((long double));
422 int	__isinfl __P((long double));
423 int	__isnanl __P((long double));
424 int	__signbitl __P((long double));
425 #endif
426 __END_DECLS
427 
428 #endif /* _MATH_H_ */
429