changeset 20132:31e3799b9e27

Map -pi to pi for principal argument in complex operators with float values (bug #43313) * Array-fC.cc (nan_ascending_compare, nan_descending_compare): Use static_cast to float on M_PI so that comparisons work. * oct-cmplx.h (DEF_COMPLEXR_COMP): Use static_cast to type <T> on M_PI so that comparisons work. * complex.tst: Add tests for single values on top of those for double.
author Rik <rik@octave.org>
date Tue, 10 Mar 2015 09:42:19 -0700
parents a0ec61ec0f73
children 277b12eed117
files liboctave/array/Array-fC.cc liboctave/util/oct-cmplx.h test/complex.tst
diffstat 3 files changed, 25 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/Array-fC.cc
+++ b/liboctave/array/Array-fC.cc
@@ -58,9 +58,10 @@
     {
       float xarg = arg (x);
       float yarg = arg (y);
-      xarg = (xarg == -M_PI) ? M_PI : xarg;
-      yarg = (yarg == -M_PI) ? M_PI : yarg;
-
+      xarg = (xarg == -static_cast<float> (M_PI)) ? static_cast<float> (M_PI)
+                                                  : xarg;
+      yarg = (yarg == -static_cast<float> (M_PI)) ? static_cast<float> (M_PI)
+                                                  : yarg;
       return xarg < yarg;
     }
   else
@@ -82,9 +83,10 @@
     {
       float xarg = arg (x);
       float yarg = arg (y);
-      xarg = (xarg == -M_PI) ? M_PI : xarg;
-      yarg = (yarg == -M_PI) ? M_PI : yarg;
-
+      xarg = (xarg == -static_cast<float> (M_PI)) ? static_cast<float> (M_PI)
+                                                  : xarg;
+      yarg = (yarg == -static_cast<float> (M_PI)) ? static_cast<float> (M_PI)
+                                                  : yarg;
       return xarg > yarg;
     }
   else
--- a/liboctave/util/oct-cmplx.h
+++ b/liboctave/util/oct-cmplx.h
@@ -50,14 +50,14 @@
     { \
       FLOAT_TRUNCATE const T ay = std::arg (a); \
       FLOAT_TRUNCATE const T by = std::arg (b); \
-      if (ay == -M_PI) \
+      if (ay == static_cast<T> (-M_PI)) \
         { \
-          if (by != -M_PI) \
-            return M_PI OP by; \
+          if (by != static_cast<T> (-M_PI)) \
+            return static_cast<T> (M_PI) OP by; \
         } \
-      else if (by == -M_PI) \
+      else if (by == static_cast<T> (-M_PI)) \
         { \
-          return ay OP M_PI; \
+          return ay OP static_cast<T> (M_PI); \
         } \
       return ay OP by; \
     } \
@@ -72,8 +72,8 @@
   if (ax == bx) \
     { \
       FLOAT_TRUNCATE const T ay = std::arg (a); \
-      if (ay == -M_PI) \
-        return M_PI OP 0; \
+      if (ay == static_cast<T> (-M_PI)) \
+        return static_cast<T> (M_PI) OP 0; \
       return ay OP 0; \
     } \
   else \
@@ -87,8 +87,8 @@
   if (ax == bx) \
     { \
       FLOAT_TRUNCATE const T by = std::arg (b); \
-      if (by == -M_PI) \
-        return 0 OP M_PI; \
+      if (by == static_cast<T> (-M_PI)) \
+        return 0 OP static_cast<T> (M_PI); \
       return 0 OP by; \
     } \
   else \
--- a/test/complex.tst
+++ b/test/complex.tst
@@ -20,15 +20,19 @@
 %!test
 %! x = [0 i 1+i 2 3i 3+4i];
 %! assert (sort (x, "descend"), fliplr (x));
+%! assert (sort (single (x), "descend"), fliplr (single (x)));
 
 %!test
 %! x = [1, -1, i, -i];
 %! xs = [-i, 1, i, -1];
 %! assert (sort (x), xs);
 %! assert (sort (x, "descend"), fliplr (xs));
+%! assert (sort (single (x)), single (xs));
+%! assert (sort (single (x), "descend"), fliplr (single (xs)));
 
 ## bug #44071, issorted incorrect because it uses different sort routine.
 %!assert (issorted ([1, -1, i, -i]), false)
+%!assert (issorted (single ([1, -1, i, -i])), false)
 
 ## bug #43313, -1 is both '>' and '==' to (-1 - 0i)
 %!test
@@ -40,5 +44,9 @@
 %!test
 %! x = [complex(-1,0), complex(-1,-0), i, -i, 1];
 %! xs = sort (x);
+%! xf = single (x);
+%! xfs = sort (xf);
 %! assert (issorted (xs));
+%! assert (issorted (xfs));
+%! assert (double (xfs), xs);