1 /* 2 * Copyright 2001-2005, 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 #define PI M_PI 24 #define PI2 M_PI_2 25 26 /* platform independent IEEE floating point special values */ 27 #define __HUGE_VAL_v 0x7ff0000000000000LL 28 #define __huge_val_t union { unsigned char __c[8]; long long __ll; double __d; } 29 #define HUGE_VAL (((__huge_val_t) { __ll: __HUGE_VAL_v }).__d) 30 31 #define __HUGE_VALF_v 0x7f800000L 32 #define __huge_valf_t union { unsigned char __c[4]; long __l; float __f; } 33 #define HUGE_VALF (((__huge_valf_t) { __l: __HUGE_VALF_v }).__f) 34 35 // ToDo: define HUGE_VALL for long doubles 36 37 #define __NAN_VALF_v 0x7fc00000L 38 #define NAN (((__huge_valf_t) { __l: __NAN_VALF_v }).__f) 39 40 #define INFINITY HUGE_VALF 41 42 /* floating-point categories */ 43 #define FP_NAN 0 44 #define FP_INFINITE 1 45 #define FP_ZERO 2 46 #define FP_SUBNORMAL 3 47 #define FP_NORMAL 4 48 49 #ifdef __cplusplus 50 struct __exception; 51 extern "C" int matherr(struct __exception *); 52 struct __exception { 53 #else 54 struct exception; 55 extern int matherr(struct exception *); 56 struct exception { 57 #endif 58 int type; 59 char *name; 60 double arg1; 61 double arg2; 62 double retval; 63 }; 64 65 #define DOMAIN 1 66 #define SING 2 67 #define OVERFLOW 3 68 #define UNDERFLOW 4 69 #define TLOSS 5 70 #define PLOSS 6 71 72 extern int signgam; 73 74 75 #ifdef __cplusplus 76 extern "C" { 77 #endif 78 79 /* float math functions */ 80 extern float acosf(float x); 81 extern float acoshf(float x); 82 extern float asinf(float x); 83 extern float asinhf(float x); 84 extern float atan2f(float y, float x); 85 extern float atanf(float x); 86 extern float atanhf(float x); 87 extern float cbrtf(float x); 88 extern float ceilf(float x); 89 extern float cosf(float x); 90 extern float coshf(float x); 91 extern float expf(float x); 92 extern float expm1f(float x); 93 extern float fabsf(float x); 94 extern float floorf(float x); 95 extern float fmodf(float x, float y); 96 extern float frexpf(float x, int *_exponent); 97 extern float gammaf(float x); 98 extern float hypotf(float x, float y); 99 extern float ldexpf(float x, int exponent); 100 extern float lgammaf(float x); 101 extern float log10f(float x); 102 extern float log1pf(float x); 103 extern float logbf(float x); 104 extern float logf(float x); 105 extern float modff(float x, float *y); 106 extern float powf(float x, float y); 107 extern float roundf(float x); 108 extern float sinf(float x); 109 extern float sinhf(float x); 110 extern float sqrtf(float x); 111 extern float tanf(float x); 112 extern float tanhf(float x); 113 114 /* double math functions */ 115 extern double acos(double x); 116 extern double asin(double x); 117 extern double atan(double x); 118 extern double atan2(double x, double y); 119 extern double ceil(double x); 120 extern double cos(double x); 121 extern double cosh(double x); 122 extern double exp(double x); 123 extern double fabs(double x); 124 extern double floor(double x); 125 extern double fmod(double x, double y); 126 extern double frexp(double x, int *_exponent); 127 extern double gamma(double x); 128 extern double ldexp(double x, int exponent); 129 extern double log(double x); 130 extern double log10(double x); 131 extern double modf(double x, double *y); 132 extern double pow(double x, double y); 133 extern double round(double x); 134 extern double sin(double x); 135 extern double sinh(double x); 136 extern double sqrt(double x); 137 extern double tan(double x); 138 extern double tanh(double x); 139 140 /* long double math functions */ 141 extern long double roundl(long double x); 142 // TODO: add and fix those! 143 extern /*long*/ double lgamma(/*long*/ double x); 144 145 /* some BSD non-ANSI or POSIX math functions */ 146 extern double acosh(double x); 147 extern double asinh(double x); 148 extern double atanh(double x); 149 extern double cbrt(double x); 150 extern double erf(double x); 151 extern double erfc(double x); 152 extern double expm1(double x); 153 extern double gamma_r(double x, int *y); 154 extern double hypot(double x, double y); 155 extern int ilogb(double x); 156 extern double j0(double x); 157 extern double j1(double x); 158 extern double jn(int x, double y); 159 extern double lgamma_r(double x, int *y); 160 extern double log1p(double x); 161 extern double logb(double x); 162 extern double nextafter(double x, double y); 163 extern double remainder(double x, double y); 164 extern double rint(double x); 165 extern double scalb (double x, double y); 166 extern double y0(double x); 167 extern double y1(double x); 168 extern double yn(int x, double y); 169 170 /* other stuff as defined in BeOS */ 171 extern int isinff(float value); 172 extern int finitef(float value); 173 extern float infnanf(int error); 174 extern float dremf(float x, float y); 175 extern float significandf(float x); 176 extern float copysignf(float x, float y); 177 extern int isnanf(float value); 178 extern double significand(double x); 179 extern double copysign(double x, double y); 180 extern double scalbn(double x, int y); 181 extern double drem(double x, double y); 182 extern int isnan(double x); 183 extern int isfinite(double x); 184 extern int finite(double x); 185 extern float j0f(float x); 186 extern float j1f(float x); 187 extern float jnf(int x, float y); 188 extern float y0f(float x); 189 extern float y1f(float x); 190 extern float ynf(int x, float y); 191 extern float erff(float x); 192 extern float erfcf(float x); 193 extern float gammaf_r(float x, int *y); 194 extern float lgammaf_r(float x, int *y); 195 extern float rintf(float x); 196 extern float nextafterf(float x, float y); 197 extern float remainderf(float x, float y); 198 extern float scalbf(float x, float n); 199 extern float scalbnf(float x, int n); 200 extern int ilogbf(float x); 201 202 /* prototypes for functions used in the macros below */ 203 extern int __fpclassifyf(float value); 204 extern int __signbitf(float value); 205 extern int __finitef(float value); 206 extern int __isnanf(float value); 207 extern int __isinff(float value); 208 209 extern int __fpclassifyl(long double value); 210 extern int __signbitl(long double value); 211 extern int __finitel(long double value); 212 extern int __isnanl(long double value); 213 extern int __isinfl(long double value); 214 215 extern int __fpclassify(double value); 216 extern int __signbit(double value); 217 extern int __finite(double value); 218 extern int __isnan(double value); 219 extern int __isinf(double value); 220 221 /* returns number of classification appropriate for 'value' */ 222 #define fpclassify(value) \ 223 (sizeof(value) == sizeof(float) ? __fpclassifyf(value) \ 224 : sizeof(value) == sizeof(double) ? __fpclassify(value) \ 225 : __fpclassifyl(value)) 226 227 /* returns non-zero if 'value' is negative */ 228 # define signbit(value) \ 229 (sizeof(value) == sizeof(float) ? __signbitf(value) \ 230 : sizeof(value) == sizeof(double) ? __signbit(value) \ 231 : __signbitl(value)) 232 233 /* returns non-zero if 'value' is not Inf or NaN */ 234 # define isfinite(value) \ 235 (sizeof(value) == sizeof(float) ? __finitef(value) \ 236 : sizeof(value) == sizeof(double) ? __finite(value) \ 237 : __finitel(value)) 238 239 /* returns non-zero if 'value' is neither zero, sub-normal, Inf, nor NaN */ 240 # define isnormal(value) \ 241 (fpclassify(value) == FP_NORMAL) 242 243 /* returns non-zero if 'value' is NaN */ 244 # define isnan(value) \ 245 (sizeof(value) == sizeof(float) ? __isnanf(value) \ 246 : sizeof(value) == sizeof(double) ? __isnan(value) \ 247 : __isnanl(value)) 248 249 /* returns non-zero if 'value is Inf */ 250 # define isinf(x) \ 251 (sizeof(value) == sizeof(float) ? __isinff(value) \ 252 : sizeof(value) == sizeof(double) ? __isinf(value) \ 253 : __isinfl(value)) 254 255 #ifdef __cplusplus 256 } 257 #endif 258 259 #endif /* _MATH_H_ */ 260