octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

please dont fix this... Matlab vs Octave Integer Division


From: John W. Eaton
Subject: please dont fix this... Matlab vs Octave Integer Division
Date: Mon, 12 Mar 2007 22:29:30 -0400

On 12-Mar-2007, David Grohmann wrote:

| So Matlab does integer division incorrectly... I thought I should let 
| you know. I prefer your implementation with the correct answer.
| 2/3 should equal 0 but Matlab will tell you it's 1.

Matlab also rounds to the nearest values for things like int32 (0.9).
I think this that having Octave behave differently from Matlab for
something like this is not good, so I checked in the following patch.
Note that the /= operator was already rounding.

If you really want C-style semantics, then I think maybe it would be
best to write your own C-style integer types for Octave and/or maybe
convince the MathWorks to also add data types that do what you want.

| octave:164> b = feval(  'uint32' , 3)
| b = 3

Why not just write

  uint32 (3)

?

The only reason I can see for using "cast" is if you are doing
something like

  cast (y, class (x));

to convert the class of one variable to the class of another and the
class of X is not always the same, or known, until the code is
executed.

jwe


liboctave/ChangeLog:

2007-03-12  John W. Eaton  <address@hidden>

        * oct-inttypes.h (octave_int::octave_int (double)):
        New Specialization.  Round arg.
        (operator / (const octave_int<T1>&, const octave_int<T2>&)):
        Round result before converting type.


Index: liboctave/oct-inttypes.h
===================================================================
RCS file: /cvs/octave/liboctave/oct-inttypes.h,v
retrieving revision 1.27
diff -u -u -r1.27 oct-inttypes.h
--- liboctave/oct-inttypes.h    27 Oct 2006 01:45:56 -0000      1.27
+++ liboctave/oct-inttypes.h    13 Mar 2007 02:19:32 -0000
@@ -211,6 +211,8 @@
   template <class U>
   octave_int (U i) : ival (OCTAVE_INT_FIT_TO_RANGE (i, T)) { }
 
+  octave_int (double d) : ival (OCTAVE_INT_FIT_TO_RANGE (xround (d), T)) { }
+
   octave_int (bool b) : ival (b) { }
 
   template <class U>
@@ -424,7 +426,7 @@
 {
   double tx = static_cast<double> (x.value ());
   double ty = static_cast<double> (y.value ());
-  double r = (tx == 0 && ty == 0) ? 0 : tx / ty;
+  double r = (tx == 0 && ty == 0) ? 0 : xround (tx / ty);
   return OCTAVE_INT_FIT_TO_RANGE2 (r, T1, T2);
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]