xref: /openbsd-src/include/math.h (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: math.h,v 1.33 2014/05/03 16:13:03 martynas Exp $	*/
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 
13 /*
14  * from: @(#)fdlibm.h 5.1 93/09/24
15  */
16 
17 #ifndef _MATH_H_
18 #define _MATH_H_
19 
20 #include <sys/_types.h>
21 #include <sys/limits.h>
22 
23 __BEGIN_DECLS
24 /*
25  * ANSI/POSIX
26  */
27 extern char __infinity[];
28 #if __GNUC_PREREQ__(3, 3) && !defined(__vax__)
29 #define HUGE_VAL	__builtin_huge_val()
30 #else /* __GNUC_PREREQ__(3, 3) && !__vax__ */
31 #define HUGE_VAL	(*(double *)(void *)__infinity)
32 #endif /* __GNUC_PREREQ__(3, 3) && !__vax__ */
33 
34 /*
35  * C99
36  */
37 #if __ISO_C_VISIBLE >= 1999
38 typedef	__double_t	double_t;
39 typedef	__float_t	float_t;
40 
41 #if __GNUC_PREREQ__(3, 3) && !defined(__vax__)
42 #define	HUGE_VALF	__builtin_huge_valf()
43 #define	HUGE_VALL	__builtin_huge_vall()
44 #define	INFINITY	__builtin_inff()
45 #define	NAN		__builtin_nanf("")
46 #else /* __GNUC_PREREQ__(3, 3) && !__vax__ */
47 #ifdef __vax__
48 extern char __infinityf[];
49 #define	HUGE_VALF	(*(float *)(void *)__infinityf)
50 #else /* __vax__ */
51 #define	HUGE_VALF	((float)HUGE_VAL)
52 #endif /* __vax__ */
53 #define	HUGE_VALL	((long double)HUGE_VAL)
54 #define	INFINITY	HUGE_VALF
55 #ifndef __vax__
56 extern char __nan[];
57 #define	NAN		(*(float *)(void *)__nan)
58 #endif /* !__vax__ */
59 #endif /* __GNUC_PREREQ__(3, 3) && !__vax__ */
60 
61 #define	FP_INFINITE	0x01
62 #define	FP_NAN		0x02
63 #define	FP_NORMAL	0x04
64 #define	FP_SUBNORMAL	0x08
65 #define	FP_ZERO		0x10
66 
67 #define FP_ILOGB0	(-INT_MAX)
68 #define FP_ILOGBNAN	INT_MAX
69 
70 #define fpclassify(x) \
71 	((sizeof (x) == sizeof (float)) ? \
72 		__fpclassifyf(x) \
73 	: (sizeof (x) == sizeof (double)) ? \
74 		__fpclassify(x) \
75 	:	__fpclassifyl(x))
76 #define isfinite(x) \
77 	((sizeof (x) == sizeof (float)) ? \
78 		__isfinitef(x) \
79 	: (sizeof (x) == sizeof (double)) ? \
80 		__isfinite(x) \
81 	:	__isfinitel(x))
82 #define isnormal(x) \
83 	((sizeof (x) == sizeof (float)) ? \
84 		__isnormalf(x) \
85 	: (sizeof (x) == sizeof (double)) ? \
86 		__isnormal(x) \
87 	:	__isnormall(x))
88 #define signbit(x) \
89 	((sizeof (x) == sizeof (float)) ? \
90 		__signbitf(x) \
91 	: (sizeof (x) == sizeof (double)) ? \
92 		__signbit(x) \
93 	:	__signbitl(x))
94 
95 #define	isgreater(x, y)		(!isunordered((x), (y)) && (x) > (y))
96 #define	isgreaterequal(x, y)	(!isunordered((x), (y)) && (x) >= (y))
97 #define	isless(x, y)		(!isunordered((x), (y)) && (x) < (y))
98 #define	islessequal(x, y)	(!isunordered((x), (y)) && (x) <= (y))
99 #define	islessgreater(x, y)	(!isunordered((x), (y)) && \
100 					((x) > (y) || (y) > (x)))
101 #define	isunordered(x, y)	(isnan(x) || isnan(y))
102 #endif /* __ISO_C_VISIBLE >= 1999 */
103 
104 #define isinf(x) \
105 	((sizeof (x) == sizeof (float)) ? \
106 		__isinff(x) \
107 	: (sizeof (x) == sizeof (double)) ? \
108 		__isinf(x) \
109 	:	__isinfl(x))
110 #define isnan(x) \
111 	((sizeof (x) == sizeof (float)) ? \
112 		__isnanf(x) \
113 	: (sizeof (x) == sizeof (double)) ? \
114 		__isnan(x) \
115 	:	__isnanl(x))
116 
117 /*
118  * XOPEN/SVID
119  */
120 #if __BSD_VISIBLE || __XPG_VISIBLE
121 #define	M_E		((double)2.7182818284590452354)  /* e */
122 #define	M_LOG2E		((double)1.4426950408889634074)  /* log 2e */
123 #define	M_LOG10E	((double)0.43429448190325182765) /* log 10e */
124 #define	M_LN2		((double)0.69314718055994530942) /* log e2 */
125 #define	M_LN10		((double)2.30258509299404568402) /* log e10 */
126 #define	M_PI		((double)3.14159265358979323846) /* pi */
127 #define	M_PI_2		((double)1.57079632679489661923) /* pi/2 */
128 #define	M_PI_4		((double)0.78539816339744830962) /* pi/4 */
129 #define	M_1_PI		((double)0.31830988618379067154) /* 1/pi */
130 #define	M_2_PI		((double)0.63661977236758134308) /* 2/pi */
131 #define	M_2_SQRTPI	((double)1.12837916709551257390) /* 2/sqrt(pi) */
132 #define	M_SQRT2		((double)1.41421356237309504880) /* sqrt(2) */
133 #define	M_SQRT1_2	((double)0.70710678118654752440) /* 1/sqrt(2) */
134 
135 #ifdef __vax__
136 #define	MAXFLOAT	((float)1.70141173319264430e+38)
137 #else
138 #define	MAXFLOAT	((float)3.40282346638528860e+38)
139 #endif /* __vax__ */
140 
141 extern int signgam;
142 #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
143 
144 #if __POSIX_VISIBLE >= 201403
145 #define	M_El		2.718281828459045235360287471352662498L /* e */
146 #define	M_LOG2El	1.442695040888963407359924681001892137L /* log 2e */
147 #define	M_LOG10El	0.434294481903251827651128918916605082L /* log 10e */
148 #define	M_LN2l		0.693147180559945309417232121458176568L /* log e2 */
149 #define	M_LN10l		2.302585092994045684017991454684364208L /* log e10 */
150 #define	M_PIl		3.141592653589793238462643383279502884L /* pi */
151 #define	M_PI_2l		1.570796326794896619231321691639751442L /* pi/2 */
152 #define	M_PI_4l		0.785398163397448309615660845819875721L /* pi/4 */
153 #define	M_1_PIl		0.318309886183790671537767526745028724L /* 1/pi */
154 #define	M_2_PIl		0.636619772367581343075535053490057448L /* 2/pi */
155 #define	M_2_SQRTPIl	1.128379167095512573896158903121545172L /* 2/sqrt(pi) */
156 #define	M_SQRT2l	1.414213562373095048801688724209698079L /* sqrt(2) */
157 #define	M_SQRT1_2l	0.707106781186547524400844362104849039L /* 1/sqrt(2) */
158 #endif /* __POSIX_VISIBLE >= 201403 */
159 
160 #if __BSD_VISIBLE
161 #define	HUGE		MAXFLOAT
162 #endif /* __BSD_VISIBLE */
163 
164 /*
165  * ANSI/POSIX
166  */
167 double acos(double);
168 double asin(double);
169 double atan(double);
170 double atan2(double, double);
171 double cos(double);
172 double sin(double);
173 double tan(double);
174 
175 double cosh(double);
176 double sinh(double);
177 double tanh(double);
178 
179 double exp(double);
180 double frexp(double, int *);
181 double ldexp(double, int);
182 double log(double);
183 double log10(double);
184 double modf(double, double *);
185 
186 double pow(double, double);
187 double sqrt(double);
188 
189 double ceil(double);
190 double fabs(double);
191 double floor(double);
192 double fmod(double, double);
193 
194 /*
195  * C99
196  */
197 #if __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XPG_VISIBLE
198 double acosh(double);
199 double asinh(double);
200 double atanh(double);
201 
202 double exp2(double);
203 double expm1(double);
204 int ilogb(double);
205 double log1p(double);
206 double log2(double);
207 double logb(double);
208 double scalbn(double, int);
209 double scalbln(double, long int);
210 
211 double cbrt(double);
212 double hypot(double, double);
213 
214 double erf(double);
215 double erfc(double);
216 double lgamma(double);
217 double tgamma(double);
218 
219 double nearbyint(double);
220 double rint(double);
221 long int lrint(double);
222 long long int llrint(double);
223 double round(double);
224 long int lround(double);
225 long long int llround(double);
226 double trunc(double);
227 
228 double remainder(double, double);
229 double remquo(double, double, int *);
230 
231 double copysign(double, double);
232 double nan(const char *);
233 double nextafter(double, double);
234 double nexttoward(double, long double);
235 
236 double fdim(double, double);
237 double fmax(double, double);
238 double fmin(double, double);
239 
240 double fma(double, double, double);
241 #endif /* __BSD_VISIBLE || __ISO_C_VISIBLE >= 1999 || __XPG_VISIBLE */
242 
243 #if __BSD_VISIBLE || __XPG_VISIBLE
244 double j0(double);
245 double j1(double);
246 double jn(int, double);
247 double scalb(double, double);
248 double y0(double);
249 double y1(double);
250 double yn(int, double);
251 #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
252 
253 #if __BSD_VISIBLE || __XPG_VISIBLE <= 500
254 double gamma(double);
255 #endif /* __BSD_VISIBLE || __XPG_VISIBLE <= 500 */
256 
257 /*
258  * BSD math library entry points
259  */
260 #if __BSD_VISIBLE
261 double drem(double, double);
262 int finite(double);
263 
264 /*
265  * Reentrant version of gamma & lgamma; passes signgam back by reference
266  * as the second argument; user must allocate space for signgam.
267  */
268 double gamma_r(double, int *);
269 double lgamma_r(double, int *);
270 
271 /*
272  * IEEE Test Vector
273  */
274 double significand(double);
275 #endif /* __BSD_VISIBLE */
276 
277 /*
278  * Float versions of C99 functions
279  */
280 #if __ISO_C_VISIBLE >= 1999
281 float acosf(float);
282 float asinf(float);
283 float atanf(float);
284 float atan2f(float, float);
285 float cosf(float);
286 float sinf(float);
287 float tanf(float);
288 
289 float acoshf(float);
290 float asinhf(float);
291 float atanhf(float);
292 float coshf(float);
293 float sinhf(float);
294 float tanhf(float);
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 log10f(float);
304 float log1pf(float);
305 float log2f(float);
306 float logbf(float);
307 float modff(float, float *);
308 float scalbnf(float, int);
309 float scalblnf(float, long int);
310 
311 float cbrtf(float);
312 float fabsf(float);
313 float hypotf(float, float);
314 float powf(float, float);
315 float sqrtf(float);
316 
317 float erff(float);
318 float erfcf(float);
319 float lgammaf(float);
320 float tgammaf(float);
321 
322 float ceilf(float);
323 float floorf(float);
324 float nearbyintf(float);
325 float rintf(float);
326 long int lrintf(float);
327 long long int llrintf(float);
328 float roundf(float);
329 long int lroundf(float);
330 long long int llroundf(float);
331 float truncf(float);
332 
333 float fmodf(float, float);
334 float remainderf(float, float);
335 float remquof(float, float, int *);
336 
337 float copysignf(float, float);
338 float nanf(const char *);
339 float nextafterf(float, float);
340 float nexttowardf(float, long double);
341 
342 float fdimf(float, float);
343 float fmaxf(float, float);
344 float fminf(float, float);
345 
346 float fmaf(float, float, float);
347 #endif /* __ISO_C_VISIBLE >= 1999 */
348 
349 #if __BSD_VISIBLE || __XPG_VISIBLE
350 float j0f(float);
351 float j1f(float);
352 float jnf(int, float);
353 float scalbf(float, float);
354 float y0f(float);
355 float y1f(float);
356 float ynf(int, float);
357 #endif /* __BSD_VISIBLE || __XPG_VISIBLE */
358 
359 #if __BSD_VISIBLE || __XPG_VISIBLE <= 500
360 float gammaf(float);
361 #endif /* __BSD_VISIBLE || __XPG_VISIBLE <= 500 */
362 
363 /*
364  * Float versions of BSD math library entry points
365  */
366 #if __BSD_VISIBLE
367 float dremf(float, float);
368 int finitef(float);
369 int isinff(float);
370 int isnanf(float);
371 
372 /*
373  * Float versions of reentrant version of gamma & lgamma; passes
374  * signgam back by reference as the second argument; user must
375  * allocate space for signgam.
376  */
377 float gammaf_r(float, int *);
378 float lgammaf_r(float, int *);
379 
380 /*
381  * Float version of IEEE Test Vector
382  */
383 float significandf(float);
384 #endif /* __BSD_VISIBLE */
385 
386 /*
387  * Long double versions of C99 functions
388  */
389 #if __ISO_C_VISIBLE >= 1999
390 long double acosl(long double);
391 long double asinl(long double);
392 long double atanl(long double);
393 long double atan2l(long double, long double);
394 long double cosl(long double);
395 long double sinl(long double);
396 long double tanl(long double);
397 
398 long double acoshl(long double);
399 long double asinhl(long double);
400 long double atanhl(long double);
401 long double coshl(long double);
402 long double sinhl(long double);
403 long double tanhl(long double);
404 
405 long double expl(long double);
406 long double exp2l(long double);
407 long double expm1l(long double);
408 long double frexpl(long double, int *);
409 int ilogbl(long double);
410 long double ldexpl(long double, int);
411 long double logl(long double);
412 long double log10l(long double);
413 long double log1pl(long double);
414 long double log2l(long double);
415 long double logbl(long double);
416 long double modfl(long double, long double *);
417 long double scalbnl(long double, int);
418 long double scalblnl(long double, long int);
419 
420 long double cbrtl(long double);
421 long double fabsl(long double);
422 long double hypotl(long double, long double);
423 long double powl(long double, long double);
424 long double sqrtl(long double);
425 
426 long double erfl(long double);
427 long double erfcl(long double);
428 long double lgammal(long double);
429 long double tgammal(long double);
430 
431 long double ceill(long double);
432 long double floorl(long double);
433 long double nearbyintl(long double);
434 long double rintl(long double);
435 long int lrintl(long double);
436 long long int llrintl(long double);
437 long double roundl(long double);
438 long int lroundl(long double);
439 long long int llroundl(long double);
440 long double truncl(long double);
441 
442 long double fmodl(long double, long double);
443 long double remainderl(long double, long double);
444 long double remquol(long double, long double, int *);
445 
446 long double copysignl(long double, long double);
447 long double nanl(const char *);
448 long double nextafterl(long double, long double);
449 long double nexttowardl(long double, long double);
450 
451 long double fdiml(long double, long double);
452 long double fmaxl(long double, long double);
453 long double fminl(long double, long double);
454 
455 long double fmal(long double, long double, long double);
456 #endif /* __ISO_C_VISIBLE >= 1999 */
457 
458 /*
459  * Library implementation
460  */
461 int __fpclassify(double);
462 int __fpclassifyf(float);
463 int __fpclassifyl(long double);
464 int __isfinite(double);
465 int __isfinitef(float);
466 int __isfinitel(long double);
467 int __isinf(double);
468 int __isinff(float);
469 int __isinfl(long double);
470 int __isnan(double);
471 int __isnanf(float);
472 int __isnanl(long double);
473 int __isnormal(double);
474 int __isnormalf(float);
475 int __isnormall(long double);
476 int __signbit(double);
477 int __signbitf(float);
478 int __signbitl(long double);
479 
480 #if __BSD_VISIBLE && defined(__vax__)
481 double infnan(int);
482 #endif /* __BSD_VISIBLE && defined(__vax__) */
483 __END_DECLS
484 
485 #endif /* !_MATH_H_ */
486