Thread Subject: datenum: additivity question...

Subject: datenum: additivity question...

From: Andrey Kazak

Date: 13 Jun, 2009 17:05:02

Message: 1 of 6

Greetings!

Could you help me please to figure out why the following expressions are not equal to zero:

datenum('00.01.0000 00:00:02', 'dd.mm.yy HH:MM:SS') - 2 * datenum('00.01.0000 00:00:01', 'dd.mm.yy HH:MM:SS')

datenum('00.01.0000 01:00:00', 'dd.mm.yy HH:MM:SS') - 3600 * datenum('00.01.0000 00:00:01', 'dd.mm.yy HH:MM:SS')

datenum('00.01.0001 00:00:00', 'dd.mm.yy HH:MM:SS') - 3600 * 24 * 365 * datenum('00.01.0000 00:00:01', 'dd.mm.yy HH:MM:SS')

etc ...

Subject: datenum: additivity question...

From: Kian

Date: 13 Jun, 2009 17:43:01

Message: 2 of 6

This has something to do with how MATLAB processes numbers. Basically, the decimal point you see on the screen has many many digits after it that you don't see on the screen. When you multiply by 2 some rounding occurs and therefore, you will end up with a 10^-10 number.

Subject: datenum: additivity question...

From: Andrey Kazak

Date: 14 Jun, 2009 03:24:01

Message: 3 of 6

"Kian " <kian.torab@utah.edu> wrote in message <h10of5$56k$1@fred.mathworks.com>...
> This has something to do with how MATLAB processes numbers. Basically, the decimal point you see on the screen has many many digits after it that you don't see on the screen. When you multiply by 2 some rounding occurs and therefore, you will end up with a 10^-10 number.

Does it deal only with rounding error only?
Can I freely make math operations with "serial date numbers"?

Subject: datenum: additivity question...

From: James Tursa

Date: 14 Jun, 2009 06:10:17

Message: 4 of 6

"Andrey Kazak" <AK@nospam.ru> wrote in message <h11qgh$mg0$1@fred.mathworks.com>...
> "Kian " <kian.torab@utah.edu> wrote in message <h10of5$56k$1@fred.mathworks.com>...
> > This has something to do with how MATLAB processes numbers. Basically, the decimal point you see on the screen has many many digits after it that you don't see on the screen. When you multiply by 2 some rounding occurs and therefore, you will end up with a 10^-10 number.
>
> Does it deal only with rounding error only?
> Can I freely make math operations with "serial date numbers"?

datenum results are doubles. You can freely make math operations with them just like you can other doubles. But you will have to live with the precision limitations of doubles. Just like you can't represent numbers like 1/3 exactly with a double, you can't in general represent your dates exactly with a double. So don't expect all of your calculations like subtractions etc. to come out exact.

James Tursa

Subject: datenum: additivity question...

From: Andrey Kazak

Date: 15 Jun, 2009 04:08:01

Message: 5 of 6

"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <h12489$n7b$1@fred.mathworks.com>...
> "Andrey Kazak" <AK@nospam.ru> wrote in message <h11qgh$mg0$1@fred.mathworks.com>...
> > "Kian " <kian.torab@utah.edu> wrote in message <h10of5$56k$1@fred.mathworks.com>...
> > > This has something to do with how MATLAB processes numbers. Basically, the decimal point you see on the screen has many many digits after it that you don't see on the screen. When you multiply by 2 some rounding occurs and therefore, you will end up with a 10^-10 number.
> >
> > Does it deal only with rounding error only?
> > Can I freely make math operations with "serial date numbers"?
>
> datenum results are doubles. You can freely make math operations with them just like you can other doubles. But you will have to live with the precision limitations of doubles. Just like you can't represent numbers like 1/3 exactly with a double, you can't in general represent your dates exactly with a double. So don't expect all of your calculations like subtractions etc. to come out exact.
>
> James Tursa

Thank you, James!

The final question: what is the precision of doubles in MATLAB? I mean the number of significant digits...

Subject: datenum: additivity question...

From: James Tursa

Date: 15 Jun, 2009 06:23:02

Message: 6 of 6

"Andrey Kazak" <AK@nospam.ru> wrote in message <h14hf1$jpk$1@fred.mathworks.com>...
> "James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <h12489$n7b$1@fred.mathworks.com>...
> > "Andrey Kazak" <AK@nospam.ru> wrote in message <h11qgh$mg0$1@fred.mathworks.com>...
> > > "Kian " <kian.torab@utah.edu> wrote in message <h10of5$56k$1@fred.mathworks.com>...
> > > > This has something to do with how MATLAB processes numbers. Basically, the decimal point you see on the screen has many many digits after it that you don't see on the screen. When you multiply by 2 some rounding occurs and therefore, you will end up with a 10^-10 number.
> > >
> > > Does it deal only with rounding error only?
> > > Can I freely make math operations with "serial date numbers"?
> >
> > datenum results are doubles. You can freely make math operations with them just like you can other doubles. But you will have to live with the precision limitations of doubles. Just like you can't represent numbers like 1/3 exactly with a double, you can't in general represent your dates exactly with a double. So don't expect all of your calculations like subtractions etc. to come out exact.
> >
> > James Tursa
>
> Thank you, James!
>
> The final question: what is the precision of doubles in MATLAB? I mean the number of significant digits...

About 15 decimal digits in general. For your datenum examples, the best way to see what precision means in terms of datenum results is to use eps. e.g.,

>> format long
>> d = datenum('00.01.0000 00:00:02', 'dd.mm.yy HH:MM:SS')
d =
    2.314813900738955e-005
>> eps(d)*86400
ans =
    2.927345865710862e-016
>> e = datenum('00.01.2009 00:00:02', 'dd.mm.yy HH:MM:SS')
e =
    7.337730000231481e+005
>> eps(e)*86400
ans =
    1.005828380584717e-005

So for your original example, which is very near the MATLAB reference date, the accuracy of the double representation is about 3e-16 seconds. Pretty good, but this is probably not a realistic date to use for this calculation. Using a modern date in 2009 yields an accuracy of the datenum result of about 1e-5 seconds, or 10 microseconds. So that is probably a more realistic number to use for the expected accuracy when using datenum results in calculations. If you need higher accuracy you should probably stick with datevec arrays and write your own custom arithmetic functions that operate on the arrays directly.

James Tursa

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
datenum Sprinceana 22 Jun, 2009 05:13:15
rssFeed for this Thread

Contact us at files@mathworks.com