[R] Number -> Fraction

Ravi Varadhan rvaradhan at jhmi.edu
Thu Sep 13 18:07:02 CEST 2007


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.



More information about the R-help mailing list