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(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(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
182 if(x < 1 && x == floor(x))
183 return nan("gamma") * '1 1 1';
188 // reflection formula:
189 // gamma(1-z) * gamma(z) = pi / sin(pi*z)
190 // lgamma(1-z) + lgamma(z) = log(pi) - log(sin(pi*z))
191 // sign of gamma(1-z) = sign of gamma(z) * sign of sin(pi*z)
193 v_x = log(M_PI) - log(fabs(v_z)) - v_x;
200 return lgamma(x + 1) - log(x) * '1 0 0';
202 return (0.5 * log(2 * M_PI * x) + x * (log(x) - 1)) * '1 0 0' + '0 1 0';
204 float tgamma(float x)
208 return exp(v_x) * v_y;
211 float nearbyint(float x)
217 return (x>=0) ? floor(x) : ceil(x);
220 float fmod(float x, float y)
222 return x - y * trunc(x / y);
224 float remainder(float x, float y)
226 return x - y * rint(x / y);
228 vector remquo(float x, float y)
237 float copysign(float x, float y)
239 return fabs(x) * ((y>0) ? 1 : -1);
241 float nan(string tag)
245 float nextafter(float x, float y)
249 return nan("nextafter");
251 return -nextafter(-x, -y);
252 // now we know that x < y
253 // so we need the next number > x
255 d = max(fabs(x), 0.00000000000000000000001);
266 float nexttoward(float x, float y)
268 return nextafter(x, y);
271 float fdim(float x, float y)
275 float fmax(float x, float y)
279 float fmin(float x, float y)
283 float fma(float x, float y, float z)
288 int isgreater(float x, float y)
292 int isgreaterequal(float x, float y)
296 int isless(float x, float y)
300 int islessequal(float x, float y)
304 int islessgreater(float x, float y)
306 return x < y || x > y;
308 int isunordered(float x, float y)
310 return !(x < y || x == y || x > y);