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

ratio::ratio

Ratio object constructor.

Syntax

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.

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

Examples

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

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

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

x = double(r)

Description

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

Example

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

See also

ratio::ratio


Copyright 2002-2007, Calerga.
All rights reserved.