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

  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, fill_tagged_out_memory, 
  14.      out_in_tagged_out_memory
  15.       end;
  16.    
  17. feature {ANY}
  18.  
  19.    infix "+" (other: DOUBLE): DOUBLE is
  20.      -- Add `other' to Current.
  21.       external "CSE"
  22.       end;
  23.  
  24.    infix "-" (other: DOUBLE): DOUBLE is
  25.      -- Subtract `other' from Current.
  26.       external "CSE"
  27.       end;
  28.  
  29.    infix "*" (other: DOUBLE): DOUBLE is
  30.      -- Multiply `other' by Current.
  31.       external "CSE"
  32.       end;
  33.    
  34.    infix "/" (other: DOUBLE): DOUBLE is
  35.      -- Divide Current by `other'.
  36.       external "CSE"
  37.       end;
  38.  
  39.    infix "^" (e: INTEGER): DOUBLE is
  40.      -- Raise Current to `e'-th power.
  41.      -- Note: use ANSI C pow.
  42.       external "CSE" 
  43.       end;
  44.  
  45.    prefix "+" : DOUBLE is
  46.       do
  47.      Result := Current
  48.       end;
  49.  
  50.    prefix "-": DOUBLE is
  51.       external "CSE"
  52.       end;
  53.  
  54.    abs: DOUBLE is
  55.       do
  56.      if Current < 0.0 then
  57.         Result := -Current;
  58.      else
  59.         Result := Current;
  60.      end;
  61.       end;
  62.          
  63.    infix "<" (other: DOUBLE): BOOLEAN is
  64.      -- Is Current less than `other'?
  65.       external "CSE"
  66.       end;
  67.    
  68.    compare(other: DOUBLE): INTEGER is
  69.      -- Compare Current with `other'.
  70.      -- '<' <==> Result < 0
  71.      -- '>' <==> Result > 0
  72.      -- Otherwise Result = 0
  73.       do
  74.      if Current < other then
  75.         Result := -1
  76.      else
  77.         if Current > other then
  78.            Result := 1
  79.         end;
  80.      end;
  81.       end;
  82.  
  83.    valid_divisor(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 "CSE"
  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.       local
  130.      d: DOUBLE;
  131.       do
  132.      if double_floor + 0.5 < Current then
  133.         Result := double_ceiling.truncated_to_integer;
  134.      else
  135.         Result := double_floor.truncated_to_integer;
  136.      end;
  137.       end;
  138.    
  139.    truncated_to_integer: INTEGER is
  140.            -- Integer part (same sign, largest absolute value
  141.             -- no greater than Current).
  142.       local
  143.      d: DOUBLE;
  144.       do
  145.      d := double_floor;
  146.      c_inline_c("R=((int)_d);");
  147.       ensure
  148.      Result.to_double <= Current
  149.       end;
  150.    
  151.    to_real: REAL is
  152.      -- Note: C conversion from "double" to "float".
  153.      -- Thus, Result can be undefine (ANSI C).
  154.       external "CSE"
  155.       end;
  156.    
  157.    to_string: STRING is
  158.      -- Convert the DOUBLE into a new allocated STRING. 
  159.      -- Note: see `append_in' to save memory.
  160.       do
  161.      !!Result.make(0);
  162.      append_in(Result);
  163.       end; 
  164.  
  165.    append_in(str: STRING) is
  166.      -- Append the equivalent of `to_string' at the end of 
  167.      -- `str'. Thus you can save memory because no other
  168.      -- STRING is allocate for the job.
  169.       require
  170.      str /= Void;
  171.       do
  172.      tmp_string.ansi_c_sprintf(Current,"%%f");
  173.      from
  174.      until
  175.         tmp_string.last /= '0'
  176.      loop
  177.         tmp_string.remove_last(1);
  178.      end;
  179.      str.append(tmp_string);
  180.      
  181.       end; 
  182.    
  183.    to_string_format(d: INTEGER): STRING is
  184.      -- Convert the DOUBLE into a new allocated STRING including 
  185.      -- only `d' digits in fractionnal part. 
  186.      -- Note: see `append_in_format' to save memory.
  187.       do
  188.      !!Result.make(0);
  189.      append_in_format(Result,d);
  190.       end; 
  191.  
  192.    append_in_format(str: STRING; f: INTEGER) is
  193.      -- Same as `append_in' but produce only `f' digit of 
  194.      -- the fractionnal part.
  195.       require
  196.      str /= Void;
  197.      f >= 0;
  198.       do
  199.      fmt.clear;
  200.      fmt.extend('%%');
  201.      fmt.extend('.');
  202.      f.append_in(fmt);
  203.      fmt.extend('f');
  204.      fmt.extend('%/0/');
  205.      tmp_string.ansi_c_sprintf(Current,fmt);
  206.      str.append(tmp_string);
  207.       end; 
  208.    
  209. feature -- Maths functions :
  210.  
  211.    sqrt: DOUBLE is
  212.      -- Square routine (ANSI C sqrt).
  213.       require
  214.      Current >= 0;
  215.       external "CSE"
  216.       end;
  217.  
  218.    sin: DOUBLE is
  219.      -- Sinus (ANSI C sin).
  220.       external "CSE"
  221.       end;
  222.  
  223.    cos: DOUBLE is
  224.      -- Cosinus (ANSI C cos).
  225.       external "CSE"
  226.       end;
  227.  
  228.    tan: DOUBLE is
  229.      -- (ANSI C tan).
  230.       external "CSE"
  231.       end;
  232.  
  233.    asin: DOUBLE is
  234.      -- (ANSI C asin).
  235.       external "CSE"
  236.       end;
  237.  
  238.    acos: DOUBLE is
  239.      -- (ANSI C acos).
  240.       external "CSE"
  241.       end;
  242.  
  243.    atan: DOUBLE is
  244.      -- (ANSI C atan).
  245.       external "CSE"
  246.       end;
  247.  
  248.    sinh: DOUBLE is
  249.      -- (ANSI C sinh).
  250.       external "CSE"
  251.       end;
  252.  
  253.    cosh: DOUBLE is
  254.      -- (ANSI C cosh).
  255.       external "CSE"
  256.       end;
  257.  
  258.    tanh: DOUBLE is
  259.      -- (ANSI C tanh).
  260.       external "CSE"
  261.       end;
  262.  
  263.    exp: DOUBLE is
  264.      -- (ANSI C exp).
  265.       external "CSE"
  266.       end;
  267.  
  268.    log: DOUBLE is
  269.      -- (ANSI C log).
  270.       external "CSE"
  271.       end;
  272.  
  273.    log10: DOUBLE is
  274.      -- (ANSI C log10).
  275.       external "CSE"
  276.       end;
  277.  
  278. feature -- Object Printing :
  279.  
  280.    out_in_tagged_out_memory, fill_tagged_out_memory is
  281.       do
  282.      Current.append_in(tagged_out_memory);
  283.       end;
  284.  
  285. feature {NONE}
  286.  
  287.    ceil: DOUBLE is 
  288.       external "CSE"
  289.       end;
  290.  
  291.    tmp_string: STRING is
  292.       once
  293.      !!Result.make(1024);
  294.       end;
  295.  
  296.    fmt: STRING is
  297.       once
  298.      !!Result.make(4);
  299.       end;
  300.  
  301. end -- DOUBLE
  302.  
  303.