datenum seconds within minutes

9 ビュー (過去 30 日間)
egrandje
egrandje 2018 年 3 月 22 日
コメント済み: Peter Perkins 2018 年 3 月 23 日
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.
  1 件のコメント
Stephen23
Stephen23 2018 年 3 月 22 日
egrandje's "Answer" moved here:
Thanks a lot ! 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 Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by