home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Small Eiffel 0.4.8 / lib_std / numeric.e < prev    next >
Encoding:
Text File  |  1997-04-13  |  2.3 KB  |  124 lines  |  [TEXT/ttxt]

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. deferred class  NUMERIC
  5. --
  6. -- This class describes a ring.
  7. --
  8.    
  9. inherit
  10.    ANY 
  11.       undefine is_equal
  12.       end;
  13.    
  14. feature  
  15.  
  16.    infix "+" (other: like Current): like Current is
  17.      -- Sum of 'Current' and 'other'.
  18.       require
  19.      other /= Void
  20.       deferred
  21.       end;
  22.    
  23.    infix "-" (other: like Current): like Current is
  24.      -- Difference of 'Current' and 'other'.
  25.       require
  26.      other /= Void
  27.       deferred
  28.       end;
  29.  
  30.    infix "*" (other: like Current): like Current is
  31.      -- Product of 'Current' and 'other'.
  32.       require
  33.      other /= Void
  34.       deferred
  35.       end;
  36.    
  37.    infix "/" (other: like Current): NUMERIC is
  38.      -- Quotient of 'Current' and 'other'.
  39.       require
  40.      other /= Void;
  41.      valid_divisor (other)
  42.       deferred
  43.       end;
  44.  
  45.    infix "^" (exp: INTEGER): NUMERIC is
  46.      -- 'Current' raised to 'exp'-th power.
  47.       require
  48.      exp >= 0 
  49.       local
  50.      e      : INTEGER;
  51.      product: like Current;
  52.      factor : like Current;
  53.       do
  54.      product := one;
  55.      factor  := Current;
  56.      from
  57.         e := exp;
  58.      until
  59.         e = 0
  60.      loop
  61.         if (e \\ 2) = 1 then
  62.            product := product * factor
  63.         end;
  64.         e := e // 2;
  65.         factor := factor * factor;
  66.      end;
  67.      Result := product;
  68.       end;
  69.  
  70.    prefix "+" : like Current is
  71.      -- Unary plus of 'Current'.
  72.       do
  73.      Result := Current
  74.       end;
  75.  
  76.    prefix "-" : like Current is
  77.      -- Negative of 'Current'.
  78.       do
  79.      Result := zero - Current
  80.       end;
  81.  
  82.    valid_divisor(other: like Current): BOOLEAN is
  83.      -- Is 'other' a valid divisor for 'Current'?
  84.       require
  85.      other /= Void
  86.       deferred
  87.       end;
  88.  
  89.    one: like Current is
  90.      -- The neutral element of multiplication.
  91.       deferred
  92.       ensure
  93.      neutral_element: -- Result is the neutral element of 
  94.      -- multiplication.
  95.       end;
  96.  
  97.    zero: like Current is
  98.      -- The neutral element of addition.
  99.       deferred
  100.       ensure
  101.      neutral_element: -- Result is the neutral element of 
  102.      -- addition.
  103.       end;
  104.    
  105.    sign: INTEGER is
  106.       do
  107.      if Current > zero then
  108.         Result := 1;
  109.      elseif Current < zero then
  110.         Result := -1;
  111.      end;
  112.       end;
  113.  
  114.    infix "<" (other: like Current): BOOLEAN is
  115.       deferred
  116.       end;
  117.  
  118.    infix ">" (other: like Current): BOOLEAN is
  119.       deferred
  120.       end;
  121.  
  122. end -- class NUMERIC
  123.  
  124.