2 Copyright (c) 2009 Rudolf Polzer
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 int fpclassify(float x)
35 return !(isnan(x) || isinf(x));
39 return (x != 0) && (x + x == x);
43 return isunordered(x, x);
56 return log(x + sqrt(x*x - 1));
60 return log(x + sqrt(x*x + 1));
64 return 0.5 * log((1+x) / (1-x));
68 return 0.5 * (exp(x) + exp(-x));
72 return 0.5 * (exp(x) - exp(-x));
76 return sinh(x) / cosh(x);
102 return floor(log2(fabs(x)));
104 float ldexp(float x, int e)
106 return x * pow(2, e);
110 // TODO improve speed
118 return -log(1 / x); // faster
120 return 2 * log(sqrt(x)); // faster
123 for(i = 1; fabs(r - r0) >= 0.00001; ++i)
125 // Newton iteration on exp(r) = x:
126 // r <- r - (exp(r) - x) / (exp(r))
127 // r <- r - 1 + x / exp(r)
129 r = r0 - 1 + x / exp(r0);
135 return log(x) * M_LOG10E;
143 return log(x) * M_LOG2E;
147 return floor(log2(fabs(x)));
151 return '1 0 0' * (f - trunc(f)) + '0 1 0' * trunc(f);
154 float scalbn(float x, int n)
156 return x * pow(2, n);
161 return copysign(pow(fabs(x), 1.0/3.0), x);
163 float hypot(float x, float y)
165 return sqrt(x*x + y*y);
170 // approximation taken from wikipedia
173 return copysign(sqrt(1 - exp(-y * (1.273239544735163 + 0.14001228868667 * y) / (1 + 0.14001228868667 * y))), x);
179 vector lgamma(float x)
181 // TODO improve accuracy
184 if(x < 1 && x == floor(x))
185 return nan("gamma") * '1 1 1';
190 // reflection formula:
191 // gamma(1-z) * gamma(z) = pi / sin(pi*z)
192 // lgamma(1-z) + lgamma(z) = log(pi) - log(sin(pi*z))
193 // sign of gamma(1-z) = sign of gamma(z) * sign of sin(pi*z)
195 v_x = log(M_PI) - log(fabs(v_z)) - v_x;
202 return lgamma(x + 1) - log(x) * '1 0 0';
204 return (0.5 * log(2 * M_PI * x) + x * (log(x) - 1)) * '1 0 0' + '0 1 0';
206 float tgamma(float x)
210 return exp(v_x) * v_y;
213 float nearbyint(float x)
219 return (x>=0) ? floor(x) : ceil(x);
222 float fmod(float x, float y)
224 return x - y * trunc(x / y);
226 float remainder(float x, float y)
228 return x - y * rint(x / y);
230 vector remquo(float x, float y)
239 float copysign(float x, float y)
241 return fabs(x) * ((y>0) ? 1 : -1);
243 float nan(string tag)
247 float nextafter(float x, float y)
251 return nan("nextafter");
253 return -nextafter(-x, -y);
254 // now we know that x < y
255 // so we need the next number > x
257 d = max(fabs(x), 0.00000000000000000000001);
268 float nexttoward(float x, float y)
270 return nextafter(x, y);
273 float fdim(float x, float y)
277 float fmax(float x, float y)
281 float fmin(float x, float y)
285 float fma(float x, float y, float z)
290 int isgreater(float x, float y)
294 int isgreaterequal(float x, float y)
298 int isless(float x, float y)
302 int islessequal(float x, float y)
306 int islessgreater(float x, float y)
308 return x < y || x > y;
310 int isunordered(float x, float y)
312 return !(x < y || x == y || x > y);