https://github.com/bertcarnell/rational
An R rational number class using a variety of class systems
https://github.com/bertcarnell/rational
r
Last synced: 17 days ago
JSON representation
An R rational number class using a variety of class systems
- Host: GitHub
- URL: https://github.com/bertcarnell/rational
- Owner: bertcarnell
- License: gpl-2.0
- Created: 2015-06-29T00:42:01.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-07-28T20:37:29.000Z (10 months ago)
- Last Synced: 2024-07-29T14:38:55.972Z (10 months ago)
- Topics: r
- Language: R
- Size: 162 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rational
A rational number class using a variety of class systems
[](https://github.com/bertcarnell/rational/actions/workflows/r-cmd-check.yml)
[](https://codecov.io/github/bertcarnell/rational?branch=master)[Rational Website](https://bertcarnell.github.io/rational/)
## Why a Rational Package?
This package serves 2 purposes:
- Demonstrates the creation of the same classes in S3, S4, and R6 class systems
- Demonstrates one way to solve some numerical accuracy problems that people struggle with```r
> # expectations dashed
> (0.1 + 0.2) == 0.3
[1] FALSE
> # what?
> print(0.1 + 0.2, digits = 20)
[1] 0.30000000000000004
> # what am I supposed to do in R? (or any other floating point arithmetic system)
> all.equal(0.1 + 0.2, 0.3, tolerance = 1E-9)
[1] TRUE
> abs(0.1 + 0.2 - 0.3) < 1E-9
[1] TRUE
> # is there another way? Yes, rational numbers
> # NOTE: the "L" notation indicates an integer
> rational(1L, 10L) + rational(2L, 10L) == rational(3L, 10L)
[1] TRUE
```