2

Is there a way in R to do rational arithmetic? I need something like:

> rat(1, 2) - rat(1, 3)
1/6

R doesn't have standard rational data type. And I can not find any package which implement it.

anton0xf
  • 51
  • 6
  • 4
    You mean like `library(MASS); fractions(1/2 - 1/3)`? Probably a duplicate of http://stackoverflow.com/questions/5046026/print-number-as-reduced-fraction-in-r – Rich Scriven Jun 05 '16 at 21:04
  • I just simply googled it. I suppose [THIS](https://cran.r-project.org/web/packages/rcdd/vignettes/vinny.pdf) does the job. – 989 Jun 05 '16 at 21:20
  • @m0h3n +1 this is a great link to really well written documentation. – Chris Jun 05 '16 at 22:03
  • I assume something like data type, which consists of two integers representing numerator and denominator, with exact results for operations. But MASS use some approximations: `fractions(1)/fractions(123456) => 0` instead of 1/123456 – anton0xf Jun 06 '16 at 18:17
  • @m0h3n, can you give example of rcdd usage for rational arithmetic? I found this library too before asking this question but could not figure out how to use it in a reasonable amount of time. – anton0xf Jun 06 '16 at 18:47
  • [gmp](https://cran.r-project.org/package=gmp) looks like the answer, but I can not install it on my ubuntu 16.4: it complains `GNU MP not found, or not 4.1.4 or up` however `libgmp10` package is installed. – anton0xf Jul 25 '18 at 14:42

0 Answers0