datenum seconds within minutes

6 visualizzazioni (ultimi 30 giorni)
egrandje
egrandje il 22 Mar 2018
Commentato: Peter Perkins il 23 Mar 2018
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.

Risposta accettata

Stephen23
Stephen23 il 22 Mar 2018
Modificato: Stephen23 il 22 Mar 2018
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:

Più risposte (1)

Walter Roberson
Walter Roberson il 22 Mar 2018
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 Commento
Peter Perkins
Peter Perkins il 23 Mar 2018
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

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by