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