Hi All, is there a function in R that allows me to work with fractions without transforming them to floats (or whatever) in between? Something that would calculate something like: (1/2 + 1/8) * 1/2 = 5/16 without ever transforming to 0.5 and 0.125? Best, Federico -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com
require(MASS) ?as.fractions as.fractions(1/2+1/8) -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O<http://maps.google.com/maps?f=q&hl=en&q=Curitiba,+Brazil&layer=&ie=UTF8&z=18&ll=-25.448315,-49.276916&spn=0.002054,0.005407&t=k&om=1> On 25/06/07, Federico Calboli <f.calboli@imperial.ac.uk> wrote:> > Hi All, > > is there a function in R that allows me to work with fractions without > transforming them to floats (or whatever) in between? > > Something that would calculate something like: > > (1/2 + 1/8) * 1/2 = 5/16 > > without ever transforming to 0.5 and 0.125? > > Best, > > Federico > > -- > Federico C. F. Calboli > Department of Epidemiology and Public Health > Imperial College, St Mary's Campus > Norfolk Place, London W2 1PG > > Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 > > f.calboli [.a.t] imperial.ac.uk > f.calboli [.a.t] gmail.com > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >[[alternative HTML version deleted]]
> is there a function in R that allows me to work with fractions without > transforming them to floats (or whatever) in between? > >You could use the ryacas (CAS) package: http://code.google.com/p/ryacas/ and/or http://cran.r-project.org/doc/vignettes/Ryacas/Ryacas.pdf e.g:> library(Ryacas) > yacas(expression((1/2 + 1/8) * 1/2 ))[1] "Starting Yacas!" expression(5/16)>Stefan> Something that would calculate something like: > > (1/2 + 1/8) * 1/2 = 5/16 > > without ever transforming to 0.5 and 0.125? >
On Mon, 25 Jun 2007, Henrique Dallazuanna wrote:> require(MASS) > ?as.fractions > as.fractions(1/2+1/8)I think Federico wanted 1/as.fractions(2) + 1/as.fractions(8) that is avoiding computing 1/8 in float (although it is exact). You might be better off with package gmp:> 1/as.bigq(2) + 1/as.bigq(8)[1] "5/8" allows you to handle arbitrarily large fractions. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595