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 !(x + x == 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 return log(x) * M_LOG10E;
118 return log(x) * M_LOG2E;
122 return floor(log2(fabs(x)));
126 return '1 0 0' * (f - trunc(f)) + '0 1 0' * trunc(f);
129 float scalbn(float x, int n)
131 return x * pow(2, n);
136 return copysign(pow(fabs(x), 1.0/3.0), x);
138 float hypot(float x, float y)
140 return sqrt(x*x + y*y);
145 // approximation taken from wikipedia
148 return copysign(sqrt(1 - exp(-y * (1.273239544735163 + 0.14001228868667 * y) / (1 + 0.14001228868667 * y))), x);
154 vector lgamma(float x)
156 // TODO improve accuracy
158 return fabs(x) * '1 0 0' + copysign(1, x) * '0 1 0';
159 if(x < 1 && x == floor(x))
160 return nan("gamma") * '1 1 1';
165 // reflection formula:
166 // gamma(1-z) * gamma(z) = pi / sin(pi*z)
167 // lgamma(1-z) + lgamma(z) = log(pi) - log(sin(pi*z))
168 // sign of gamma(1-z) = sign of gamma(z) * sign of sin(pi*z)
170 v_x = log(M_PI) - log(fabs(v_z)) - v_x;
177 return lgamma(x + 1) - log(x) * '1 0 0';
179 return (0.5 * log(2 * M_PI * x) + x * (log(x) - 1)) * '1 0 0' + '0 1 0';
181 float tgamma(float x)
185 return exp(v_x) * v_y;
188 float nearbyint(float x)
194 return (x>=0) ? floor(x) : ceil(x);
197 float fmod(float x, float y)
199 return x - y * trunc(x / y);
201 float remainder(float x, float y)
203 return x - y * rint(x / y);
205 vector remquo(float x, float y)
214 float copysign(float x, float y)
216 return fabs(x) * ((y>0) ? 1 : -1);
218 float nan(string tag)
222 float nextafter(float x, float y)
226 return nan("nextafter");
228 return -nextafter(-x, -y);
229 // now we know that x < y
230 // so we need the next number > x
232 d = max(fabs(x), 0.00000000000000000000001);
243 float nexttoward(float x, float y)
245 return nextafter(x, y);
248 float fdim(float x, float y)
252 float fmax(float x, float y)
256 float fmin(float x, float y)
260 float fma(float x, float y, float z)
265 int isgreater(float x, float y)
269 int isgreaterequal(float x, float y)
273 int isless(float x, float y)
277 int islessequal(float x, float y)
281 int islessgreater(float x, float y)
283 return x < y || x > y;
285 int isunordered(float x, float y)
287 return !(x < y || x == y || x > y);