\manpage {rational} {} {Rational Numbers}
 
\definition An instance $q$ of type \name\ is a rational number
where the numerator and the denominator are both of type
$integer$.
 
\creation q
 
\create { }
   { creates an instance \var\ of type \name. }
 
 
\create {(double\ x)}
   { creates an instance \var\ of type \name\ and initializes it
with the value of $x$. }
 
 
\create {(int\ n)}
   { creates an instance \var\ of type \name\ and initializes it
with the value of $n$. }
 
 
\create {(int\ m,\ int\ n)}
   { creates an instance \var\ of type \name\ and initializes its
numerator with $m$ and its denominator with $n$. }
 
 
\create {(integer\ a)}
   { creates an instance \var\ of type \name\ and initializes it
with the value of $a$. }
 
 
\create {(integer\ a,\ integer\ b)}
   { creates an instance \var\ of type \name\ and initializes its
numerator with $a$ and its denominator with $b$. }
 
 
\operations 2 5
 

The arithmetic operations $+,\ -,\ *,\ /,\ +=,\
-=,\ *=,\ /=,\ -$(unary), $++,\ --$,  
the comparison operations $<,\ <=,\ >,\ 
>=,\ ==,\ !=$ and the stream operations are all available.
 
\op  integer numerator {()}
           {  returns the numerator of $q$. }
 
 
\op  integer denominator {()}
           {  returns the denominator of $q$. }
 
 
\op  rational\& simplify {( integer\ a)}
           {  simplifies $q$ by $a$.\\
        \precond $a$ divides the numerator and the denominator of $q$. }
 
 
\op  rational\& normalize {()}
           {  normalizes \var. }
 
 
\op  void negate {()}
           {  negates \var. }
 
 
\op  void invert {()}
           {  inverts \var. }
 
 
\op  rational inverse {()}
           {  returns the inverse of \var. }
 
 

\smallskip
{\bf Non-member functions}
\smallskip 
 
\func  int sign {( rational\ q)}
           {  returns the sign of $q$. }
 
 
\func  rational abs {( rational\ q)}
           {  returns the absolute value of $q$. }
 
 
\func  rational sqr {( rational\ q)}
           {  returns the square of $q$. }
 
 
\func  rational pow {( rational\ q, \ int\ n)}
           {  returns the $n$-th power of $q$. }
 
 
\func  rational pow {( rational\ q, \ integer\ a)}
           {  returns the $a$-th power of $q$. }
 
 
\func  integer trunc {( rational\ q)}
           {  returns the $integer$ with the next smaller absolute value. }
 
 
\func  integer floor {( rational\ q)}
           {  returns the next smaller $integer$. }
 
 
\func  integer ceil {( rational\ q)}
           {  returns the next bigger $integer$. }
 
 
\func  integer round {( rational\ q)}
           {  rounds $q$ to the nearest $integer$. }
 
 
\implementation A $rational$ is implemented by two $integer$
numbers which represent the numerator and the denominator. The sign
is represented by the sign of the numerator.
 
