home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / double.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  4.3 KB  |  222 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. expanded class DOUBLE
  5. --
  6. -- Note : corresponding C type is "double"
  7. --
  8. inherit
  9.    DOUBLE_REF
  10.       redefine
  11.      infix "+", infix "-", infix "*", infix "/", infix "^",
  12.      prefix "+", prefix "-", valid_divisor, infix "<", 
  13.      compare, one, zero
  14.       end;
  15.    
  16. feature {ANY}
  17.  
  18.    infix "+" (other: DOUBLE): DOUBLE is
  19.      -- Add `other' to Current.
  20.       external "CSE"
  21.       end;
  22.  
  23.    infix "-" (other: DOUBLE): DOUBLE is
  24.      -- Subtract `other' from Current.
  25.       external "CSE"
  26.       end;
  27.  
  28.    infix "*" (other: DOUBLE): DOUBLE is
  29.      -- Multiply `other' by Current.
  30.       external "CSE"
  31.       end;
  32.    
  33.    infix "/" (other: DOUBLE): DOUBLE is
  34.      -- Divide Current by `other'.
  35.       external "CSE"
  36.       end;
  37.  
  38.    infix "^" (exp : INTEGER): DOUBLE is
  39.      -- Raise Current to `exp'-th power.
  40.      -- Note: use ANSI C pow.
  41.       external "CSE" 
  42.       end;
  43.  
  44.    prefix "+" : DOUBLE is
  45.       do
  46.      Result := Current
  47.       end;
  48.  
  49.    prefix "-": DOUBLE is
  50.       external "CSE"
  51.       end;
  52.  
  53.    abs: DOUBLE is
  54.       do
  55.      if Current < 0.0 then
  56.         Result := -Current;
  57.      else
  58.         Result := Current;
  59.      end;
  60.       end;
  61.          
  62.    infix "<" (other: DOUBLE): BOOLEAN is
  63.      -- Is Current less than `other'?
  64.       external "CSE"
  65.       end;
  66.    
  67.    compare(other: DOUBLE): INTEGER is
  68.      -- Compare Current with `other'.
  69.      -- '<' <==> Result < 0
  70.      -- '>' <==> Result > 0
  71.      -- Otherwise Result = 0
  72.       do
  73.      if Current < other then
  74.         Result := -1
  75.      else
  76.         if Current > other then
  77.            Result := 1
  78.         end;
  79.      end;
  80.       end;
  81.  
  82.    valid_divisor(other: DOUBLE): BOOLEAN is
  83.       do
  84.      Result := (other /= 0.0)
  85.       end;
  86.    
  87.    one: DOUBLE is 1.0;
  88.    
  89.    zero: DOUBLE is 0.0;
  90.    
  91.    floor: INTEGER is
  92.      -- Greatest integral value no greater than Current.
  93.       external "CSE"
  94.       ensure 
  95.      result_no_greater: Result <= Current;
  96.      close_enough: Current - Result < one;
  97.       end;
  98.       
  99.    ceiling: INTEGER is
  100.      -- Smallest integral value no smaller than Current.
  101.       do
  102.      Result := floor + 1;
  103.       ensure
  104.      result_no_smaller: Result >= Current;
  105.      -- *** BUG in ELKS :
  106.      -- close_enough: Result - Current < one;
  107.       end;
  108.    
  109.    truncated_to_integer: INTEGER is
  110.      -- Integer part (same sign, largest absolute value
  111.      -- no greater than Current).
  112.       local
  113.      
  114.       do
  115.      Result := floor;
  116.      if 0.5 + Result < Current then
  117.         Result := ceiling;
  118.      end;
  119.       end;
  120.    
  121.    to_real: REAL is
  122.      -- Note: C conversion from "double" to "float".
  123.      -- Thus, Result can be undefine (ANSI C).
  124.       external "CSE"
  125.       end;
  126.    
  127.    to_string: STRING is
  128.      -- Convert the DOUBLE into a new allocated STRING. 
  129.      -- Note: see `append_in' to save memory.
  130.       do
  131.      !!Result.make(0);
  132.      append_in(Result);
  133.       end; 
  134.  
  135.    append_in(str: STRING) is
  136.      -- Append the equivalent of `to_string' at the end of 
  137.      -- `str'. Thus you can save memory because no other
  138.      -- STRING is allocate for the job.
  139.       require
  140.      str /= Void;
  141.       local
  142.      d: DOUBLE;
  143.      i: INTEGER;
  144.       do
  145.      if Current < 0 then
  146.         str.extend('-');
  147.         d := -Current;
  148.      else
  149.         d := Current;
  150.      end;
  151.      i := d.floor;
  152.      i.append_in(str);
  153.      str.extend('.');
  154.      d := d - i;
  155.      from
  156.      invariant
  157.         d < 1;
  158.      until
  159.         d = 0.0
  160.      loop
  161.         d := d * 10.0;
  162.         i := d.floor;
  163.         d := d - i;
  164.         str.extend(i.digit);
  165.      end;
  166.       end; 
  167.    
  168.    to_string_format(d: INTEGER): STRING is
  169.      -- Convert the DOUBLE into a new allocated STRING including 
  170.      -- only `d' digits in fractionnal part. 
  171.      -- Note: see `append_in_format' to save memory.
  172.       do
  173.      !!Result.make(0);
  174.      append_in(Result);
  175.       end; 
  176.  
  177.    append_in_format(str: STRING; f: INTEGER) is
  178.      -- Same as `append_in' but produce only `f' digit of 
  179.      -- the fractionnal part.
  180.       require
  181.      str /= Void;
  182.      f >= 0;
  183.       local
  184.      r: DOUBLE;
  185.      i, f_count: INTEGER;
  186.       do
  187.      if Current < 0 then
  188.         str.extend('-');
  189.         r := -Current;
  190.      else
  191.         r := Current;
  192.      end;
  193.      i := r.floor;
  194.      i.append_in(str);
  195.      str.extend('.');
  196.      r := r - i;
  197.      from
  198.         f_count := f;
  199.      invariant
  200.            r < 1;
  201.      until
  202.         f_count = 0
  203.      loop
  204.         r := r * 10.0;
  205.         i := r.floor;
  206.         r := r - i;
  207.         str.extend(i.digit);
  208.         f_count := f_count - 1;
  209.      end;
  210.       end; 
  211.    
  212.    sqrt: DOUBLE is
  213.      -- Compute the square routine.
  214.      -- Note: use ANSI C sqrt.
  215.       require
  216.      Current >= 0;
  217.       external "CSE"
  218.       end;
  219.    
  220. end -- DOUBLE
  221.  
  222.