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일

0 개 추천

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일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2018년 3월 22일

댓글:

2018년 3월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by