bugGNU Octave - Bugs: bug #39735, printf with empty arguments is...

 
 

bug #39735: printf with empty arguments is inconsistent with Matlab

Submitter:  None
Submitted:  Fri 09 Aug 2013 09:16:14 AM UTC
   
 
Category:  Octave Function Severity:  1 - Wish
Priority:  3 - Low Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Michael Leitner 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
   

Wed 23 Nov 2016 04:10:27 PM UTC, comment #5: 

I checked in the following changeset:

http://hg.savannah.gnu.org/hgweb/octave/rev/86b6f79d4de1

It seems to fix the problem for me so I'm closing this report as fixed.

John W. Eaton <jwe>
Group administrator
Tue 22 Nov 2016 09:19:10 PM UTC, comment #4: 

This behavior is still present in Octave 4.2.0.

Hartmut <hardy>
Thu 04 Jun 2015 01:49:03 AM UTC, comment #3: 

Updating release to the development version, still present.

Mike Miller <mtmiller>
Group Member
Mon 26 Aug 2013 07:45:23 PM UTC, comment #2: 

Jordi,

I can confirm Matlab behaves as you indicated.


>> fprintf('a %d b\n',ones(0,1))
a  b
>> sprintf('a %d b\n',ones(0,1))

ans =

a  b


Ben Abbott <bpabbott>
Group Member
Fri 09 Aug 2013 01:24:52 PM UTC, comment #1: 

Okay, I'm told Matlab does this:


>> fprintf('a %d b\n',ones(0,1))
a  b


So we should do the same, no matter how stupid it is.

Jordi GutiƩrrez Hermoso <jordigh>
Group Member
Fri 09 Aug 2013 09:16:14 AM UTC, original submission:  

Consider:


~:10> printf("a %d b\n",ones(2,1))
a 1 b
a 1 b
~:11> printf("a %d b\n",ones(1,1))
a 1 b
~:12> printf("a %d b\n",ones(0,1))
a ~:13>


For me, this is inconsistent behaviour, I would expect the last command to give empty output. It seems that this is connect to the feature of being able to output matrices that "are not exact multiples of the number of conversions in the format template":


~:13> printf("a %d %d b\n",ones(3,1))
a 1 1 b
a 1 ~:14>


Without looking into the code, I cannot imagine the logic leading to these results, for me the most obvious implementation would be to integer-divide the number of supplied elements by the number of conversions in the output string, call C's printf accordingly and special-case the remaining elements (by cutting the output string and pass it once more to C's printf). This would give my expected behaviour. By the way, what does MATLAB do?

At the very least, the observed behaviour is a documentation bug. In 14.2.5 it says "If more than one value is to be printed in a single call, the output functions do not return to the beginning of the format template when moving on from one value to the next. This can lead to confusing output if the number of elements in the matrices are not exact multiples of the number of conversions in the format template." But here it is not more than one value that is to be printed, and also the number of elements is an exact (should say: integer) multiple of the number of output conversions.

Going a step further, I would find an added feature useful: add m and v as type modifier characters, which in the hitherto syntax are being ignored. The output should be:


~:14> printf("a %vd b\n",ones(2,1))
a 1 1 b
~:15> printf("a %vd b\n",ones(1,1))
a 1 b
~:16> printf("a %vd b\n",ones(0,1))
a  b
~:17> printf("a %md b\n",ones(2,2))
a 1 1
1 1 b
~:18> printf("a %md b\n",ones(2,1))
a 1
1 b


Initial and trailing spaces/newlines would be left to debate, but I would keep them at a minimum, as the user can supply them in the format string. This could be implemented rather easily by a pass over the output conversion string looking for v or d in a conversion and generating a new string, substituting a %*v* by the corresponding sequence of %**, given by length(corresponding argument)). Obviously, mixing a %d with a %vd in a single call with both vector arguments would give an awkward situation.

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 jwe (Posted a comment)
  • -email is unavailable- added by hardy (Posted a comment)
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by bpabbott (Posted a comment)
  • -email is unavailable- added by jordigh (Posted a comment)
  •  

    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 9 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2016-11-23 jwe StatusConfirmed Fixed
        Open/ClosedOpen Closed
    2015-06-04 mtmiller Severity3 - Normal 1 - Wish
        Priority5 - Normal 3 - Low
        Release3.6.2 dev
        Operating SystemGNU/Linux Any
    2013-08-09 jordigh Item GroupInaccurate Result Matlab Compatibility
        Summaryprintf behaviour is inconsistent for matrix arguments printf with empty arguments is inconsistent with Matlab
    2013-08-09 jordigh StatusNone Confirmed

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code