record Fraction(num: int, den: int)
A Fraction is a rational number num / den kept in lowest terms with a positive denominator. Being exact, it sidesteps the rounding error of floating point:
val third = Fraction::of(1, 3)
val whole = third + third + third // exactly 1/1, not 0.999...
Operator methods (plus, minus, times, div) back + - * / directly, and toDouble crosses over to floating point when you need it.
Fractionstatic def of(n: int, d: int): Fraction
Builds a normalized fraction n / d, reduced with a positive denominator.
def plus(o: Fraction): Fraction
Adds o to this fraction. Backs the + operator.
def minus(o: Fraction): Fraction
Subtracts o from this fraction. Backs the - operator.
def times(o: Fraction): Fraction
Multiplies this fraction by o. Backs the * operator.
def toDouble(): double
The closest Double to this exact value.
def show(): String
Renders the fraction as num/den.
static def gcd(a: int, b: int): int
num: int
A Fraction is a rational number num / den kept in lowest terms with a positive denominator. Being exact, it sidesteps the rounding error of floating point:
val third = Fraction::of(1, 3)
val whole = third + third + third // exactly 1/1, not 0.999...
Operator methods (plus, minus, times, div) back + - * / directly, and toDouble crosses over to floating point when you need it.
Fractionden: int
A Fraction is a rational number num / den kept in lowest terms with a positive denominator. Being exact, it sidesteps the rounding error of floating point:
val third = Fraction::of(1, 3)
val whole = third + third + third // exactly 1/1, not 0.999...
Operator methods (plus, minus, times, div) back + - * / directly, and toDouble crosses over to floating point when you need it.
Fraction