Library ratio implements the constructors and methods of class ratio for rational numbers. It is based on long integers, so that the precision is limited only by available memory. Basic arithmetic operators and functions are overloaded to support expressions with the same syntax as for numbers.
The following statement makes available functions defined in ratio:
use ratio
Ratio object constructor.
r = ratio r = ratio(n) r = ratio(num, den) r = ratio(r)
ratio(num, den) creates a rational fraction object whose value is num/den. Arguments num and den may be double integer numbers or longint. Common factors are canceled out. With one numeric input argument, ratio(n) creates a rational fraction whose denominator is 1. Without input argument, ratio creates a rational number whose value is 0.
With one input argument which is already a ratio object, ratio returns it without change.
The following operators and functions may be used with ratio objects, with results analog to the corresponding functions of LME.
|
|
r = ratio(2, 3) r = 2/3 q = 5 * r - 1 q = 7/3
ratio::disp, ratio::double, ratio::char
Display a ratio object.
char(r)
char(r) converts ratio r to a character string.
ratio::ratio, ratio::disp, char
Display a ratio object.
disp(r)
disp(r) displays ratio r with the same format as char. It is also executed implicitly when LME displays the ratio result of an expression which does not end with a semicolon.
ratio::ratio, ratio::char, disp
Convert a ratio object to a floating-point number.
x = double(r)
double(r) converts ratio r to a floating-point number of class double.
r = ratio(2, 3); double(r) 0.6666