changeset 18242:36057e2411f8 stable

test float rcond using float arithmetic in inv function (bug #41065) * inv.cc (Finv): Test float rcond using float arithmetic. Use correct rcond in warning message.
author John W. Eaton <jwe@octave.org>
date Tue, 07 Jan 2014 22:06:37 -0500
parents 450f50d3eb18
children c7fe55478e11 1f072ae35ede
files libinterp/corefcn/inv.cc
diffstat 1 files changed, 25 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/inv.cc	Tue Jan 07 10:57:09 2014 -0500
+++ b/libinterp/corefcn/inv.cc	Tue Jan 07 22:06:37 2014 -0500
@@ -213,11 +213,22 @@
 
       retval(0) = result;
 
-      volatile double xrcond = rcond;
-      xrcond += 1.0;
-      if (nargout < 2 && (info == -1 || xrcond == 1.0))
+      bool rcond_plus_one_eq_one = false;
+
+      if (isfloat)
+        {
+          volatile float xrcond = frcond;
+          rcond_plus_one_eq_one = xrcond + 1.0F == 1.0F;
+        }
+      else
+        {
+          volatile double xrcond = rcond;
+          rcond_plus_one_eq_one = xrcond + 1.0 == 1.0;
+        }
+
+      if (nargout < 2 && (info == -1 || rcond_plus_one_eq_one))
         warning ("inverse: matrix singular to machine precision, rcond = %g",
-                 rcond);
+                 (isfloat ? frcond : rcond));
     }
 
   return retval;
@@ -230,6 +241,16 @@
 %!error inv ()
 %!error inv ([1, 2; 3, 4], 2)
 %!error <argument must be a square matrix> inv ([1, 2; 3, 4; 5, 6])
+
+%!test
+%! [xinv, rcond] = inv (single ([1,2;3,4]));
+%! assert (isa (xinv, 'single'));
+%! assert (isa (rcond, 'single'));
+
+%!test
+%! [xinv, rcond] = inv ([1,2;3,4]);
+%! assert (isa (xinv, 'double'));
+%! assert (isa (rcond, 'double'));
 */
 
 // FIXME: this should really be done with an alias, but