home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / numeric.e < prev    next >
Text File  |  1999-06-05  |  3KB  |  130 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. deferred class  NUMERIC
  13. --
  14. -- This class describes a ring.
  15. --
  16.    
  17. inherit
  18.    HASHABLE 
  19.       undefine is_equal
  20.       end;
  21.    
  22. feature  
  23.  
  24.    infix "+" (other: like Current): like Current is
  25.          -- Sum with 'other' (commutative).
  26.       require
  27.          other /= Void
  28.       deferred
  29.       end;
  30.    
  31.    infix "-" (other: like Current): like Current is
  32.          -- Result of substracting `other'.
  33.       require
  34.          other /= Void
  35.       deferred
  36.       end;
  37.  
  38.    infix "*" (other: like Current): like Current is
  39.          -- Product by `other'.
  40.       require
  41.          other /= Void
  42.       deferred
  43.       end;
  44.    
  45.    infix "/" (other: like Current): NUMERIC is
  46.          -- Division by `other'.
  47.       require
  48.          other /= Void;
  49.          divisible (other)
  50.       deferred
  51.       end;
  52.  
  53.    infix "^" (exp: INTEGER): NUMERIC is
  54.          -- `Current' raised to `exp'-th power.
  55.       require
  56.          exp >= 0 
  57.       local
  58.          e      : INTEGER;
  59.          product: like Current;
  60.          factor : like Current;
  61.       do
  62.          product := one;
  63.          factor  := Current;
  64.          from
  65.             e := exp;
  66.          until
  67.             e = 0
  68.          loop
  69.             if (e \\ 2) = 1 then
  70.                product := product * factor
  71.             end;
  72.             e := e // 2;
  73.             factor := factor * factor;
  74.          end;
  75.          Result := product;
  76.       end;
  77.  
  78.    prefix "+" : like Current is
  79.          -- Unary plus of `Current'.
  80.       do
  81.          Result := Current
  82.       end;
  83.  
  84.    prefix "-" : like Current is
  85.          -- Unary minus of `Current'.
  86.       do
  87.          Result := zero - Current
  88.       end;
  89.  
  90.    divisible(other: like Current): BOOLEAN is
  91.          -- May `Current' be divided by `other' ?
  92.       require
  93.          other /= Void
  94.       deferred
  95.       end;
  96.  
  97.    one: like Current is
  98.          -- Neutral element for "*" and "/".
  99.       deferred
  100.       end;
  101.  
  102.    zero: like Current is
  103.          -- Neutral element for "+" and "-".
  104.       deferred
  105.       end;
  106.    
  107.    sign: INTEGER is
  108.          -- Sign of Current (0 -1 or 1).
  109.       do
  110.          if Current > zero then
  111.             Result := 1;
  112.          elseif Current < zero then
  113.             Result := -1;
  114.          end;
  115.       ensure
  116.          -1 <= Result; Result <= 1
  117.       end;
  118.  
  119.    infix "<" (other: like Current): BOOLEAN is
  120.       deferred
  121.       end;
  122.  
  123.    infix ">" (other: like Current): BOOLEAN is
  124.       deferred
  125.       end;
  126.  
  127. end --  NUMERIC
  128.  
  129.  
  130.