1 int fpclassify(float x)
13 return !(isnan(x) || isinf(x));
17 return (x != 0) && (x + x == x);
21 return isunordered(x, x);
34 return log(x + sqrt(x*x - 1));
38 return log(x + sqrt(x*x + 1));
42 return 0.5 * log((1+x) / (1-x));
46 return 0.5 * (exp(x) + exp(-x));
50 return 0.5 * (exp(x) - exp(-x));
54 return sinh(x) / cosh(x);
80 return floor(log2(x));
82 float ldexp(float x, int e)
96 return -log(1 / x); // faster
98 return 2 * log(sqrt(x)); // faster
101 for(i = 1; fabs(r - r0) >= 0.0000001; ++i)
103 // Newton iteration on exp(r) = x:
104 // r <- r - (exp(r) - x) / (exp(r))
105 // r <- r - 1 + x / exp(r)
107 r = r0 - 1 + x / exp(r0);
109 print(ftos(i), "\n");
114 return log(x) * M_LOG10E;
122 return log(x) * M_LOG2E;
126 return floor(log2(x));
130 return '1 0 0' * (f - trunc(f)) + '0 1 0' * trunc(f);
133 float scalbn(float x, int n)
135 return x * pow(2, n);
140 return pow(x, 1.0/3.0);
142 float hypot(float x, float y)
144 return sqrt(x*x + y*y);
146 //float pow(float x, float y);
147 //float sqrt(float x, float y);
151 // approximation taken from wikipedia
154 return copysign(sqrt(1 - exp(-y * (1.273239544735163 + 0.14001228868667 * y) / (1 + 0.14001228868667 * y))), x);
160 vector lgamma(float x)
162 // TODO improve accuracy
163 if(x < 1 && x == floor(x))
164 return nan("gamma") * '1 1 1';
169 // reflection formula:
170 // gamma(1-z) * gamma(z) = pi / sin(pi*z)
171 // lgamma(1-z) + lgamma(z) = log(pi) - log(sin(pi*z))
172 // sign of gamma(1-z) = sign of gamma(z) * sign of sin(pi*z)
174 v_x = log(M_PI) - log(fabs(v_z)) - v_x;
181 return lgamma(x + 1) - log(x) * '1 0 0';
183 return (0.5 * log(2 * M_PI * x) + x * (log(x) - 1)) * '1 0 0' + '0 1 0';
185 float tgamma(float x)
189 return exp(v_x) * v_y;
192 //float ceil(float x);
193 //float floor(float x);
194 float nearbyint(float x)
198 //float rint(float x);
199 //float round(float x);
202 return (x>=0) ? floor(x) : ceil(x);
205 float fmod(float x, float y)
207 return x - y * trunc(x / y);
209 float remainder(float x, float y)
211 return x - y * rint(x / y);
213 vector remquo(float x, float y)
222 float copysign(float x, float y)
224 return fabs(x) * ((y>0) ? 1 : -1);
226 float nan(string tag)
230 float nextafter(float x, float y)
234 return nan("nextafter");
236 return -nextafter(-x, -y);
237 // now we know that x < y
238 // so we need the next number > x
240 d = max(fabs(x), 0.00000000000000000000001);
251 float nexttoward(float x, float y)
253 return nextafter(x, y);
256 float fdim(float x, float y)
260 float fmax(float x, float y)
264 float fmin(float x, float y)
268 float fma(float x, float y, float z)
273 int isgreater(float x, float y)
277 int isgreaterequal(float x, float y)
281 int isless(float x, float y)
285 int islessequal(float x, float y)
289 int islessgreater(float x, float y)
291 return x < y || x > y;
293 int isunordered(float x, float y)
295 return !(x < y || x == y || x > y);