Mathematics with Python and Ruby/Fractions in Ruby



A fraction is nothing more than the exact quotient of an integer by another one. As the result is not necessarily a decimal number, fractions have been used way before the decimal numbers: They are known at least since the Egyptians and Babylonians. And they still have many uses nowadays, even when they are more or less hidden like in these examples:


 * 1) If a man is said to be 5'7" high, this means that his height, in feet, is $$5+\frac{7}{12}=\frac{67}{12}$$.
 * 2) Hearing that it is 8 hours 13, one can infer that, past midday, $$8+\frac{13}{60}=\frac{493}{60}$$ hours have passed.
 * 3) If a price is announced as three quarters it means that, in dollars, it is $$\frac{3}{4}$$: Once again, a fraction!
 * 4) Probabilities are often given as fraction (mostly of the egyptian type). Like in "There is one chance over 10 millions that a meteor fall on my head" or "Stallion is favorite at five against one".
 * 5) Statistics too like fractions: "5 people over 7 think there are too many surveys".

The equality 0.2+0.5=0.7 can be written as $$\frac{2}{10}+\frac{5}{10}=\frac{7}{10}$$ but conversely, $$\frac{1}{2}+\frac{1}{3}=\frac{5}{6}$$ cannot be written as a decimal equality because such an equality would not be exact.

=How to get a fraction in Ruby=

To enter a fraction, the Rational object is used:

The simplification is automatic. An other way is to use mathn, which changes the behavior of the slash operator:

It is also possible to get a fraction from a real number with its to_r method. Yet the fraction is ensured to be correct only if its denominator is a power of 2 :

In this case, to_r from String is more exact:

=Properties of the fractions=

Numerator
To get the numerator of a fraction f, one enters f.numerator:

Notice, the result is not 24, because the fraction will be reduced to 12/5.

Denominator
To get the denominator of a fraction f, one enters f.denominator:

Value
An approximate value of a fraction is obtained by a conversion to a float:

=Operations=

Negation
Like any number, the negation of a fraction is obtained while preceding its name by the minus sign "-":

inverse
To invert a fraction, one divides 1 by this fraction:

Addition
To add two fractions, one uses the "+" symbol, and the result will be a fraction.

If a non-fraction is added to a fraction, the resulting type will vary. Adding a fraction and an integer will result in a fraction. But, adding a fraction and a float results in a float.

Subtraction
Likewise, to subtract two fractions, one writes the minus sign between them:

Multiplication
The product of two fractions will ever be a fraction either:

Division
The integer quotient and remainder are still defined for fractions:

Exponentiation
If the exponent is an integer, the power of a fraction will still be a fraction:

But if the exponent is a float, even if the power is actually a fraction, Ruby will give it as a float:

=Algorithms=

Farey mediant
Ruby has no method to compute the Farey mediant of two fractions, but it is easy to create it with a definition:

Egyptian fractions
To write a fraction like the Egyptians did, one can use Fibonacci's algorithm:



The algorithm can be summed up like this:


 * 1) One extracts the integer part of the fraction (with to_i) and stores it in a list;
 * 2) One subtracts to f (the remaining fraction) the largest integer inverse possible;
 * 3) And so on while the numerator of f is larger than one.
 * 4) Finally one adds the last fraction to the list.