2 #include "../dpdefs/csprogsdefs.qh"
6 #include "../dpdefs/progsdefs.qh"
7 #include "../dpdefs/dpextensions.qh"
11 int fpclassify(float x)
21 bool isfinite(float x)
23 return !(isnan(x) || isinf(x));
27 return (x != 0) && (x + x == x);
35 bool isnormal(float x)
46 return log(x + sqrt(x*x - 1));
50 return log(x + sqrt(x*x + 1));
54 return 0.5 * log((1+x) / (1-x));
58 return 0.5 * (exp(x) + exp(-x));
62 return 0.5 * (exp(x) - exp(-x));
66 return sinh(x) / cosh(x);
92 return floor(log2(fabs(x)));
94 float ldexp(float x, int e)
100 return log(x) * M_LOG10E;
108 return log(x) * M_LOG2E;
112 return floor(log2(fabs(x)));
116 return '1 0 0' * (f - trunc(f)) + '0 1 0' * trunc(f);
119 float scalbn(float x, int n)
121 return x * pow(2, n);
126 return copysign(pow(fabs(x), 1.0/3.0), x);
128 float hypot(float x, float y)
130 return sqrt(x*x + y*y);
135 // approximation taken from wikipedia
138 return copysign(sqrt(1 - exp(-y * (1.273239544735163 + 0.14001228868667 * y) / (1 + 0.14001228868667 * y))), x);
144 vector lgamma(float x)
146 // TODO improve accuracy
148 return fabs(x) * '1 0 0' + copysign(1, x) * '0 1 0';
149 if(x < 1 && x == floor(x))
150 return nan("gamma") * '1 1 1';
155 // reflection formula:
156 // gamma(1-z) * gamma(z) = pi / sin(pi*z)
157 // lgamma(1-z) + lgamma(z) = log(pi) - log(sin(pi*z))
158 // sign of gamma(1-z) = sign of gamma(z) * sign of sin(pi*z)
160 v_x = log(M_PI) - log(fabs(v.z)) - v.x;
167 return lgamma(x + 1) - log(x) * '1 0 0';
169 return (0.5 * log(2 * M_PI * x) + x * (log(x) - 1)) * '1 0 0' + '0 1 0';
171 float tgamma(float x)
175 return exp(v.x) * v.y;
178 float nearbyint(float x)
184 return (x>=0) ? floor(x) : ceil(x);
187 float fmod(float x, float y)
189 return x - y * trunc(x / y);
191 float remainder(float x, float y)
193 return x - y * rint(x / y);
195 vector remquo(float x, float y)
204 float copysign(float x, float y)
206 return fabs(x) * ((y>0) ? 1 : -1);
208 float nan(string tag)
212 float nextafter(float x, float y)
216 return nan("nextafter");
218 return -nextafter(-x, -y);
219 // now we know that x < y
220 // so we need the next number > x
222 d = max(fabs(x), 0.00000000000000000000001);
233 float nexttoward(float x, float y)
235 return nextafter(x, y);
238 float fdim(float x, float y)
242 float fmax(float x, float y)
246 float fmin(float x, float y)
250 float fma(float x, float y, float z)
255 int isgreater(float x, float y)
259 int isgreaterequal(float x, float y)
263 int isless(float x, float y)
267 int islessequal(float x, float y)
271 int islessgreater(float x, float y)
273 return x < y || x > y;
275 int isunordered(float x, float y)
277 return !(x < y || x == y || x > y);