Pb with date time sequence with period<1sec

classic Classic list List threaded Threaded
3 messages Options
DE
Reply | Threaded
Open this post in threaded view
|

Pb with date time sequence with period<1sec

DE
This post was updated on .
Hi,

I'd like to create a date-time seq with a period of 0.05 s, over several days. But If I am looking at the sequence, the seconds are not separated by 0.05, but by something very close (0.04999995).

# try :
start<-strptime(nom_fich,format="%y%m%d")
time<-seq(from=start, by=0.05, length.out = 86400*20*3)
print(as.POSIXlt(time[2])$sec)
# result is 0.04999995 and not 0.05 as expected

Same pb if I want to add a fraction of seconds to other date-time object :

# try :
start<-as.POSIXlt(1435701600,origin="1970-01-01")
print(as.POSIXlt(start+0.01)$sec)
# result is 0.00999999 and not 0.01 as expected

Any idea to solve this pb ?

Thank you in advance !
Reply | Threaded
Open this post in threaded view
|

Re: Pb with date time sequence with period<1sec

Sarah Goslee
Looks like R FAQ 7.31 to me.

https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

On Wed, Sep 9, 2015 at 4:19 AM, DE <[hidden email]> wrote:

> Hi,
>
> I'd like to create a date-time seq with a period of 0.05 s, over several
> days.
>
> # try :
> start<-strptime(nom_fich,format="%y%m%d")
> time<-seq(from=start, by=0.05, length.out = 86400*20*3)
> print(as.POSIXlt(time[2])$sec)
> # result is 0.04999995 and not 0.05 as expected
>
> But If I am looking at the sequence, the seconds are not separated by 0.05,
> but by something very close (0.04999995). Same pb if I want to add a
> fraction of seconds to a date-time object :
>
> # try :
> start<-strptime(nom_fich,format="%y%m%d")
> as.POSIXlt(start+0.05,origin="1970-01-01")$sec
> # result is 0.04999995 and not 0.05 as expected
>
> Any idea to solve this pb ?
>
> Thank you in advance !
>
>


--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Pb with date time sequence with period<1sec

Peter Dalgaard-2
Plus, current dates are an awful lot of seconds since 1970-01-01, so the relative error on second-scale differences is bigger than you might think:

> as.numeric(Sys.time())
[1] 1441874878

and since relative representation errors are of the order 1e-16, the corresponding absolute errors are about 1e-7.

(The examples given forgot to tell us what nom_fich is supposed to be, but I assume something relatively current was meant.)

-pd

On 09 Sep 2015, at 16:50 , Sarah Goslee <[hidden email]> wrote:

> Looks like R FAQ 7.31 to me.
>
> https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
>
> On Wed, Sep 9, 2015 at 4:19 AM, DE <[hidden email]> wrote:
>> Hi,
>>
>> I'd like to create a date-time seq with a period of 0.05 s, over several
>> days.
>>
>> # try :
>> start<-strptime(nom_fich,format="%y%m%d")
>> time<-seq(from=start, by=0.05, length.out = 86400*20*3)
>> print(as.POSIXlt(time[2])$sec)
>> # result is 0.04999995 and not 0.05 as expected
>>
>> But If I am looking at the sequence, the seconds are not separated by 0.05,
>> but by something very close (0.04999995). Same pb if I want to add a
>> fraction of seconds to a date-time object :
>>
>> # try :
>> start<-strptime(nom_fich,format="%y%m%d")
>> as.POSIXlt(start+0.05,origin="1970-01-01")$sec
>> # result is 0.04999995 and not 0.05 as expected
>>
>> Any idea to solve this pb ?
>>
>> Thank you in advance !
>>
>>
>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.