[R] julian() and numerical noise

R. Michael Weylandt michael.weylandt at gmail.com
Sat Mar 24 15:57:41 CET 2012


It might be easier to just work with a character representation rather
than fighting the floating point demons:

fnc <- function(date){
    as.Date(paste(date, "01", sep = "."), format = "%Y.%m.%d")
}

Hope that helps,

Michael

On Sat, Mar 24, 2012 at 10:47 AM, Andreas Eckner <andreas at eckner.com> wrote:
> Thanks for the help. A workaround seems indeed easy to write, for example,
> by rounding the the value of 'm' inside chron:::julian.default(), or only
> passing integers to the function.
>
> I never intended to use a case involving a quadrillionth of a day, but
> encountered the problem when converting a dates in YYYY.MM format to a CHRON
> object. Here is an example for future reference (the output might be highly
> system-dependent):
>
>>   library(chron)
>>   date <- 2012.02
>>   y <- floor(date)
>>   m <- (date %% 1) * 100
>>   julian(2, 1, 2012) - julian(m, 1, y)
> [1] 1
>
> ~Andreas
>
> P.S.: I have included Kurt Hornik on the email.
>
> On 3/23/2012 10:52 PM, R. Michael Weylandt wrote:
>>
>> julian() is in the package base. chron only provides julian.default to
>> avoid the error Uwe observed. If you look at the code for
>> julian.default, it's not too hard to see why date will be slightly
>> more sensitive to "numeric fuzz"....whether that's intentional (or
>> even a good/bad thing) is somewhat hard to say, but I'd guess the
>> maintainer didn't really consider a use case involving one
>> quadrillionth of a day.
>>
>> Is it problematic for you? If so, a workaround is not hard to provide
>>
>> Michael
>>
>> On Fri, Mar 23, 2012 at 4:17 PM, Andreas Eckner<andreas at eckner.com>
>>  wrote:
>>>
>>> Sorry, forgot to mention: R version 2.14.2
>>>
>>> The function julian() is part of the "chron" package of the base
>>> distribution.
>>>
>>> On 3/23/2012 2:47 PM, Uwe Ligges wrote:
>>>>
>>>> On 23.03.2012 15:45, Andreas Eckner wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> does anybody know if the following behavior of julian() is intentional?
>>>>>
>>>>>> julian(2, 1, 2012) - julian(2 - 1e-15, 1, 2012)
>>>>>
>>>>> [1] 1
>>>>>>
>>>>>> julian(2, 1, 2012) - julian(2, 1 - 1e-15, 2012)
>>>>>
>>>>> [1] 0
>>>>>>
>>>>>> julian(2, 1, 2012) - julian(2, 1, 2012 - 1e-15)
>>>>>
>>>>> [1] 0
>>>>>
>>>>> In other words, julian() is subject to numerical noise in the 'day'
>>>>> argument, but not in the 'month' and 'year' argument? Another example:
>>>>>
>>>>>> julian(2 - 1e-15, 1, 2012) - julian(1 - 1e-15, 1, 2012)
>>>>>
>>>>> [1] 30
>>>>
>>>>
>>>>
>>>> Which vbersion of R are you talking about?
>>>>
>>>> I get:
>>>>
>>>>> julian(2, 1, 2012) - julian(2 - 1e-15, 1, 2012)
>>>>
>>>> Error in UseMethod("julian") :
>>>>  no applicable method for 'julian' applied to an object of class
>>>> "c('double', 'numeric')"
>>>>
>>>>
>>>> Uwe Ligges
>>>>
>>>>
>>>>> Thanks,
>>>>> Andreas
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> 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.
>>>>
>>>>
>>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> 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.
>>
>>
>



More information about the R-help mailing list