bugGNU Octave - Bugs: bug #45174, num2str produces different results...

 
 

bug #45174: num2str produces different results from Matlab for some corner cases

Submitter:  Rik <rik5>
Submitted:  Sun 24 May 2015 04:58:19 AM UTC
   
 
Category:  Octave Function Severity:  2 - Minor
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Wed 17 Jun 2015 07:55:53 PM UTC, comment #6: 

I rebased your cset to the dev branch and applied it here (http://hg.savannah.gnu.org/hgweb/octave/rev/f357e076776f)

With regards to comment #2 and and when the '%e' format should be applied, the documentation says:


Notable Behavior of Conversions with Formatting Operators

    Numeric conversions print only the real component of complex numbers.

    If you specify a conversion that does not fit the data, such as a string conversion for a numeric value, MATLABĀ® overrides the specified conversion, and uses %e.

    Example: '%s' converts pi to 3.141593e+00.

    If you apply a string conversion (%s) to integer values, MATLAB converts values that correspond to valid character codes to characters.

    Example: '%s' converts [65 66 67] to ABC.


Thus, it appears to apply only to the %s and %c conversions which your patch covers.


Rik <rik5>
Group administrator
Wed 17 Jun 2015 02:59:03 PM UTC, comment #5: 

The attached patch solves these bugs and adds some tests.

The patch also solves bug #44684

I haven't touched the case for complex numbers/matrices. Can anyone check if the outputs are correct (respect to matlab).


(file #34245)

Juan Pablo Carbajal <juanpi>
Group Member
Wed 17 Jun 2015 01:22:21 PM UTC, comment #4: 

The result I get from Matlab (R2015a) is:


saveFile = num2str([1 2], 'sometext_%d_%d')

saveFile =

sometext_1_2


The use of sprintf is the better option. But the option with num2str was in a Matlab Code I tried to use in Octave.



Anonymous
Wed 17 Jun 2015 01:07:30 PM UTC, comment #3: 

The bug in comment #1 is caused at line ~119 of num2str where it says

fmt = [deblank(repmat (fmt, 1, columns (x))), "\n"];


This line assumes that the fmt input is only a format string and nothing else, and applies the same for all columns of x.

The use of num2str in comment #1 seems incorrect to me,  sprintf should be used instead. What does matlab do when the format string of num2str is "sometext_%d_%d"?


Juan Pablo Carbajal <juanpi>
Group Member
Wed 17 Jun 2015 12:41:58 PM UTC, comment #2: 

Can anybody test in Matlab what are the result of

num2str(e ,"%d")
num2str(e ,"%i")
num2str(e ,"%u")
num2str(e ,"%o")
num2str(e ,"%x")
num2str(e ,"%X")
num2str(e ,"%f")
num2str(e ,"%F")
num2str(e ,"%E")
num2str(e ,"%a")
num2str(e ,"%A")

I am trying to guess when %e should be set by default. Is it only when the format string is "%c" or "%s"?


Juan Pablo Carbajal <juanpi>
Group Member
Wed 17 Jun 2015 10:47:36 AM UTC, comment #1: 

A additional case where num2str fails:

With 2 or more numbers to convert a wrong answer is returned:

octave:39> saveFile = num2str([1 2], '/sometext%g_%g')
saveFile = /sometext1_2/sometex


Octave adds characters before the first number after the end of the string.

With 1 number the answer is correct:

octave:40> saveFile = num2str([1], '/sometext%g_')
saveFile = /sometext1_


and if the first number is close enough to the beginning:

octave:42> saveFile = num2str([1 2], '/%gsometext%g_')
saveFile = /1sometext2_


Anonymous
Sun 24 May 2015 04:58:19 AM UTC, original submission:  

Corner case 1 : %s on integer values

This is supposed to produce a string where the integers are interpreted as ASCII characters as long as they are in the appropriate range.  Octave almost succeeds, but truncates the string by one character.



octave:1> num2str ([65 66 67], '%c')
ans = ABC
octave:2> num2str ([65 66 67], '%s')
ans = AB


Corner case 2 : wrong conversion for type of numeric input, should default to '%e'

If you use an incorrect format, Octave is supposed to recognize that the data is not correct and switch to the '%e' format.  For example,


octave:3> num2str (e, '%s')
ans =

The answer should be

octave:4> num2str (e, '%e')
ans = 2.718282e+00



Rik <rik5>
Group administrator

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #34245:  num2str.diff added by juanpi (2KiB - text/x-patch)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by juanpi (Posted a comment)
  • -email is unavailable- added by mtmiller (Updated the item)
  • -email is unavailable- added by rik5 (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 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2015-06-17 rik5 StatusConfirmed Fixed
        Open/ClosedOpen Closed
    2015-06-17 juanpi Attached File- Added num2str.diff, #34245
    2015-05-25 rik5 Summarynum2str produces differently results from Matlab for some corner cases num2str produces different results from Matlab for some corner cases
    2015-05-24 mtmiller Summarynumstr produces differently results from Matlab for some corner cases num2str produces differently results from Matlab for some corner cases
    2015-05-24 rik5 Priority5 - Normal 3 - Low
        Summarynumstr produces differently sized results from Matlab for some corner cases numstr produces differently results from Matlab for some corner cases

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code