Library - ratio

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

Functions

ratio::ratio

Ratio object constructor.

Syntax

use ratio
r = ratio
r = ratio(n)
r = ratio(num, den)
r = ratio(r)

Description

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.

==eq \mldivide
>=ge ^mpower
>gt /mrdivide
inv *mtimes
<=le ~=ne
<lt +plus
max -uminus
min +uplus
-minus  

Examples

use ratio
r = ratio(2, 3)
  r =
    2/3
q = 5 * r - 1
  q =
    7/3

See also

ratio::disp, ratio::double, ratio::char

ratio::char

Display a ratio object.

Syntax

use ratio
char(r)

Description

char(r) converts ratio r to a character string.

See also

ratio::ratio, ratio::disp, char

ratio::disp

Display a ratio object.

Syntax

use ratio
disp(r)

Description

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.

See also

ratio::ratio, ratio::char, disp

ratio::double

Convert a ratio object to a floating-point number.

Syntax

use ratio
x = double(r)

Description

double(r) converts ratio r to a floating-point number of class double.

Example

use ratio
r = ratio(2, 3);
double(r)
  0.6666

See also

ratio::ratio


Copyright 2002-2014, Calerga.
All rights reserved.