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.0000001; ++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);
131 print(ftos(i), "\n");
136 return log(x) * M_LOG10E;
144 return log(x) * M_LOG2E;
148 return floor(log2(x));
152 return '1 0 0' * (f - trunc(f)) + '0 1 0' * trunc(f);
155 float scalbn(float x, int n)
157 return x * pow(2, n);
162 return pow(x, 1.0/3.0);
164 float hypot(float x, float y)
166 return sqrt(x*x + y*y);
168 //float pow(float x, float y);
169 //float sqrt(float x, float y);
173 // approximation taken from wikipedia
176 return copysign(sqrt(1 - exp(-y * (1.273239544735163 + 0.14001228868667 * y) / (1 + 0.14001228868667 * y))), x);
182 vector lgamma(float x)
184 // TODO improve accuracy
185 if(x < 1 && x == floor(x))
186 return nan("gamma") * '1 1 1';
191 // reflection formula:
192 // gamma(1-z) * gamma(z) = pi / sin(pi*z)
193 // lgamma(1-z) + lgamma(z) = log(pi) - log(sin(pi*z))
194 // sign of gamma(1-z) = sign of gamma(z) * sign of sin(pi*z)
196 v_x = log(M_PI) - log(fabs(v_z)) - v_x;
203 return lgamma(x + 1) - log(x) * '1 0 0';
205 return (0.5 * log(2 * M_PI * x) + x * (log(x) - 1)) * '1 0 0' + '0 1 0';
207 float tgamma(float x)
211 return exp(v_x) * v_y;
214 //float ceil(float x);
215 //float floor(float x);
216 float nearbyint(float x)
220 //float rint(float x);
221 //float round(float x);
224 return (x>=0) ? floor(x) : ceil(x);
227 float fmod(float x, float y)
229 return x - y * trunc(x / y);
231 float remainder(float x, float y)
233 return x - y * rint(x / y);
235 vector remquo(float x, float y)
244 float copysign(float x, float y)
246 return fabs(x) * ((y>0) ? 1 : -1);
248 float nan(string tag)
252 float nextafter(float x, float y)
256 return nan("nextafter");
258 return -nextafter(-x, -y);
259 // now we know that x < y
260 // so we need the next number > x
262 d = max(fabs(x), 0.00000000000000000000001);
273 float nexttoward(float x, float y)
275 return nextafter(x, y);
278 float fdim(float x, float y)
282 float fmax(float x, float y)
286 float fmin(float x, float y)
290 float fma(float x, float y, float z)
295 int isgreater(float x, float y)
299 int isgreaterequal(float x, float y)
303 int isless(float x, float y)
307 int islessequal(float x, float y)
311 int islessgreater(float x, float y)
313 return x < y || x > y;
315 int isunordered(float x, float y)
317 return !(x < y || x == y || x > y);