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

 
 

bug #51515: sprintf not rounding as Matlab does

Submitted by:  Luis Mendo <lmendo>
Submitted on:  Thu 20 Jul 2017 08:52:34 AM UTC  
 
Category: Octave FunctionSeverity: 2 - Minor
Priority: 3 - LowItem Group: Other
Status: Wont FixAssigned to: None
Originator Name: lmendoOpen/Closed: Closed
Release: 4.0.3Operating System: GNU/Linux

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

(Jump to the original submission 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>
Project Administrator
Fri 21 Jul 2017 08:09:37 AM UTC, comment #6:

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

Pantxo Diribarne <pantxo>
Project 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>
Project 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

is

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:

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

if the desired behavior is regular rounding.

Rik <rik5>
Project 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>
Project Member
Thu 20 Jul 2017 08:52:34 AM UTC, original submission:

For example,

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 File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by pantxo (Posted a comment)
  • -unavailable- added by rik5 (Posted a comment)
  • -unavailable- added by andy1978 (Posted a comment)
  • -unavailable- added by lmendo (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only project members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 7 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Fri 21 Jul 2017 03:26:04 PM UTCrik5Item GroupMatlab Compatibility=>Other
      StatusNone=>Wont Fix
      Open/ClosedOpen=>Closed
    Thu 20 Jul 2017 11:23:50 PM UTCrik5Severity3 - Normal=>2 - Minor
      Priority5 - Normal=>3 - Low
    Thu 20 Jul 2017 05:56:18 PM UTCrik5Item GroupNone=>Matlab Compatibility
      Summarysprintf not rounding correctly=>sprintf not rounding as Matlab does

    Back to the top


    Powered by Savane 3.1-cleanup1