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);
171 // approximation taken from wikipedia
174 return copysign(sqrt(1 - exp(-y * (1.273239544735163 + 0.14001228868667 * y) / (1 + 0.14001228868667 * y))), x);
180 vector lgamma(float x)
182 // TODO improve accuracy
183 if(x < 1 && x == floor(x))
184 return nan("gamma") * '1 1 1';
189 // reflection formula:
190 // gamma(1-z) * gamma(z) = pi / sin(pi*z)
191 // lgamma(1-z) + lgamma(z) = log(pi) - log(sin(pi*z))
192 // sign of gamma(1-z) = sign of gamma(z) * sign of sin(pi*z)
194 v_x = log(M_PI) - log(fabs(v_z)) - v_x;
201 return lgamma(x + 1) - log(x) * '1 0 0';
203 return (0.5 * log(2 * M_PI * x) + x * (log(x) - 1)) * '1 0 0' + '0 1 0';
205 float tgamma(float x)
209 return exp(v_x) * v_y;
212 float nearbyint(float x)
218 return (x>=0) ? floor(x) : ceil(x);
221 float fmod(float x, float y)
223 return x - y * trunc(x / y);
225 float remainder(float x, float y)
227 return x - y * rint(x / y);
229 vector remquo(float x, float y)
238 float copysign(float x, float y)
240 return fabs(x) * ((y>0) ? 1 : -1);
242 float nan(string tag)
246 float nextafter(float x, float y)
250 return nan("nextafter");
252 return -nextafter(-x, -y);
253 // now we know that x < y
254 // so we need the next number > x
256 d = max(fabs(x), 0.00000000000000000000001);
267 float nexttoward(float x, float y)
269 return nextafter(x, y);
272 float fdim(float x, float y)
276 float fmax(float x, float y)
280 float fmin(float x, float y)
284 float fma(float x, float y, float z)
289 int isgreater(float x, float y)
293 int isgreaterequal(float x, float y)
297 int isless(float x, float y)
301 int islessequal(float x, float y)
305 int islessgreater(float x, float y)
307 return x < y || x > y;
309 int isunordered(float x, float y)
311 return !(x < y || x == y || x > y);