comparison liboctave/lo-mappers.h @ 11211:2554b4a0806e

use templates for some lo-mappers functions
author John W. Eaton <jwe@octave.org>
date Tue, 09 Nov 2010 03:24:18 -0500
parents 94d9d412a2a0
children ce27d6f4e134
comparison
equal deleted inserted replaced
11210:b79924abf776 11211:2554b4a0806e
23 */ 23 */
24 24
25 #if !defined (octave_liboctave_mappers_h) 25 #if !defined (octave_liboctave_mappers_h)
26 #define octave_liboctave_mappers_h 1 26 #define octave_liboctave_mappers_h 1
27 27
28 #include <limits>
29
28 #include "oct-cmplx.h" 30 #include "oct-cmplx.h"
29 #include "lo-math.h" 31 #include "lo-math.h"
30 32
31 // Double Precision 33 // Double Precision
34 inline double xtrunc (double x) { return gnulib::trunc (x); }
35 inline double xcopysign (double x, double y) { return copysignf (x, y); }
36 inline double xfloor (double x) { return floor (x); }
37
32 extern OCTAVE_API double arg (double x); 38 extern OCTAVE_API double arg (double x);
33 extern OCTAVE_API double conj (double x); 39 extern OCTAVE_API double conj (double x);
34 extern OCTAVE_API double fix (double x); 40 extern OCTAVE_API double fix (double x);
35 extern OCTAVE_API double imag (double x); 41 extern OCTAVE_API double imag (double x);
36 extern OCTAVE_API double real (double x); 42 extern OCTAVE_API double real (double x);
37 extern OCTAVE_API double xround (double x); 43 extern OCTAVE_API double xround (double x);
38 extern OCTAVE_API double xroundb (double x); 44 extern OCTAVE_API double xroundb (double x);
39 extern OCTAVE_API double signum (double x); 45 extern OCTAVE_API double signum (double x);
40 extern OCTAVE_API double xtrunc (double x);
41 extern OCTAVE_API double xmod (double x, double y);
42 extern OCTAVE_API double xrem (double x, double y);
43 extern OCTAVE_API double xlog2 (double x); 46 extern OCTAVE_API double xlog2 (double x);
44 extern OCTAVE_API Complex xlog2 (const Complex& x); 47 extern OCTAVE_API Complex xlog2 (const Complex& x);
45 extern OCTAVE_API double xlog2 (double x, int& exp); 48 extern OCTAVE_API double xlog2 (double x, int& exp);
46 extern OCTAVE_API Complex xlog2 (const Complex& x, int& exp); 49 extern OCTAVE_API Complex xlog2 (const Complex& x, int& exp);
47 extern OCTAVE_API double xexp2 (double x); 50 extern OCTAVE_API double xexp2 (double x);
117 120
118 extern OCTAVE_API Complex xmin (const Complex& x, const Complex& y); 121 extern OCTAVE_API Complex xmin (const Complex& x, const Complex& y);
119 extern OCTAVE_API Complex xmax (const Complex& x, const Complex& y); 122 extern OCTAVE_API Complex xmax (const Complex& x, const Complex& y);
120 123
121 // Single Precision 124 // Single Precision
125 inline float xtrunc (float x) { return gnulib::truncf (x); }
126 inline float xcopysign (float x, float y) { return copysignf (x, y); }
127 inline float xfloor (float x) { return floorf (x); }
128
122 extern OCTAVE_API float arg (float x); 129 extern OCTAVE_API float arg (float x);
123 extern OCTAVE_API float conj (float x); 130 extern OCTAVE_API float conj (float x);
124 extern OCTAVE_API float fix (float x); 131 extern OCTAVE_API float fix (float x);
125 extern OCTAVE_API float imag (float x); 132 extern OCTAVE_API float imag (float x);
126 extern OCTAVE_API float real (float x); 133 extern OCTAVE_API float real (float x);
127 extern OCTAVE_API float xround (float x); 134 extern OCTAVE_API float xround (float x);
128 extern OCTAVE_API float xroundb (float x); 135 extern OCTAVE_API float xroundb (float x);
129 extern OCTAVE_API float signum (float x); 136 extern OCTAVE_API float signum (float x);
130 extern OCTAVE_API float xtrunc (float x);
131 extern OCTAVE_API float xmod (float x, float y);
132 extern OCTAVE_API float xrem (float x, float y);
133 extern OCTAVE_API float xlog2 (float x); 137 extern OCTAVE_API float xlog2 (float x);
134 extern OCTAVE_API FloatComplex xlog2 (const FloatComplex& x); 138 extern OCTAVE_API FloatComplex xlog2 (const FloatComplex& x);
135 extern OCTAVE_API float xlog2 (float x, int& exp); 139 extern OCTAVE_API float xlog2 (float x, int& exp);
136 extern OCTAVE_API FloatComplex xlog2 (const FloatComplex& x, int& exp); 140 extern OCTAVE_API FloatComplex xlog2 (const FloatComplex& x, int& exp);
137 extern OCTAVE_API float xexp2 (float x); 141 extern OCTAVE_API float xexp2 (float x);
222 226
223 // Test for negative sign. 227 // Test for negative sign.
224 extern OCTAVE_API bool xnegative_sign (double x); 228 extern OCTAVE_API bool xnegative_sign (double x);
225 extern OCTAVE_API bool xnegative_sign (float x); 229 extern OCTAVE_API bool xnegative_sign (float x);
226 230
227 231 extern OCTAVE_API octave_idx_type NINTbig (double x);
228 #endif 232 extern OCTAVE_API octave_idx_type NINTbig (float x);
233
234 extern OCTAVE_API int NINT (double x);
235 extern OCTAVE_API int NINT (float x);
236
237 template <typename T>
238 OCTAVE_API
239 T
240 X_NINT (T x)
241 {
242 return (xisinf (x) || xisnan (x)) ? x : xfloor (x + 0.5);
243 }
244
245 inline OCTAVE_API double D_NINT (double x) { return X_NINT (x); }
246 inline OCTAVE_API float F_NINT (float x) { return X_NINT (x); }
247
248 // Template functions can have either float or double arguments.
249
250 template <typename T>
251 OCTAVE_API
252 T
253 xmod (T x, T y)
254 {
255 T retval;
256
257 if (y == 0)
258 retval = x;
259 else
260 {
261 T q = x / y;
262
263 T n = floor (q);
264
265 if (X_NINT (y) != y)
266 {
267 if (X_NINT (q) == q)
268 n = q;
269 else
270 {
271 if (x >= -1 && x <= 1)
272 {
273 if (std::abs (q - X_NINT (q))
274 < std::numeric_limits<T>::epsilon ())
275 n = X_NINT (q);
276 }
277 else
278 {
279 if (std::abs ((q - X_NINT (q))/ X_NINT (q))
280 < std::numeric_limits<T>::epsilon ())
281 n = D_NINT (q);
282 }
283 }
284 }
285
286 retval = x - y * n;
287 }
288
289 if (x != y && y != 0)
290 retval = xcopysign (retval, y);
291
292 return retval;
293 }
294
295 template <typename T>
296 OCTAVE_API
297 T
298 xrem (T x, T y)
299 {
300 T retval;
301
302 if (y == 0)
303 retval = x;
304 else
305 {
306 T q = x / y;
307
308 T n = xtrunc (q);
309
310 if (X_NINT (y) != y)
311 {
312 if (X_NINT (q) == q)
313 n = q;
314 else
315 {
316 if (x >= -1 && x <= 1)
317 {
318 if (std::abs (q - X_NINT (q))
319 < std::numeric_limits<T>::epsilon ())
320 n = X_NINT (q);
321 }
322 else
323 {
324 if (std::abs ((q - X_NINT (q))/ X_NINT (q))
325 < std::numeric_limits<T>::epsilon ())
326 n = X_NINT (q);
327 }
328 }
329 }
330
331 retval = x - y * n;
332 }
333
334 if (x != y && y != 0)
335 retval = xcopysign (retval, x);
336
337 return retval;
338 }
339
340 #endif