[R] Number -> Fraction
Thomas Lumley
tlumley at u.washington.edu
Thu Sep 13 18:31:31 CEST 2007
I should have remembered MASS::fractions(). This has a slightly different
aim, giving continued fraction approximations to any real number, where my
function just tries to handle numbers that really are close to fractions
with small denominators.
For example, unfrac() won't report a fraction for sqrt(2), even with
tol=0.1, max=10000, since sqrt(2), which doesn't have very good rational
approximations (algebraic numbers tend not to).
-thomas
On Thu, 13 Sep 2007, Ravi Varadhan wrote:
> Finding sufficiently accurate rational approximations to a real number, can
> be done using fractions() in MASS, which uses continued fractions.
> "Sufficient" accuracy can be specified using the number of cycles and
> maximum denominator size options (note that max.denom is the final term in
> the continued fraction).
>
> Some examples:
>> library(MASS)
>> fractions(0.333333,max.denom=100000)
> [1] 1/3
>> fractions(0.333333,max.denom=1000000)
> [1] 1519169814041/4557513999637
>> fractions(0.333333,max.denom=1000000,cycles=11)
> [1] 8587703744937/25763136997948
>
>> fractions(pi,max=1)
> [1] 3
>> fractions(pi,max=10)
> [1] 22/7
>> fractions(pi,max=100)
> [1] 355/113
>> fractions(pi,max=1000)
> [1] 4272943/1360120
>> fractions(pi,max=1000,cycles=12)
> [1] 80143857/25510582
> This last rational approximation to pi is quite accurate, up to machine
> precision (i.e. 16 digits)
>
> Ravi.
>
> ----------------------------------------------------------------------------
> -------
>
> Ravi Varadhan, Ph.D.
>
> Assistant Professor, The Center on Aging and Health
>
> Division of Geriatric Medicine and Gerontology
>
> Johns Hopkins University
>
> Ph: (410) 502-2619
>
> Fax: (410) 614-9625
>
> Email: rvaradhan at jhmi.edu
>
> Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
>
>
>
> ----------------------------------------------------------------------------
> --------
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Thomas Lumley
> Sent: Thursday, September 13, 2007 11:34 AM
> To: Mauro Arnoldi
> Cc: r-help at r-project.org
> Subject: Re: [R] Number -> Fraction
>
> On Thu, 13 Sep 2007, Mauro Arnoldi wrote:
>
>> Hi everybody!
>> I'm new to this list and also to the R program.
>>
>> I'd like to know if there is a function able to convert results into
>> Fractional form like my scientific calculator have. For example:
>>
>>> 1/3
>> [1] 0.3333333
>>
>>> function_that_return_a_fraction_from_numbers(0.3333333)
>> [1] 1/3
>>
>
> This must have some restrictions (so it doesn't return 3333333/1000000,
> which would be a more accurate fraction).
>
> One approach is
>> unfrac <- function(x, max=100, tol=0.01){
> num <- x * (1:max)
> err <- (num - round(num)) * (1:max)
> if (!any(abs(err) < tol))
> return(NA)
> i <- which.min(abs(err))
> c(round(num[i]), i)
> }
>
> This returns the best fraction approximation with denominator up to `max`,
> where `best` is in terms of the non-integer part of the numerator, and no
> answer is given if the non-integer part of the numerator is more than
> `tol`
>
>> unfrac(0.3333333)
> [1] 1 3
>> unfrac(pi)
> [1] NA
>> unfrac(pi,max=1000)
> [1] 355 113
>> unfrac(pi,tol=0.1)
> [1] 22 7
>
> -thomas
>
> ______________________________________________
> 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.
>
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
More information about the R-help
mailing list