MATLAB: Datenum seconds within minutes

datenumsubstraction

Hello Every One, I have a question regarding datenum behavior (sorry if it has already asked, I haven't found it). I do not understand what is happening in the following cases because I was naively expecting the same value every time ?
>> datenum('00:00:00,998','hh:mm:ss,FFF')-datenum('00:00:00,997','hh:mm:ss,FFF')
ans =
1.164153218269348e-08
>> datenum('00:00:00,999','hh:mm:ss,FFF')-datenum('00:00:00,998','hh:mm:ss,FFF')
ans =
1.152511686086655e-08
>> datenum('00:00:01,000','hh:mm:ss,FFF')-datenum('00:00:00,999','hh:mm:ss,FFF')
ans =
1.164153218269348e-08
>> datenum('00:01:00,000','hh:mm:ss,FFF')-datenum('00:00:59,999','hh:mm:ss,FFF')
ans =
30.999305567122065
Thanks a lot for your help. E.

Best Answer

You are using the wrong format characters: the datenum help clearly lists only capital letters for time values, whereas you are using lower case. When you use the correct format characters 'HH:MM:SS' then your final example matches the others:
>> datenum('00:01:00,000','HH:MM:SS,FFF')-datenum('00:00:59,999','HH:MM:SS,FFF')
ans = 1.15251168608665e-08
Also the values will never be "the same value every time" because a datenum value is stored as a double binary floating point number and it does not store exact representations of microseconds or even milliseconds. Your examples differ by around 1e-10 days (equivalent to about 10 usec), which is right at the precision limit of double floating point numbers. I would not expect these calculations to give the same output values.
Read these to know more about the limits of serial date numbers:
and read these to learn about floating point numbers in general: