bugGNU Octave - Bugs: bug #63829, Padding string with 0 in fprintf...

 
 

bug #63829: Padding string with 0 in fprintf results in padding with spaces

Submitter:  Thibaud Pigeon <thibaudp>
Submitted:  Wed 22 Feb 2023 01:06:55 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  WTF, Matlab?!?
Status:  Wont Fix 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

Thu 23 Feb 2023 09:25:10 AM UTC, comment #6: 


>> fprintf("%-08d\n", 10101)
10101
>> fprintf("%-08s\n", '10101')
10101000
>>


With three spaces at the end of first output.

Thibaud Pigeon <thibaudp>
Thu 23 Feb 2023 01:50:40 AM UTC, comment #5: 

I'm curious, what does Matlab do for


fprintf ("-08s\n", "10101")
fprintf ("-08d\n", 10101)


?

John W. Eaton <jwe>
Group administrator
Thu 23 Feb 2023 12:37:49 AM UTC, comment #4: 

Thank you for checking on other systems.

Given the undefined behavior and the workaround for the original problem, I'm closing this as Won't Fix, but it can be reopened if anyone feels we need Matlab compatibility for this.

Arun Giridhar <arungiridhar>
Group Member
Wed 22 Feb 2023 06:37:16 PM UTC, comment #3: 

Arun's workaround is imo more appropriate than original script.
Behaviour is indeed undefined :
 - Redhat bash behaves like Octave
 - Windows mingw64 bash (from git for windows) & c (mingw, gcc...)  behave like Matlab
So definitely not a good practice to expect it to work one way or another.

Thibaud Pigeon <thibaudp>
Wed 22 Feb 2023 05:51:21 PM UTC, comment #2: 

I'm changing the Item Group to "WTF, Matlab?!?" because Octave's behavior matches the C-language *printf functions.  If I understand correctly, the leading 0 feature of *printf only applies to numeric conversions.  For other conversions, the behavior is undefined.

John W. Eaton <jwe>
Group administrator
Wed 22 Feb 2023 05:26:10 PM UTC, comment #1: 

Same thing on dev. Setting to confirmed and Matlab compatibility for fprintf.

Easy workaround for now: dec2bin (42, 8) will left-pad with zeros and doesn't need fprintf.

Arun Giridhar <arungiridhar>
Group Member
Wed 22 Feb 2023 01:06:55 PM UTC, original submission:  


fprintf('%08s', dec2bin(42));


expected output: '00101010'
actual output:   '  101010'

Matlab R2020b and codeblocks-20.03mingw behave as expected.

Thibaud Pigeon <thibaudp>

 

(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 arungiridhar (Posted a comment)
  • -email is unavailable- added by thibaudp (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
    2023-02-23 arungiridhar StatusConfirmed Wont Fix
        Open/ClosedOpen Closed
    2023-02-22 jwe Item GroupMatlab Compatibility WTF, Matlab?!?
    2023-02-22 arungiridhar Operating SystemMicrosoft Windows Any
    2023-02-22 arungiridhar Item GroupIncorrect Result Matlab Compatibility
        StatusNone Confirmed
        Release7.3.0 dev

    Back to the top

    Powered by Savane 3.13-caa5.
    Corresponding source code