bugGNU Octave - Bugs: bug #37903, difference between int32(-44) and...

 
 

bug #37903: difference between int32(-44) and -44

Submitter:  Michael Godfrey <godfrey>
Submitted:  Tue 11 Dec 2012 08:58:08 PM UTC
   
 
Category:  Documentation Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Documentation
Status:  Wont Fix Assigned to:  None
Originator Name:  godfrey Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 27 Mar 2014 02:54:12 AM UTC, comment #4: 

No progress on this bug in over a year, the excerpts from the manual that I mentioned seem pretty clear on the topic to me, so I'm not sure what needs to be added to the manual. Closing, feel free to reopen or report as a new bug if this still needs work.

Mike Miller <mtmiller>
Group Member
Tue 06 Aug 2013 03:04:27 AM UTC, comment #3: 

I think it's guaranteed that we do need to retain Matlab compatibility for the way values are rounded when casting a floating point value to an integer type. I'm relabeling this to reflect the last comment that the documentation should be corrected.

Can you suggest what should be added or propose a patch to the manual?

For reference, here is what the manual has to say on the issue, I think it's pretty clear (from http://www.gnu.org/software/octave/doc/interpreter/Integer-Data-Types.html)


In general most integer matrices are created by casting existing matrices to integers. The following example shows how to cast a matrix into 32 bit integers.

     float = rand (2, 2)
          ⇒ float = 0.37569   0.92982
                    0.11962   0.50876
     integer = int32 (float)
          ⇒ integer = 0  1
                      0  1

As can be seen, floating point values are rounded to the nearest integer when converted.


And on the issue of integer division (from http://www.gnu.org/software/octave/doc/interpreter/Integer-Arithmetic.html)


When doing integer division Octave will round the result to the nearest integer. This is different from most programming languages, where the result is often floored to the nearest integer. So, the result of int32(5) ./ int32(8) is 1.


Mike Miller <mtmiller>
Group Member
Tue 11 Dec 2012 10:10:28 PM UTC, comment #2: 

John said:
"I don't see this as a problem with floor as much as the confusing way that
integers work in Matlab and that we have to conform to for compatibility."

I agree that Matlab is weird and the problem can be
considered to be with integers.  But, it definitely leads
to confusion.

Is it really necessary to be Matlab compatible on this issue?

If so, the documentation needs to be corrected to reflect
the behavior.  I find it hard to explain in terms ofther
than "Matlab does it that way."

Michael Godfrey <godfrey>
Group Member
Tue 11 Dec 2012 09:58:29 PM UTC, comment #1: 

I don't see this as a problem with floor as much as the confusing way that integers work in Matlab and that we have to conform to for compatibility.

It looks to me as though int32(-46)/10 is -5 because the result of the division is rounded to nearest instead of just truncating the fractional part.  Note that it is true whether you use in32(-46)/10, which is division by a double, or int32(-46)/int32(10), which is division of two int32 values.

John W. Eaton <jwe>
Group administrator
Tue 11 Dec 2012 08:58:08 PM UTC, original submission:  

The Manual says floor(x) returns the largest integer not greater than x.  For x type double this is what it does.  But for x int32
floor(int32(-44)/10) is -4.  floor(-44/10)  is -5.  And, even worse, floor(int32(-46)/10) is -5.  This is not
consistent with the Manual or help, but it is what Matlab does!!

So, is this a documentation problem, or could
it behave in a more reasonable way despite Matlab?


Michael Godfrey <godfrey>
Group Member

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by godfrey (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2014-03-27 mtmiller StatusNeed Info Wont Fix
        Open/ClosedOpen Closed
    2013-08-06 mtmiller CategoryInterpreter Documentation
        Item GroupMatlab Compatibility Documentation
        StatusNone Need Info

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code