home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_std / real.e < prev    next >
Text File  |  1999-06-05  |  6KB  |  261 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. expanded class  REAL
  13. --
  14. -- Note: An Eiffel REAL is mapped as a C float or as a Java float.
  15. --
  16.  
  17. inherit
  18.    REAL_REF
  19.       redefine
  20.          infix "+", infix "-", infix "*", infix "/", infix "^",
  21.          prefix "+", prefix "-", divisible, infix "<", 
  22.          infix "<=", infix ">", infix ">=", out_in_tagged_out_memory, 
  23.          fill_tagged_out_memory, hash_code
  24.       end;
  25.    
  26. feature {ANY}
  27.  
  28.    infix "+" (other: REAL): REAL is
  29.       external "SmallEiffel"
  30.       end;
  31.  
  32.    infix "-" (other: REAL): REAL is
  33.       external "SmallEiffel"
  34.       end;
  35.  
  36.    infix "*" (other: REAL): REAL is
  37.       external "SmallEiffel"
  38.       end;
  39.    
  40.    infix "/" (other: REAL): REAL is
  41.       external "SmallEiffel"
  42.       end;
  43.  
  44.    infix "^" (e: INTEGER): DOUBLE is
  45.       do
  46.          check
  47.             Current = 0.0 implies e > 0;
  48.          end;
  49.          Result := to_double ^ e; 
  50.       end;
  51.  
  52.    prefix "+" : REAL is
  53.       do
  54.          Result := Current
  55.       end;
  56.  
  57.    prefix "-": REAL is
  58.       external "SmallEiffel"
  59.       end;
  60.  
  61.    abs: REAL is
  62.       do
  63.          if Current < 0.0 then
  64.             Result := -Current;
  65.          else
  66.             Result := Current;
  67.          end;
  68.       end;
  69.          
  70.    infix "<" (other: REAL): BOOLEAN is
  71.       external "SmallEiffel"
  72.       end;
  73.    
  74.    infix "<=" (other: REAL): BOOLEAN is
  75.       external "SmallEiffel"
  76.       end;
  77.    
  78.    infix ">" (other: REAL): BOOLEAN is
  79.       external "SmallEiffel"
  80.       end;
  81.    
  82.    infix ">=" (other: REAL): BOOLEAN is
  83.       external "SmallEiffel"
  84.       end;
  85.    
  86.    divisible(other: REAL): BOOLEAN is
  87.       do
  88.          Result := (other /= 0.0)
  89.       end;
  90.    
  91.    floor: INTEGER is
  92.          -- Greatest integral value no greater than Current.
  93.       do
  94.          Result := to_double.floor;
  95.       ensure 
  96.          result_no_greater: Current >= Result;
  97.          close_enough: Current - Result < one;
  98.       end;
  99.       
  100.    ceiling: INTEGER is
  101.          -- Smallest integral value no smaller than Current.
  102.       do
  103.          Result := to_double.ceiling;
  104.       ensure
  105.          result_no_smaller: Current <= Result;
  106.          close_enough: Result.to_real - Current < one;
  107.       end;
  108.  
  109.    rounded: INTEGER is
  110.          -- Rounded integral value.
  111.       do
  112.          Result := to_double.rounded;
  113.       end;
  114.  
  115.    truncated_to_integer: INTEGER is
  116.          -- Integer part (same sign, largest absolute value
  117.          -- no greater than Current).
  118.       do
  119.          Result := to_double.truncated_to_integer;
  120.       ensure
  121.          Result.to_real <= Current
  122.       end;
  123.    
  124.    to_string: STRING is
  125.          -- Convert the REAL into a new allocated STRING. 
  126.          -- Note: see `append_in' to save memory.
  127.       do
  128.          Result := to_double.to_string;
  129.       end; 
  130.  
  131.    append_in(str: STRING) is
  132.          -- Append the equivalent of `to_string' at the end of 
  133.          -- `str'. Thus you can save memory because no other
  134.          -- STRING is allocate for the job.
  135.       require
  136.          str /= Void;
  137.       do
  138.          to_double.append_in(str);
  139.       end; 
  140.    
  141.    to_string_format(d: INTEGER): STRING is
  142.          -- Convert the REAL into a new allocated STRING including 
  143.          -- only `d' digits in fractionnal part. 
  144.          -- Note: see `append_in_format' to save memory.
  145.       do
  146.          Result := to_double.to_string_format(d);
  147.       end; 
  148.  
  149.    append_in_format(str: STRING; f: INTEGER) is
  150.          -- Same as `append_in' but produce only `f' digit of 
  151.          -- the fractionnal part.
  152.       require
  153.          str /= Void;
  154.          f >= 0
  155.       do
  156.          to_double.append_in_format(str,f);
  157.       end; 
  158.    
  159.    to_double: DOUBLE is
  160.       external "SmallEiffel"
  161.       end;
  162.    
  163. feature -- Maths functions :
  164.  
  165.    sqrt: DOUBLE is
  166.          -- Compute the square routine.
  167.       require
  168.          Current >= 0
  169.       do
  170.          Result := to_double.sqrt;
  171.       end;
  172.    
  173.    sin: DOUBLE is
  174.          -- Sinus (ANSI C sin).
  175.       do
  176.          Result := to_double.sin;
  177.       end;
  178.  
  179.    cos: DOUBLE is
  180.          -- Cosinus (ANSI C cos).
  181.       do
  182.          Result := to_double.cos;
  183.       end;
  184.  
  185.    tan: DOUBLE is
  186.          -- (ANSI C tan).
  187.       do
  188.          Result := to_double.tan;
  189.       end;
  190.  
  191.    asin: DOUBLE is
  192.          -- (ANSI C asin).
  193.       do
  194.          Result := to_double.asin;
  195.       end;
  196.  
  197.    acos: DOUBLE is
  198.          -- (ANSI C acos).
  199.       do
  200.          Result := to_double.acos;
  201.       end;
  202.  
  203.    atan: DOUBLE is
  204.          -- (ANSI C atan).
  205.       do
  206.          Result := to_double.atan;
  207.       end;
  208.  
  209.    sinh: DOUBLE is
  210.          -- (ANSI C sinh).
  211.       do
  212.          Result := to_double.sinh;
  213.       end;
  214.  
  215.    cosh: DOUBLE is
  216.          -- (ANSI C cosh).
  217.       do
  218.          Result := to_double.cosh;
  219.       end;
  220.  
  221.    tanh: DOUBLE is
  222.          -- (ANSI C tanh).
  223.       do
  224.          Result := to_double.tanh;
  225.       end;
  226.  
  227.    exp: DOUBLE is
  228.          -- (ANSI C exp).
  229.       do
  230.          Result := to_double.exp;
  231.       end;
  232.  
  233.    log: DOUBLE is
  234.          -- (ANSI C log).
  235.       do
  236.          Result := to_double.log;
  237.       end;
  238.  
  239.    log10: DOUBLE is
  240.          -- (ANSI C log10).
  241.       do
  242.          Result := to_double.log10;
  243.       end;
  244.  
  245. feature -- Object Printing :
  246.  
  247.    out_in_tagged_out_memory, fill_tagged_out_memory is
  248.       do
  249.          Current.append_in(tagged_out_memory);
  250.       end;
  251.  
  252. feature -- Hashing :
  253.  
  254.    hash_code: INTEGER is
  255.       do
  256.          Result := to_double.hash_code;
  257.       end;
  258.  
  259. end --  REAL
  260.  
  261.