Skip to contents

R has no built in rational number representation; humdrumR defines one.

Usage

rational(numerator, denominator = as.integer64(1L))

e1 %R% e2

numerator(x)

denominator(x)

# S4 method for rational
numerator(x)

# S4 method for rational
denominator(x)

is.rational(x)

# S4 method for rational
is.numeric(x)

# S4 method for rational
rank(x, na.last = TRUE, ties.method = "average")

# S4 method for rational,rational
Compare(e1, e2)

# S4 method for rational,ANY
Compare(e1, e2)

# S4 method for ANY,rational
Compare(e1, e2)

# S4 method for rational
Summary(x)

# S4 method for rational
prod(x, ..., na.rm = FALSE)

# S4 method for rational
abs(x)

# S4 method for rational
sign(x)

# S4 method for rational
max(x, ..., na.rm = FALSE)

# S4 method for rational
min(x, ..., na.rm = FALSE)

# S4 method for rational
mean(x)

# S4 method for rational
round(x)

# S4 method for rational
floor(x)

# S4 method for rational
ceiling(x)

# S4 method for rational
trunc(x)

# S4 method for rational
expand(x)

# S4 method for rational
sum(x, ..., na.rm = FALSE)

# S4 method for rational
cumsum(x)

as.rational(x, ...)

# S4 method for rational
as.rational(x, ...)

# S4 method for matrix
as.rational(x)

# S4 method for integer
as.rational(x)

# S4 method for numeric
as.rational(x)

# S4 method for logical
as.rational(x)

# S4 method for character
as.rational(x, sep = "/|%")

# S4 method for fraction
as.rational(x, sep = "/|%")

fraction(numerator, denominator, sep = "/")

as.fraction(x, sep = "/")

# S3 method for fraction
as.double(x)

# S3 method for fraction
as.integer(x)

Details

Using rational numbers, we can represent numbers like 1/3 without any numeric inaccuracies. In other words, \(1/3 * 3 = 3\), never \(.999999999\). On the other hand, if our rational numbers start to have numerators or demoninators that are too large, we can run into integer overflow problems. Since the rational numbers we'll be using in the context of music analysis are relatively simple, we can safely use such numbers without any numeric inaccuracy.

fraction is a class (and associated constructor) which represents rational numbers as character strings. Unlike rational, the fraction class is not numeric and thus cannot do arithmetic. However, fraction can be converted to/from rational.