bugGNU Octave - Bugs: bug #49192, dec2bin function does not behave...

 
 

bug #49192: dec2bin function does not behave the same as Matlab

Submitter:  None
Submitted:  Mon 26 Sep 2016 07:43:26 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  WTF, Matlab?!?
Status:  Wont Fix Assigned to:  None
Originator Name:  Jim Haberly Originator Email:  -email is unavailable-
Open/Closed:  * Closed Release:  * dev
Operating System:  * Any Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 29 Sep 2016 05:57:32 AM UTC, comment #4: 

Matlab 2010b:


>>  dec2bin (5, 0)

ans =

101

>> dec2bin (0, 3)

ans =

000

>> dec2bin (0, 1)

ans =

0

>> dec2bin (0, 0)

ans =

   Empty string: 1-by-0


moreover

>> dec2base (0,2,0)

ans =

0


so dec2bin(x,y) = dec2base (x,2,y) unless x=0 & y=0

Anonymous
Tue 27 Sep 2016 05:44:04 PM UTC, comment #3: 

This seems like an odd choice by Matlab.  According to their own documentation:


str = dec2bin(d,n) produces a binary representation with at least n bits.


So using n == 0 should imply that the result in binary will have 0 or more digits.  Given that the decimal number 0 can be represented in binary by the decimal digit '0' it seems like the answer should have exactly one digit.

Just for fun, what does Matlab do for the following?


dec2bin (5, 0)
dec2bin (0, 3)


Octave's results are


octave:3> dec2bin (5, 0)
ans = 101
octave:4> dec2bin (0, 3)
ans = 000


which makes sense to me.


Rik <rik5>
Group administrator
Tue 27 Sep 2016 11:12:08 AM UTC, comment #2: 

Another hint to solve you issue in a Matlab compatible way, depending on your code, check for the second argument and make a branch. This should not be more expensive than calling a function without expecting any useful output, e.g.


a = '0101001';
d = ?;
len = ?;

if (len > 0)
  a = [a, dec2bin(d, len)];
endif


Kai Torben Ohlhus <siko1056>
Group Member
Tue 27 Sep 2016 11:04:01 AM UTC, comment #1: 

I would throw this bug report back to The Mathworks, e.g. in Matlab R2016a I get:


>> dec2bin (0, 0)

ans =

   Empty matrix: 1-by-0


>> dec2hex (0, 0)
Error using reshape
To RESHAPE the number of elements must not change.

Error in dec2hex (line 59)
h = reshape(h,n0,numD)';

>> dec2base (0, 2, 0)

ans =

0

>> dec2base (0, 16, 0)

ans =

0


Here Octave looks more consistend, as it uses only dec2base in any case:


>> dec2bin (0, 0)
ans = 0
>> dec2hex (0, 0)
ans = 0
>> dec2base (0, 2, 0)
ans = 0
>> dec2base (0, 16, 0)
ans = 0


Kai Torben Ohlhus <siko1056>
Group Member
Mon 26 Sep 2016 07:43:26 PM UTC, original submission:  


I just finished porting a complex model of an MPEG-2 video encoder originally written in Matlab. When executed on GNU Octave the model produced syntax errors in the output video stream. I traced this to the dec2bin() function.

Matlab: dec2bin(0,0) returns ''
Octave: dec2bin(0,0) returns '0'

When using dec2bin to produce character strings by concatenation, extra 0 bits were appearing in the result.


Anonymous

 

(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 (Updated the item)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by siko1056 (Posted a comment)
  • -email is unavailable- added by None (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 15 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-09-29 rik5 Open/ClosedOpen Closed
    2016-09-27 mtmiller Item GroupMatlab Compatibility WTF, Matlab?!?
        Release4.0.2 dev
        Operating SystemMicrosoft Windows Any
    2016-09-27 rik5 StatusNone Wont Fix
    2016-09-27 rik5 Priority3 - Low 5 - Normal
        Item GroupWTF, Matlab?!? Matlab Compatibility
        StatusWont Fix None
        Release4.2.0-rc2 4.0.2
        Operating SystemAny Microsoft Windows
    2016-09-27 siko1056 Priority5 - Normal 3 - Low
        Item GroupMatlab Compatibility WTF, Matlab?!?
        StatusNone Wont Fix
        Release4.0.2 4.2.0-rc2
        Operating SystemMicrosoft Windows Any

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code