bugGNU Octave - Bugs: bug #51515, sprintf not rounding as Matlab does

 
 

bug #51515: sprintf not rounding as Matlab does

Submitter:  Luis Mendo <lmendo>
Submitted:  Thu 20 Jul 2017 08:52:34 AM UTC
   
 
Category:  Octave Function Severity:  2 - Minor
Priority:  3 - Low Item Group:  Other
Status:  Wont Fix Assigned to:  None
Originator Name:  lmendo Open/Closed:  * Closed
Release:  * 4.0.3 Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Fri 21 Jul 2017 03:26:04 PM UTC, comment #7: 

@Pantxo: Thanks for the test on Linux.  Clearly this is a platform-dependent issue.  The only way to guarantee uniform results is to code defensively and round in the particular manner the programmer desires before calling sprintf.  This defensiveness avoids relying on the particular rounding mode chosen by the C library available.

Rik <rik5>
Group administrator
Fri 21 Jul 2017 08:09:37 AM UTC, comment #6: 

On linux, ML 2016a, the resuts are different:


>> sprintf ('%1.1g|', [2.5, -2.5, 3.5, -3.5])

ans =

2|-2|4|-4|


Pantxo Diribarne <pantxo>
Group Member
Thu 20 Jul 2017 11:23:50 PM UTC, comment #5: 

Thanks for testing.  Hopefully someone else can try a different platform just to be sure.  For example, this article from Stack Overflow (https://stackoverflow.com/questions/31142600/c-rounding-behavior-consistency-for-ties-with-sprintf) shows that Windows Visual Studio and GCC perform different rounding in exactly the way you are experiencing.

The simplest thing, for the moment, would be to call round() yourself on any data you want formatted in a particular fashion.

The rounding mode could be set as a whole for the C math library by using fsetround.  But that might be overkill for this.

Rik <rik5>
Group administrator
Thu 20 Jul 2017 09:01:34 PM UTC, comment #4: 

I tested with Matlab R2015b and R2016b, both on Windows. But I'm fairly certain it's that way in other, older versions too.

I'm on R2015b now. The result of


sprintf ('%1.1g|', [2.5, -2.5, 3.5, -3.5])


is


3|-3|4|-4|



Luis Mendo <lmendo>
Thu 20 Jul 2017 03:46:26 PM UTC, comment #3: 

What version of Matlab and what platform (Linux, Mac, Windows) did you test with?  As Andreas indicated, it may vary simply because of the platform and the underlying C libraries.

Also, could you test the following:


sprintf ('%1.1g|', [2.5, -2.5, 3.5, -3.5])


Finally, as always, if you want a particular result it is best to code for that rather than rely on implicit and undocumented defaults which could change from Matlab version to version.  In this case, there are functions floor, ceil, round, fix, and roundb so the programmer can get exactly the desired rounding behavior.  I would recode to


sprintf ('%1.1g', round (x))


if the desired behavior is regular rounding.

Rik <rik5>
Group administrator
Thu 20 Jul 2017 09:37:03 AM UTC, comment #2: 

Thanks. I failed to notice that it's just banker's rounding. Still, it's inconsistent with Matlab and with 'round'; but I'm not sure that qualifies as a &quot;bug&quot;

As for Matlab's 'sprintf' or 'fprintf', I have no idea, as they are built-in functions. They round .5 away from zero, probably for consistence with 'round',

Luis Mendo <lmendo>
Thu 20 Jul 2017 09:23:25 AM UTC, comment #1: 

Octave outputs the same here as a C snippet "printf ("%1.1g\n", 2.5);" compiled with gcc would do.

See https://www.gnu.org/software/libc/manual/html_node/Rounding.html



Round to nearest.

    This is the default mode. It should be used unless there is a specific need for one of the others. In this mode results are rounded to the nearest representable value. If the result is midway between two representable values, the even representable is chosen. Even here means the lowest-order bit is zero. This rounding mode prevents statistical bias and guarantees numeric stability: round-off errors in a lengthy calculation will remain smaller than half of FLT_EPSILON.



Do you have a description how Matlab does rounding in fprintf/sprintf? Or is it just because they've linked another C library?

Andreas Weber <andy1978>
Group Member
Thu 20 Jul 2017 08:52:34 AM UTC, original submission:  

For example,


sprintf('%1.1g', 2.5)


gives 2 instead of 3. It gives 3 in Matlab.

Luis Mendo <lmendo>

 

(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 pantxo (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by andy1978 (Posted a comment)
  • -email is unavailable- added by lmendo (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
    2017-07-21 rik5 Item GroupMatlab Compatibility Other
        StatusNone Wont Fix
        Open/ClosedOpen Closed
    2017-07-20 rik5 Severity3 - Normal 2 - Minor
        Priority5 - Normal 3 - Low
    2017-07-20 rik5 Item GroupNone Matlab Compatibility
        Summarysprintf not rounding correctly sprintf not rounding as Matlab does

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code