xref: /haiku/headers/posix/math.h (revision e688bf23d48bfd1216a0cacbdbda5e35a1bcd779)
1 /*
2  * Copyright 2001-2012 Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _MATH_H_
6 #define _MATH_H_
7 
8 
9 #define M_E				2.7182818284590452354	/* e */
10 #define M_LOG2E			1.4426950408889634074	/* log 2e */
11 #define M_LOG10E		0.43429448190325182765	/* log 10e */
12 #define M_LN2			0.69314718055994530942	/* log e2 */
13 #define M_LN10			2.30258509299404568402	/* log e10 */
14 #define M_PI			3.14159265358979323846	/* pi */
15 #define M_PI_2			1.57079632679489661923	/* pi/2 */
16 #define M_PI_4			0.78539816339744830962	/* pi/4 */
17 #define M_1_PI			0.31830988618379067154	/* 1/pi */
18 #define M_2_PI			0.63661977236758134308	/* 2/pi */
19 #define M_2_SQRTPI		1.12837916709551257390	/* 2/sqrt(pi) */
20 #define M_SQRT2			1.41421356237309504880	/* sqrt(2) */
21 #define M_SQRT1_2		0.70710678118654752440	/* 1/sqrt(2) */
22 
23 /* platform independent IEEE floating point special values */
24 #if __GNUC__ >= 4
25 	/* gcc4 and clang definitions */
26 #	ifndef HUGE_VAL
27 #		define HUGE_VAL     __builtin_huge_val()
28 #	endif
29 
30 #	define HUGE_VALF        __builtin_huge_valf()
31 #	define HUGE_VALL		__builtin_huge_vall()
32 #	define NAN              __builtin_nanf("")
33 #	define INFINITY			__builtin_inff()
34 #else
35 	/* gcc2 definitions */
36 #	define	__HUGE_VAL_v	0x7ff0000000000000LL
37 #	define __huge_val_t	union { unsigned char __c[8]; long long __ll; double __d; }
38 #	ifndef HUGE_VAL
39 #		define HUGE_VAL		(((__huge_val_t) { __ll: __HUGE_VAL_v }).__d)
40 #	endif
41 
42 #	define __HUGE_VALF_v	0x7f800000L
43 #	define __huge_valf_t	union { unsigned char __c[4]; long __l; float __f; }
44 #	define HUGE_VALF		(((__huge_valf_t) { __l: __HUGE_VALF_v }).__f)
45 
46 	/* TODO: define HUGE_VALL for long doubles */
47 
48 #	define __NAN_VALF_v	0x7fc00000L
49 #	define NAN				(((__huge_valf_t) { __l: __NAN_VALF_v }).__f)
50 #	define INFINITY			HUGE_VALF
51 #endif
52 
53 /* floating-point categories */
54 #define FP_NAN			0
55 #define FP_INFINITE		1
56 #define FP_ZERO			2
57 #define FP_SUBNORMAL	3
58 #define FP_NORMAL		4
59 
60 #ifdef __cplusplus
61 struct __exception;
62 extern "C" int matherr(struct __exception *);
63 struct __exception {
64 #else
65 struct exception;
66 extern int matherr(struct exception *);
67 struct exception {
68 #endif
69 	int		type;
70 	char	*name;
71 	double	arg1;
72 	double	arg2;
73 	double	retval;
74 };
75 
76 #define DOMAIN		1
77 #define SING		2
78 #define OVERFLOW	3
79 #define UNDERFLOW	4
80 #define TLOSS		5
81 #define PLOSS		6
82 
83 extern int signgam;
84 
85 
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89 
90 /* float math functions */
91 extern float		acosf(float x);
92 extern float		acoshf(float x);
93 extern float		asinf(float x);
94 extern float		asinhf(float x);
95 extern float		atan2f(float y, float x);
96 extern float		atanf(float x);
97 extern float		atanhf(float x);
98 extern float		cbrtf(float x);
99 extern float		ceilf(float x);
100 extern float		copysignf(float x, float y);
101 extern float		cosf(float x);
102 extern float		coshf(float x);
103 extern float		erfcf(float x);
104 extern float		erff(float x);
105 extern float		expf(float x);
106 extern float		exp2f(float x);
107 extern float		expm1f(float x);
108 extern float		fabsf(float x);
109 extern float		fdimf(float x, float y);
110 extern float		floorf(float x);
111 extern float		fmaf(float x, float y, float z);
112 extern float		fmaxf(float x, float y);
113 extern float		fminf(float x, float y);
114 extern float		fmodf(float x, float y);
115 extern float		frexpf(float x, int *_exponent);
116 extern float		gammaf(float x);
117 extern float		hypotf(float x, float y);
118 extern int			ilogbf(float x);
119 extern float		ldexpf(float x, int exponent);
120 extern float		lgammaf(float x);
121 extern long long	llrintf(float x);
122 extern long long	llroundf(float x);
123 extern float		log10f(float x);
124 extern float		log1pf(float x);
125 extern float		log2f(float x);
126 extern float		logbf(float x);
127 extern float		logf(float x);
128 extern long			lrintf(float x);
129 extern long			lroundf(float x);
130 extern float		modff(float x, float *y);
131 extern float		nanf(const char *tagp);
132 extern float		nearbyintf(float x);
133 extern float		nextafterf(float x, float y);
134 extern float		nexttowardf(float x, long double y);
135 extern float		powf(float x, float y);
136 extern float		remainderf(float x, float y);
137 extern float		remquof(float x, float y, int *quo);
138 extern float		rintf(float x);
139 extern float		roundf(float x);
140 extern float		scalbf(float x, float n);
141 extern float		scalblnf(float x, long n);
142 extern float		scalbnf(float x, int n);
143 extern float		sinf(float x);
144 extern float		sinhf(float x);
145 extern float		sqrtf(float x);
146 extern float		tanf(float x);
147 extern float		tanhf(float x);
148 extern float		tgammaf(float x);
149 extern float		truncf(float x);
150 
151 /* double math functions */
152 extern double		acos(double x);
153 extern double		acosh(double x);
154 extern double		asin(double x);
155 extern double		asinh(double x);
156 extern double		atan(double x);
157 extern double		atan2(double x, double y);
158 extern double		atanh(double x);
159 extern double		cbrt(double x);
160 extern double		ceil(double x);
161 extern double		copysign(double x, double y);
162 extern double		cos(double x);
163 extern double		cosh(double x);
164 extern double		erf(double x);
165 extern double		erfc(double x);
166 extern double		exp(double x);
167 extern double		expm1(double x);
168 extern double		exp2(double x);
169 extern double		fabs(double x);
170 extern double		fdim(double x, double y);
171 extern double		floor(double x);
172 extern double		fma(double x, double y, double z);
173 extern double		fmax(double x, double y);
174 extern double		fmin(double x, double y);
175 extern double		fmod(double x, double y);
176 extern double		frexp(double x, int *_exponent);
177 extern double		gamma(double x);
178 extern double		hypot(double x, double y);
179 extern int			ilogb(double x);
180 extern double		ldexp(double x, int exponent);
181 extern double		lgamma(double x);
182 extern long long	llrint(double x);
183 extern long long	llround(double x);
184 extern double		log(double x);
185 extern double		log10(double x);
186 extern double		log1p(double x);
187 extern double		log2(double x);
188 extern double		logb(double x);
189 extern long			lrint(double x);
190 extern long			lround(double x);
191 extern double		modf(double x, double *y);
192 extern double		nan(const char* tagp);
193 extern double		nearbyint(double x);
194 extern double		nextafter(double x, double y);
195 extern double		nexttoward(double x, long double y);
196 extern double		pow(double x, double y);
197 extern double		remainder(double x, double y);
198 extern double		remquo(double x, double y, int *quo);
199 extern double		rint(double x);
200 extern double		round(double x);
201 extern double		scalb(double x, double y);
202 extern double 		scalbln(double x, long n);
203 extern double		scalbn(double x, int y);
204 extern double		sin(double x);
205 extern double		sinh(double x);
206 extern double		sqrt(double x);
207 extern double		tan(double x);
208 extern double		tanh(double x);
209 extern double		tgamma(double x);
210 extern double		trunc(double x);
211 
212 /* long double math functions */
213 extern long double	acoshl(long double x);
214 extern long double	acosl(long double x);
215 extern long double	asinhl(long double x);
216 extern long double	asinl(long double x);
217 extern long double	atanhl(long double x);
218 extern long double	atanl(long double x);
219 extern long double	atan2l(long double y, long double x);
220 extern long double	cbrtl(long double x);
221 extern long double	ceill(long double x);
222 extern long double	copysignl(long double x, long double y);
223 extern long double	cosl(long double x);
224 extern long double	coshl(long double x);
225 extern long double	erfcl(long double x);
226 extern long double	erfl(long double x);
227 extern long double	expl(long double x);
228 extern long double	expm1l(long double x);
229 extern long double	exp2l(long double x);
230 extern long double	fabsl(long double x);
231 extern long double	fdiml(long double x, long double y);
232 extern long double	floorl(long double x);
233 extern long double	fmal(long double x, long double y, long double z);
234 extern long double	fmaxl(long double x, long double y);
235 extern long double	fminl(long double x, long double y);
236 extern long double	fmodl(long double x, long double y);
237 extern long double	frexpl(long double num, int *_exponent);
238 extern long double	hypotl(long double x, long double y);
239 extern int			ilogbl(long double x);
240 extern long double	ldexpl(long double x, int exponent);
241 extern long double	lgammal(long double x);
242 extern long long	llrintl(long double x);
243 extern long long	llroundl(long double x);
244 extern long double	logbl(long double x);
245 extern long double	logl(long double x);
246 extern long double	log1pl(long double x);
247 extern long double	log10l(long double x);
248 extern long double	log2l(long double x);
249 extern long			lrintl(long double x);
250 extern long			lroundl(long double x);
251 extern long double	modfl(long double value, long double *iptr);
252 extern long double	nanl(const char* tagp);
253 extern long double	nearbyintl(long double x);
254 extern long double	nextafterl(long double x, long double y);
255 extern long double	nexttowardl(long double x, long double y);
256 extern long double	powl(long double x, long double y);
257 extern long double	remainderl(long double x, long double y);
258 extern long double	remquol(long double x, long double y, int *quo);
259 extern long double	rintl(long double x);
260 extern long double	roundl(long double x);
261 extern long double	scalbnl(long double x, int n);
262 extern long double	scalblnl(long double x, long n);
263 extern long double	sinl(long double x);
264 extern long double	sinhl(long double x);
265 extern long double	sqrtl(long double x);
266 extern long double	tanl(long double x);
267 extern long double	tanhl(long double x);
268 extern long double	tgammal(long double x);
269 extern long double	truncl(long double x);
270 
271 /* some BSD non-ANSI or POSIX math functions */
272 extern double		j0(double x);
273 extern double		j1(double x);
274 extern double		jn(int x, double y);
275 extern double		lgamma_r(double x, int *y);
276 extern double		y0(double x);
277 extern double		y1(double x);
278 extern double		yn(int x, double y);
279 
280 /* other stuff as defined in BeOS */
281 extern int			isinff(float value);
282 extern int			finitef(float value);
283 extern float		dremf(float x, float y);
284 extern float		significandf(float x);
285 extern int			isnanf(float value);
286 extern double		significand(double x);
287 extern double		drem(double x, double y);
288 extern int			finite(double x);
289 extern float		j0f(float x);
290 extern float		j1f(float x);
291 extern float		jnf(int x, float y);
292 extern float		y0f(float x);
293 extern float		y1f(float x);
294 extern float		ynf(int x, float y);
295 extern float		lgammaf_r(float x, int *y);
296 
297 
298 /* prototypes for functions used in the macros below */
299 extern int			__fpclassifyf(float value);
300 extern int			__signbitf(float value);
301 extern int			__finitef(float value);
302 extern int			__isnanf(float value);
303 extern int			__isinff(float value);
304 
305 extern int			__fpclassifyl(long double value);
306 extern int			__signbitl(long double value);
307 extern int			__finitel(long double value);
308 extern int			__isnanl(long double value);
309 extern int			__isinfl(long double value);
310 
311 extern int			__fpclassify(double value);
312 extern int			__signbit(double value);
313 extern int			__finite(double value);
314 extern int			__isnan(double value);
315 extern int			__isinf(double value);
316 
317 /* returns number of classification appropriate for 'value' */
318 #define fpclassify(value) \
319 	(sizeof(value) == sizeof(float) ? __fpclassifyf(value)		\
320 		: sizeof(value) == sizeof(double) ? __fpclassify(value)	\
321 		: __fpclassifyl(value))
322 
323 /* returns non-zero if 'value' is negative */
324 # define signbit(value) \
325 	(sizeof(value) == sizeof(float) ? __signbitf(value)			\
326 		: sizeof(value) == sizeof(double) ? __signbit(value)	\
327 		: __signbitl(value))
328 
329 /* returns non-zero if 'value' is not Inf or NaN */
330 # define isfinite(value) \
331 	(sizeof(value) == sizeof(float) ? __finitef(value)			\
332 		: sizeof(value) == sizeof(double) ? __finite(value)		\
333 		: __finitel(value))
334 
335 /* returns non-zero if 'value' is neither zero, sub-normal, Inf, nor NaN */
336 # define isnormal(value) \
337 	(fpclassify(value) == FP_NORMAL)
338 
339 /* returns non-zero if 'value' is NaN */
340 # define isnan(value) \
341 	(sizeof(value) == sizeof(float) ? __isnanf(value)			\
342 		: sizeof(value) == sizeof(double) ? __isnan(value)		\
343 		: __isnanl(value))
344 
345 /* returns non-zero if 'value is Inf */
346 # define isinf(value) \
347 	(sizeof(value) == sizeof(float) ? __isinff(value)			\
348 		: sizeof(value) == sizeof(double) ? __isinf(value)		\
349 		: __isinfl(value))
350 
351 #ifdef __cplusplus
352 }
353 #endif
354 
355 #endif	/* _MATH_H_ */
356