bugGNU Octave - Bugs: bug #50673, datestr truncates fractional...

 
 

bug #50673: datestr truncates fractional minutes rather than rounding

Submitter:  Luis Mendo <lmendo>
Submitted:  Wed 29 Mar 2017 08:26:18 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Fixed Assigned to:  None
Originator Name:  Luis Mendo 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

Sun 02 Apr 2017 10:35:33 PM UTC, comment #10: 

Sorry, please ignore previous comment. All correct. Thanks again!

Luis Mendo <lmendo>
Sun 02 Apr 2017 10:34:06 PM UTC, comment #9: 

Thank you both!

I've noticed that with the patched file

http://hg.savannah.gnu.org/hgweb/octave/file/68dbde0e670a/scripts/time/datestr.m

the result of 'datestr(datenum('1:00')-datenum('0:55'))' is now '12:05 AM'. I expected it would be '0:05 AM' (before the patch it was '0:04 AM', and Matlab gives '0:05 AM').

Luis Mendo <lmendo>
Sun 02 Apr 2017 04:12:53 AM UTC, comment #8: 

Thanks Lars.  I pushed it to the development branch here http://hg.savannah.gnu.org/hgweb/octave/rev/68dbde0e670a.  Marking as fixed and closing report.

Rik <rik5>
Group administrator
Thu 30 Mar 2017 10:48:20 PM UTC, comment #7: 

I've attached a changeset. Hope formatting is correct. Had to learn about Mercurial basics first.

(file #40215)

Lars Kindermann <larskindermann>
Thu 30 Mar 2017 04:48:12 AM UTC, comment #6: 

@Lars: I tested the patch and it works fine.  Do you want to make it into a complete cset with a commit message and some BIST tests?

Rik <rik5>
Group administrator
Thu 30 Mar 2017 01:25:20 AM UTC, comment #5: 

I have attached a patch that rounds fractional seconds >= 0.9995 s to the next full second before formatting in datestr(). That also avoids printing 1000 ms in a FFF field.


datestr(datenum('1:00')-datenum('0:55'))
ans = 12:05 AM


(file #40202)

Lars Kindermann <larskindermann>
Wed 29 Mar 2017 11:26:49 PM UTC, comment #4: 

typo...

I suggest datestr() and datevec() should stay compatible after any rounding...


>> datestr (etime,'HH:MM:SS.FFF')
ans = 00:04:59.1000

>> datevec (etime)
ans =

   -1.00000   12.00000   31.00000    0.00000    4.00000   59.99999


Lars Kindermann <larskindermann>
Wed 29 Mar 2017 09:33:35 PM UTC, comment #3: 

Perhaps datestr() should round the serial date to milliseconds before conversion. At the moment it looks pretty strange when including the milliseconds in the format:


etime = datenum ('1:00') - datenum ('0:55')
etime =  0.0034722

>> datestr(etime)
ans =  0:04 AM

>> datestr(etime,'HH:MM:SS.FFF')
ans = 00:04:59.1000


But one should also consider that datestr() and datenum() should not give contradictory results.

Lars Kindermann <larskindermann>
Wed 29 Mar 2017 09:12:25 PM UTC, comment #2: 

Not sure what Matlab's 'datestr' function does internally; its code is fairly complicated. But it seems to round. Compare Matlab:


>> datestr(0.00347222213167697)
ans =
12:05 AM
>> datestr(0.00347222213167697-1e-10)
ans =
12:05 AM
>> datestr(0.00347222213167697+1e-10)
ans =
12:05 AM


and Octave:


>> datestr(0.00347222213167697)
ans =  0:04 AM
>> datestr(0.00347222213167697-1e-10)
ans =  0:04 AM
>> datestr(0.00347222213167697+1e-10)
ans =  0:05 AM


Luis Mendo <lmendo>
Wed 29 Mar 2017 08:47:42 PM UTC, comment #1: 

Definitely looks like rounding.  datenum() calculates in days.  See below where I calculate the expected difference in minutes.


octave:18> format long
octave:19> etime = datenum ('1:00') - datenum ('0:55')
etime =  0.00347222213167697
octave:20> minutes = etime * 24 * 60
minutes =  4.99999986961484


So the question is whether datestr should use round or whether it should simply truncate the fractional part of minutes.  The current behavior is to truncate as shown below.


octave:23> datestr (4.9/60/24)
ans = 12:04 AM
octave:24> datestr (4.1/60/24)
ans = 12:04 AM


One way to resolve this would be to test the equivalent code in Matlab.  If they truncate results then Octave should too.  If they round results then Octave should too.


Rik <rik5>
Group administrator
Wed 29 Mar 2017 08:26:18 PM UTC, original submission:  

This code


datestr(datenum('1:00')-datenum('0:55'))


gives


ans =  0:04 AM


instead of the expected


ans =  0:05 AM


It seems to be a rounding issue with datestr, because


datestr(datenum('1:00')-datenum('0:55')+1e-10)


does give


ans =  0:05 AM


Luis Mendo <lmendo>

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #40215:  datestr.cset added by larskindermann (1KiB - application/octet-stream)
file #40202:  datestr.diff added by larskindermann (495B - text/x-patch)

 

Depends on the following items: None found

Items that depend on this one

Digest:
   bug dependencies.

 

Carbon-Copy List
  • -email is unavailable- added by larskindermann (Posted a comment)
  • -email is unavailable- added by rik5 (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 10 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-06-20 mtmiller Dependencies- bugs #51275 is dependent
    2017-04-02 rik5 StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2017-03-30 larskindermann Attached File- Added datestr.cset, #40215
    2017-03-30 rik5 StatusNeed Info Patch Submitted
    2017-03-30 larskindermann Attached File- Added datestr.diff, #40202
    2017-03-29 rik5 Item GroupNone Matlab Compatibility
        StatusNone Need Info
        Release4.0.3 dev
        Summary'datestr' gives wrong result. Possibly a rounding issue datestr truncates fractional minutes rather than rounding

    Back to the top

    Powered by Savane 3.13-02a9.
    Corresponding source code