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