changeset 7636:99c410f7f0b0

implement mapper function for banker's rounding
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 25 Mar 2008 15:56:41 -0400
parents ba7a3e20ee3d
children 2be056f03720
files ChangeLog configure.in liboctave/ChangeLog liboctave/lo-mappers.cc liboctave/lo-mappers.h src/ChangeLog src/mappers.cc src/ov-base.cc src/ov-base.h src/ov-bool-mat.h src/ov-bool-sparse.h src/ov-bool.h src/ov-complex.cc src/ov-complex.h src/ov-cx-mat.cc src/ov-cx-mat.h src/ov-cx-sparse.cc src/ov-cx-sparse.h src/ov-intx.h src/ov-range.h src/ov-re-mat.cc src/ov-re-mat.h src/ov-re-sparse.cc src/ov-re-sparse.h src/ov-scalar.cc src/ov-scalar.h src/ov.h
diffstat 27 files changed, 94 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2008-03-25  John W. Eaton  <jwe@octave.org>
+
+	* configure.in: Check for trunc.
+
 2008-03-21  David Bateman  <dbateman@free.fr>
 
 	* configure.in (HAVE_AMD): Complete test for presence of amd.
--- a/configure.in
+++ b/configure.in
@@ -1321,7 +1321,7 @@
   raise readlink realpath rename resolvepath rindex rmdir round select \
   setgrent setlocale setpwent setvbuf sigaction siglongjmp sigpending \
   sigprocmask sigsuspend snprintf stat strcasecmp strdup strerror stricmp \
-  strncasecmp strnicmp strptime strsignal symlink tempnam tgamma umask \
+  strncasecmp strnicmp strptime strsignal symlink tempnam tgamma trunc umask \
   uname unlink usleep utime vfprintf vsprintf vsnprintf waitpid \
   _chmod _snprintf x_utime _utime32)
 
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,13 @@
+2008-03-25  John W. Eaton  <jwe@octave.org>
+
+	* lo-mappers.cc (xtrunc): New function.
+	* lo-mappers.h: Provide decl.
+
+2008-03-25  Jaroslav Hajek  <highegg@gmail.com>
+
+	* lo-mappers.cc (xroundb): New function.
+	* lo-mappers.h: Provide decl.
+
 2008-03-23  David Bateman  <dbateman@free.fr>
 
 	* mx-ops: Definite binary operators for mixed integer array +
--- a/liboctave/lo-mappers.cc
+++ b/liboctave/lo-mappers.cc
@@ -80,6 +80,27 @@
 }
 
 double
+xtrunc (double x)
+{
+#if defined (HAVE_TRUNC)
+  return trunc (x);
+#else
+  return x > 0 ? floor (x) : ceil (x);
+#endif
+}
+
+double 
+xroundb (double x)
+{
+  double t = xround (x);
+
+  if (fabs (x - t) == 0.5)
+    t = 2 * xtrunc (0.5 * t);
+
+  return t;
+}
+
+double
 signum (double x)
 {
   double tmp = 0.0;
@@ -267,6 +288,12 @@
 }
 
 Complex
+xroundb (const Complex& x)
+{
+  return Complex (xroundb (real (x)), xroundb (imag (x)));
+}
+
+Complex
 signum (const Complex& x)
 {
   double tmp = abs (x);
--- a/liboctave/lo-mappers.h
+++ b/liboctave/lo-mappers.h
@@ -32,7 +32,9 @@
 extern OCTAVE_API double imag (double x);
 extern OCTAVE_API double real (double x);
 extern OCTAVE_API double xround (double x);
+extern OCTAVE_API double xroundb (double x);
 extern OCTAVE_API double signum (double x);
+extern OCTAVE_API double xtrunc (double x);
 extern OCTAVE_API double xlog2 (double x); 
 extern OCTAVE_API double xexp2 (double x);
 
@@ -57,6 +59,7 @@
 extern OCTAVE_API Complex fix (const Complex& x);
 extern OCTAVE_API Complex floor (const Complex& x);
 extern OCTAVE_API Complex xround (const Complex& x);
+extern OCTAVE_API Complex xroundb (const Complex& x);
 extern OCTAVE_API Complex signum (const Complex& x);
 
 extern OCTAVE_API bool xisnan (const Complex& x);
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,13 @@
+2008-03-25  Jaroslav Hajek  <highegg@gmail.com>
+
+	* mappers.cc (Froundb): New function.
+	* ov-base.cc, ov-base.h, ov-bool-mat.h, ov-bool-sparse.h,
+	ov-bool.h, ov-complex.cc, ov-complex.h, ov-cx-mat.cc, ov-cx-mat.h,
+	ov-cx-sparse.cc, ov-cx-sparse.h, ov-intx.h, ov-range.h,
+	ov-re-mat.cc, ov-re-mat.h, ov-re-sparse.cc, ov-re-sparse.h,
+	ov-scalar.cc, ov-scalar.h, ov.h:
+ 	Provide roundb mapper function.
+
 2008-03-25  Jaroslav Hajek <highegg@gmail.com>
 
 	* load-save.cc (save_vars): Handle -struct modifier.
--- a/src/mappers.cc
+++ b/src/mappers.cc
@@ -804,6 +804,24 @@
   return retval;
 }
 
+DEFUN (roundb, args, ,
+    "-*- texinfo -*-\n\
+@deftypefn {Mapping Function} {} roundb (@var{x})\n\
+Return the integer nearest to @var{x}. If there are two nearest\n\
+integers, return the even one (banker's rounding). If @var{x} is complex,\n\
+return @code{roundb (real (@var{x})) + roundb (imag (@var{x})) * I}.\n\
+@seealso{rem}\n\
+@end deftypefn")
+{
+  octave_value retval;
+  if (args.length () == 1)
+    retval = args(0).roundb ();
+  else
+    print_usage ();
+
+  return retval;
+}
+
 DEFUN (sign, args, ,
     "-*- texinfo -*-\n\
 @deftypefn {Mapping Function} {} sign (@var{x})\n\
--- a/src/ov-base.cc
+++ b/src/ov-base.cc
@@ -940,6 +940,7 @@
 UNDEFINED_MAPPER (log10)
 UNDEFINED_MAPPER (real)
 UNDEFINED_MAPPER (round)
+UNDEFINED_MAPPER (roundb)
 UNDEFINED_MAPPER (signum)
 UNDEFINED_MAPPER (sin)
 UNDEFINED_MAPPER (sinh)
--- a/src/ov-base.h
+++ b/src/ov-base.h
@@ -501,6 +501,7 @@
   virtual octave_value log10 (void) const;
   virtual octave_value real (void) const;
   virtual octave_value round (void) const;
+  virtual octave_value roundb (void) const;
   virtual octave_value signum (void) const;
   virtual octave_value sin (void) const;
   virtual octave_value sinh (void) const;
--- a/src/ov-bool-mat.h
+++ b/src/ov-bool-mat.h
@@ -221,6 +221,7 @@
   BOOL_MAT_MAPPER (log10)
   BOOL_MAT_MAPPER (real)
   BOOL_MAT_MAPPER (round)
+  BOOL_MAT_MAPPER (roundb)
   BOOL_MAT_MAPPER (signum)
   BOOL_MAT_MAPPER (sin)
   BOOL_MAT_MAPPER (sinh)
--- a/src/ov-bool-sparse.h
+++ b/src/ov-bool-sparse.h
@@ -177,6 +177,7 @@
   BOOL_SPARSE_MAPPER (log10)
   BOOL_SPARSE_MAPPER (real)
   BOOL_SPARSE_MAPPER (round)
+  BOOL_SPARSE_MAPPER (roundb)
   BOOL_SPARSE_MAPPER (signum)
   BOOL_SPARSE_MAPPER (sin)
   BOOL_SPARSE_MAPPER (sinh)
--- a/src/ov-bool.h
+++ b/src/ov-bool.h
@@ -229,6 +229,7 @@
   BOOL_MAPPER (log10)
   BOOL_MAPPER (real)
   BOOL_MAPPER (round)
+  BOOL_MAPPER (roundb)
   BOOL_MAPPER (signum)
   BOOL_MAPPER (sin)
   BOOL_MAPPER (sinh)
--- a/src/ov-complex.cc
+++ b/src/ov-complex.cc
@@ -379,6 +379,7 @@
 COMPLEX_MAPPER (log10, std::log10)
 COMPLEX_MAPPER (real, xreal)
 COMPLEX_MAPPER (round, xround)
+COMPLEX_MAPPER (roundb, xroundb)
 COMPLEX_MAPPER (signum, ::signum)
 COMPLEX_MAPPER (sin, std::sin)
 COMPLEX_MAPPER (sinh, std::sinh)
--- a/src/ov-complex.h
+++ b/src/ov-complex.h
@@ -171,6 +171,7 @@
   octave_value log10 (void) const;
   octave_value real (void) const;
   octave_value round (void) const;
+  octave_value roundb (void) const;
   octave_value signum (void) const;
   octave_value sin (void) const;
   octave_value sinh (void) const;
--- a/src/ov-cx-mat.cc
+++ b/src/ov-cx-mat.cc
@@ -699,6 +699,7 @@
 ARRAY_MAPPER (log10, ComplexNDArray::cmapper, std::log10)
 ARRAY_MAPPER (real, ComplexNDArray::dmapper, xreal)
 ARRAY_MAPPER (round, ComplexNDArray::cmapper, xround)
+ARRAY_MAPPER (roundb, ComplexNDArray::cmapper, xroundb)
 ARRAY_MAPPER (signum, ComplexNDArray::cmapper, ::signum)
 ARRAY_MAPPER (sin, ComplexNDArray::cmapper, std::sin)
 ARRAY_MAPPER (sinh, ComplexNDArray::cmapper, std::sinh)
--- a/src/ov-cx-mat.h
+++ b/src/ov-cx-mat.h
@@ -174,6 +174,7 @@
   octave_value log10 (void) const;
   octave_value real (void) const;
   octave_value round (void) const;
+  octave_value roundb (void) const;
   octave_value signum (void) const;
   octave_value sin (void) const;
   octave_value sinh (void) const;
--- a/src/ov-cx-sparse.cc
+++ b/src/ov-cx-sparse.cc
@@ -868,6 +868,7 @@
 SPARSE_MAPPER (log10, SparseComplexMatrix::cmapper, std::log10)
 SPARSE_MAPPER (real, SparseComplexMatrix::dmapper, xreal)
 SPARSE_MAPPER (round, SparseComplexMatrix::cmapper, xround)
+SPARSE_MAPPER (roundb, SparseComplexMatrix::cmapper, xroundb)
 SPARSE_MAPPER (signum, SparseComplexMatrix::cmapper, ::signum)
 SPARSE_MAPPER (sin, SparseComplexMatrix::cmapper, std::sin)
 SPARSE_MAPPER (sinh, SparseComplexMatrix::cmapper, std::sinh)
--- a/src/ov-cx-sparse.h
+++ b/src/ov-cx-sparse.h
@@ -173,6 +173,7 @@
   octave_value log10 (void) const;
   octave_value real (void) const;
   octave_value round (void) const;
+  octave_value roundb (void) const;
   octave_value signum (void) const;
   octave_value sin (void) const;
   octave_value sinh (void) const;
--- a/src/ov-intx.h
+++ b/src/ov-intx.h
@@ -252,6 +252,7 @@
   NO_OP_MAPPER (floor)
   NO_OP_MAPPER (real)
   NO_OP_MAPPER (round)
+  NO_OP_MAPPER (roundb)
 
 #undef NO_OP_MAPPER
 
@@ -506,6 +507,7 @@
   NO_OP_MAPPER (floor)
   NO_OP_MAPPER (real)
   NO_OP_MAPPER (round)
+  NO_OP_MAPPER (roundb)
 
 #undef NO_OP_MAPPER
 
--- a/src/ov-range.h
+++ b/src/ov-range.h
@@ -304,6 +304,7 @@
   RANGE_MAPPER (log10)
   RANGE_MAPPER (real)
   RANGE_MAPPER (round)
+  RANGE_MAPPER (roundb)
   RANGE_MAPPER (signum)
   RANGE_MAPPER (sin)
   RANGE_MAPPER (sinh)
--- a/src/ov-re-mat.cc
+++ b/src/ov-re-mat.cc
@@ -737,6 +737,7 @@
 CD_ARRAY_MAPPER (log10, ::log10, std::log10, 0.0, octave_Inf)
 ARRAY_MAPPER (real, NDArray::dmapper, ::real)
 ARRAY_MAPPER (round, NDArray::dmapper, xround)
+ARRAY_MAPPER (roundb, NDArray::dmapper, xroundb)
 ARRAY_MAPPER (signum, NDArray::dmapper, ::signum)
 ARRAY_MAPPER (sin, NDArray::dmapper, ::sin)
 ARRAY_MAPPER (sinh, NDArray::dmapper, ::sinh)
--- a/src/ov-re-mat.h
+++ b/src/ov-re-mat.h
@@ -205,6 +205,7 @@
   octave_value log10 (void) const;
   octave_value real (void) const;
   octave_value round (void) const;
+  octave_value roundb (void) const;
   octave_value signum (void) const;
   octave_value sin (void) const;
   octave_value sinh (void) const;
--- a/src/ov-re-sparse.cc
+++ b/src/ov-re-sparse.cc
@@ -927,6 +927,7 @@
 CD_SPARSE_MAPPER (log10, ::log10, std::log10, 0.0, octave_Inf)
 SPARSE_MAPPER (real, SparseMatrix::dmapper, ::real)
 SPARSE_MAPPER (round, SparseMatrix::dmapper, xround)
+SPARSE_MAPPER (roundb, SparseMatrix::dmapper, xroundb)
 SPARSE_MAPPER (signum, SparseMatrix::dmapper, ::signum)
 SPARSE_MAPPER (sin, SparseMatrix::dmapper, ::sin)
 SPARSE_MAPPER (sinh, SparseMatrix::dmapper, ::sinh)
--- a/src/ov-re-sparse.h
+++ b/src/ov-re-sparse.h
@@ -178,6 +178,7 @@
   octave_value log10 (void) const;
   octave_value real (void) const;
   octave_value round (void) const;
+  octave_value roundb (void) const;
   octave_value signum (void) const;
   octave_value sin (void) const;
   octave_value sinh (void) const;
--- a/src/ov-scalar.cc
+++ b/src/ov-scalar.cc
@@ -332,6 +332,7 @@
 CD_SCALAR_MAPPER (log10, ::log10, std::log10, 0.0, octave_Inf)
 SCALAR_MAPPER (real, ::real)
 SCALAR_MAPPER (round, xround)
+SCALAR_MAPPER (roundb, xroundb)
 SCALAR_MAPPER (signum, ::signum)
 SCALAR_MAPPER (sin, ::sin)
 SCALAR_MAPPER (sinh, ::sinh)
--- a/src/ov-scalar.h
+++ b/src/ov-scalar.h
@@ -246,6 +246,7 @@
   octave_value log10 (void) const;
   octave_value real (void) const;
   octave_value round (void) const;
+  octave_value roundb (void) const;
   octave_value signum (void) const;
   octave_value sin (void) const;
   octave_value sinh (void) const;
--- a/src/ov.h
+++ b/src/ov.h
@@ -915,6 +915,7 @@
   MAPPER_FORWARD (log10)
   MAPPER_FORWARD (real)
   MAPPER_FORWARD (round)
+  MAPPER_FORWARD (roundb)
   MAPPER_FORWARD (signum)
   MAPPER_FORWARD (sin)
   MAPPER_FORWARD (sinh)