wrong etime output in seconds

In my code, if I calculate Dt1 = etime(t1, t2); then
Dt2 = abs(aa1 - aa2)*3.154e+7 +...
abs(mm1 - mm2)*2.628e+6 +...
abs(dd1 - dd2)*86400 +...
abs(hh1 - hh2)*3600 +...
abs(mi1 - mi2)*60 +...
abs(se1 - se2);
abs(Dt1 - Dt2) is not zero.
Note: both t1 and t2 result from datevec calls, along with the variables for year, month, day, hour, minute, and second used in the Dt2 calculation.
What is the problem using etime? I am assuming my calculation for Dt2 is the correct one.

댓글 수: 2

abs(aa1 - aa2)*3.154e+7 +...
abs(mm1 - mm2)*2.628e+6 ...
Four significant digits is nowhere near precise enough if you want to calculate something to one second accuracy.
GCP
GCP 2018년 1월 12일
I guess it is more about the fact I am not considering bissextile years nor the different number of days in varied months. The etime function results may reflect these variations.

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

답변 (1개)

Star Strider
Star Strider 2018년 1월 12일
편집: Star Strider 2018년 1월 12일

0 개 추천

It is difficult for me to understand what you are doing, even when I edited your post to correctly show the code.
This works for me:
t1 = clock;
t2 = t1 + [0 0 1 0 0 0]; % Add 1 Day
te = etime(t2,t1)
days = te/(60*60*24)
and also works correctly here:
t1 = clock;
t2 = t1 + [0 0 0 0 0 1]; % Add 1 Second
te = etime(t2,t1)
What precisely is the problem you are experiencing?
Note that hours, minutes, and seconds with respect to a datenum conversion are in fractions of a day, so you could be seeing the results of floating-point conversion error in your calculations, discussed in Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link).
EDIT
‘I guess it is more about the fact I am not considering bissextile years nor the different number of days in varied months. The etime function results may reflect these variations.’
The datetime data type and its functions were introduced in R2014b to deal with exactly these problems. See the documentation on Date and Time Arithmetic (link) for details.

댓글 수: 2

GCP
GCP 2018년 1월 12일
편집: GCP 2018년 1월 12일
Basically, if you consider years and months, the number of days change and this will affect the final calculation. In my formula, I ignored that "just noticed this". I assume etime does consider the differences. For this reason, when I check my numbers against the etime results, they are correct for some dates but not for all.
"I guess it is more about the fact I am not considering bissextile years nor the different number of days in varied months. The etime function results may reflect these variations.
"
As SS says, datetime was introduced in 14b to address the problems you are having.
>> d1 = datetime
d1 =
datetime
12-Jan-2018 12:40:21
>> d2 = d1 + seconds(1e9*rand)
d2 =
datetime
08-Nov-2034 00:26:14
>> dt = d2 - d1
dt =
duration
147443:45:53
>> dt.Format = 's'
dt =
duration
5.308e+08 sec
>> between(d1,d2)
ans =
calendarDuration
16y 9mo 26d 11h 45m 53.009s

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

카테고리

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

질문:

GCP
2018년 1월 12일

댓글:

2018년 1월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by