Thread Subject: problem with datenum and precision

Subject: problem with datenum and precision

From: tstmwind

Date: 7 Oct, 2008 20:51:02

Message: 1 of 9

There is a difference in value with:

dif=(datenum('10-21-2007 13:00:00','mm-dd-yyyy HH:MM:SS')+(7/24))-datenum('10-21-2007 20:00:00','mm-dd-yyyy HH:MM:SS')

dif=-1.1642e-010

I am using 'datenum' to convert dates between time zones (hence '+7/24') and then using 'find' to call out specific dates. The '/' operator does not appear to use the same precision as datenum, and when dif~=0, 'find' results in 'Empty matrix: 0-by-1' . Is there a way that I can work around this issue without using datevec instead (which would require several more lines of code)?

Simplified Example:

dates=char(...
'10-21-2007 11:00:00',...
'10-21-2007 12:00:00',...
'10-21-2007 13:00:00',...
'10-21-2007 14:00:00')

%convert from PDT to UTC
dateser=datenum(dates,'mm-dd-yyyy HH:MM:SS')+7/24;

%specify date to search for
userinput='10-21-2007 20:00:00';
x=datenum(userinput,'mm-dd-yyyy HH:MM:SS');
index=find(dateser==x)

index =

   Empty matrix: 0-by-1

%difference between user input and data
dif=x-dateser(3)

dif =

  1.1642e-010

%specify date to search for
userinput='10-21-2007 21:00:00';
x=datenum(userinput,'mm-dd-yyyy HH:MM:SS');
index=find(dateser==x)

index =

     4

%difference between user input and data
dif=x-dateser(4)

dif =

     0

Subject: problem with datenum and precision

From: NZTideMan

Date: 7 Oct, 2008 21:48:08

Message: 2 of 9

On Oct 8, 9:51=A0am, "tstmwind " <tstmw...@yahoo.com> wrote:
> There is a difference in value with:
>
> dif=3D(datenum('10-21-2007 13:00:00','mm-dd-yyyy HH:MM:SS')+(7/24))-daten=
um('10-21-2007 20:00:00','mm-dd-yyyy HH:MM:SS')
>
> dif=3D-1.1642e-010
>
> I am using 'datenum' to convert dates between time zones (hence '+7/24') =
and then using 'find' to call out specific dates. The '/' operator does not=
 appear to use the same precision as datenum, and when dif~=3D0, 'find' res=
ults in =A0'Empty matrix: 0-by-1' . Is there a way that I can work around t=
his issue without using datevec instead (which would require several more l=
ines of code)?
>
> Simplified Example:
>
> dates=3Dchar(...
> '10-21-2007 11:00:00',...
> '10-21-2007 12:00:00',...
> '10-21-2007 13:00:00',...
> '10-21-2007 14:00:00')
>
> %convert from PDT to UTC
> dateser=3Ddatenum(dates,'mm-dd-yyyy HH:MM:SS')+7/24;
>
> %specify date to search for
> userinput=3D'10-21-2007 20:00:00';
> x=3Ddatenum(userinput,'mm-dd-yyyy HH:MM:SS');
> index=3Dfind(dateser=3D=3Dx)
>
> index =3D
>
> =A0 =A0Empty matrix: 0-by-1
>
> %difference between user input and data
> dif=3Dx-dateser(3)
>
> dif =3D
>
> =A0 1.1642e-010
>
> %specify date to search for
> userinput=3D'10-21-2007 21:00:00';
> x=3Ddatenum(userinput,'mm-dd-yyyy HH:MM:SS');
> index=3Dfind(dateser=3D=3Dx)
>
> index =3D
>
> =A0 =A0 =A04
>
> %difference between user input and data
> dif=3Dx-dateser(4)
>
> dif =3D
>
> =A0 =A0 =A00

1.1642e-010 days =3D 86400*1.1642e-010 =3D 1.0059e-005 seconds
Do you really need this level of accuracy?

Decide how accurate you need to be, say:
tol=3D1/86400; % i.e. 1 second
indx=3Dabs(dif) < tol;

Subject: problem with datenum and precision

From: tstmwind

Date: 7 Oct, 2008 22:15:03

Message: 3 of 9


> Do you really need this level of accuracy?
>


I don't, but 'find' does and that's the problem. Unless x precisely equals values in dateser, then you get an empty matrix. The dif is only shown in the example to demonstrate the precision differences of the code and is not utilized in my program. What I want to know is if there is a work around for the differences in precision when using the function 'find', or if there is an alternative to 'find' or 'datenum' that would work.

Subject: problem with datenum and precision

From: NZTideMan

Date: 7 Oct, 2008 22:21:09

Message: 4 of 9

On Oct 8, 11:15=A0am, "tstmwind " <tstmw...@yahoo.com> wrote:
> > Do you really need this level of accuracy?
>
> I don't, but 'find' does and that's the problem. Unless x precisely equal=
s values in dateser, then you get an empty matrix. The dif is only shown in=
 the example to demonstrate the precision differences of the code and is no=
t utilized in my program. What I want to know is if there is a work around =
for the differences in precision when using the function 'find', or if ther=
e is an alternative to 'find' or 'datenum' that would work.

Didn't you read my reply?
You're wrong about 'find'. It is only looking for a logical true, so
you don't HAVE to use =3D=3D.
You can use it like this:
indx=3Dfind(abs(dif) < tol);
where tol is some small number that you set.

Subject: problem with datenum and precision

From: NZTideMan

Date: 7 Oct, 2008 22:22:38

Message: 5 of 9

On Oct 8, 11:21=A0am, NZTideMan <mul...@gmail.com> wrote:
> On Oct 8, 11:15=A0am, "tstmwind " <tstmw...@yahoo.com> wrote:
>
> > > Do you really need this level of accuracy?
>
> > I don't, but 'find' does and that's the problem. Unless x precisely equ=
als values in dateser, then you get an empty matrix. The dif is only shown =
in the example to demonstrate the precision differences of the code and is =
not utilized in my program. What I want to know is if there is a work aroun=
d for the differences in precision when using the function 'find', or if th=
ere is an alternative to 'find' or 'datenum' that would work.
>
> Didn't you read my reply?
> You're wrong about 'find'. =A0It is only looking for a logical true, so
> you don't HAVE to use =3D=3D.
> You can use it like this:
> indx=3Dfind(abs(dif) < tol);
> where tol is some small number that you set.

And where
dif=3Dx-dateser;

Subject: problem with datenum and precision

From: Walter Roberson

Date: 7 Oct, 2008 23:24:33

Message: 6 of 9

tstmwind wrote:
>> Do you really need this level of accuracy?

> I don't, but 'find' does and that's the problem. Unless x precisely equals values in dateser,
> then you get an empty matrix. The dif is only shown in the example to demonstrate the
> precision differences of the code and is not utilized in my program. What I want to know is
> if there is a work around for the differences in precision when using the function 'find',
> or if there is an alternative to 'find' or 'datenum' that would work.

NZTideMan already showed you the work-around: do not test for floating point equality,
test for differences less than your required tolerance.

You should assume that *any* two calculation paths that have the slightest difference in
calculation methods may produce different floating point results. Don't even assume
that A+B+C will come out the same as C+B+A, because in the general case, it won't!

>> A=eps(1/2);B=1+eps;C=-eps(1/2);
>> sprintf('%.50g',A+B+C)

ans =

1.0000000000000004440892098500626161694526672363281

>> sprintf('%.50g',C+B+A)

ans =

1

This is not a problem with datestr, and it is not a problem with find: it is a
problem with the impossibility of representing all numbers with *any* finite
representation scheme. No matter what you do, you aren't going to be able to
represent Pi or e -exactly- in any finite representation: any finite representation
is algebraic, and Pi and e are transcendental. (Yes, I know that you didn't
use Pi or e: the point is that representing real numbers -exactly- is not *possible*
with finite resources.) In any finite fixed point or floating point representation
scheme with a fixed numeric base, there will be an uncountable number of numbers
which cannot be represented exactly.

1/24 has no exact representation in binary: 1/24 is 1/(8*3) and although the 1/8
has an exact representation (0.001 binary), 1/3 is an infinite repeating number
in binary (or decimal, for that matter.) Thus you should not expect that
7/24 will have an exact representation, so you should be *expecting* round-off
problems in the calculations.

Subject: problem with datenum and precision

From: Carlos Adrian Vargas Aguilera

Date: 9 Oct, 2008 13:53:05

Message: 7 of 9

Take a look to DATEVECFIX and NUMCMP at the FileExchange:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=19051

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=21190

Carlos

Subject: problem with datenum and precision

From: Vergil Weatherford

Date: 9 Nov, 2009 15:15:20

Message: 8 of 9

NZTideMan <mulgor@gmail.com> wrote in message <8b98cf7d-9925-4c39-bde5-bba17cd01de2@u29g2000pro.googlegroups.com>...
> > Didn't you read my reply?
> > You're wrong about 'find'. =A0It is only looking for a logical true, so
> > you don't HAVE to use =3D=3D.
> > You can use it like this:
> > indx=3Dfind(abs(dif) < tol);
> > where tol is some small number that you set.
>
> And where
> dif=3Dx-dateser;

I'm having the exact same problem, but I'm not sure I understand the solution. What is all this 3Dfind() and =3D=3D stuff? I assume it is part of the workaround, but I can't seem to find anything about that anywhere in MATLAB's help.

Can someone please explain this solution and how it works?

Thanks,
Vergil

Subject: problem with datenum and precision

From: someone

Date: 9 Nov, 2009 16:11:02

Message: 9 of 9

"Vergil Weatherford" <vergilcw@gmail.com> wrote in message <hd9bm8$q9k$1@fred.mathworks.com>...
> NZTideMan <mulgor@gmail.com> wrote in message <8b98cf7d-9925-4c39-bde5-bba17cd01de2@u29g2000pro.googlegroups.com>...
> > > Didn't you read my reply?
> > > You're wrong about 'find'. =A0It is only looking for a logical true, so
> > > you don't HAVE to use =3D=3D.
> > > You can use it like this:
> > > indx=3Dfind(abs(dif) < tol);
> > > where tol is some small number that you set.
> >
> > And where
> > dif=3Dx-dateser;
>
> I'm having the exact same problem, but I'm not sure I understand the solution. What is all this 3Dfind() and =3D=3D stuff? I assume it is part of the workaround, but I can't seem to find anything about that anywhere in MATLAB's help.
>
> Can someone please explain this solution and how it works?
>
> Thanks,
> Vergil

Take a look at the answer (and the cited references)
to Q6.1 of the MATLAB FAQ at:

http://matlabwiki.mathworks.com/MATLAB_FAQ

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
numcmp Carlos Adrian Vargas Aguilera 9 Oct, 2008 10:00:15
datevec Carlos Adrian Vargas Aguilera 9 Oct, 2008 10:00:15
datenum tstmwind 7 Oct, 2008 16:55:06
rssFeed for this Thread

Contact us at files@mathworks.com