rational.icn: Procedures for arithmetic on rational numbers

procedure addrat:          sum of rationals
procedure divrat:          divide rationals.
procedure medrat:          form rational mediant
procedure mpyrat:          multiply rationals
procedure negrat:          negative of rational
procedure rat2real:        floating-point approximation of rational
procedure rat2str:         convert rational to string
procedure ratred:          reduce rational to lowest terms
procedure real2rat:        convert to rational with precision p
procedure reciprat:        reciprocal of rational
procedure subrat:          difference of rationals

link rational
June 10, 2001; Ralph E. Griswold
Contributor: Gregg M. Townsend
This file is in the public domain.

These procedures perform arithmetic on rational numbers (fractions):

addrat(r1,r2) Add rational numbers r1 and r2.

divrat(r1,r2) Divide rational numbers r1 and r2.

medrat(r1,r2) Form mediant of r1 and r2.

mpyrat(r1,r2) Multiply rational numbers r1 and r2.

negrat(r)     Produce negative of rational number r.

rat2real(r)   Produce floating-point approximation of r

rat2str(r)    Convert the rational number r to its string
              representation.

real2rat(v,p) Convert real to rational with precision p.
              The default precision is 1e-10.
              (Too much precision gives huge, ugly factions.)

reciprat(r)   Produce the reciprocal of rational number r.

str2rat(s)    Convert the string representation of a rational number
              (such as "3/2") to a rational number.

subrat(r1,r2) Subtract rational numbers r1 and r2.

Source code | Program Library Page | Icon Home Page