datenum seconds within minutes

10 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2018-3-22
编辑:Stephen23 2018-3-22
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:

更多回答(1 个)

Walter Roberson
Walter Roberson 2018-3-22
For datetime, mm is months, not minutes. You should be using HH:MM:SS,FFF
Anyhow, remember that datenum represents time as full days and fractions of a day, so you are seeing differences in the round-off of the last bit of a floating point number .
  1 个评论
Peter Perkins
Peter Perkins 2018-3-23
Walter's point about day fractions is worth heeding. Multiples of 1/86400000 are inherently not representable exact in floating point. datetime (or maybe duration for your example?) will give you better answers (although the way I've shown it, you do have to mess with the display format):
>> duration(0,0,0,998) - duration(0,0,0,997)
ans =
duration
00:00:00
>> ans.Format = 'hh:mm:ss.SSS'
ans =
duration
00:00:00.001

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by