home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-13 | 5.8 KB | 303 lines | [TEXT/ttxt] |
- -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C)
- -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
- --
- expanded class DOUBLE
- --
- -- Note : corresponding C type is "double"
- --
- inherit
- DOUBLE_REF
- redefine
- infix "+", infix "-", infix "*", infix "/", infix "^",
- prefix "+", prefix "-", valid_divisor, infix "<",
- compare, one, zero, fill_tagged_out_memory,
- out_in_tagged_out_memory
- end;
-
- feature {ANY}
-
- infix "+" (other: DOUBLE): DOUBLE is
- -- Add `other' to Current.
- external "CSE"
- end;
-
- infix "-" (other: DOUBLE): DOUBLE is
- -- Subtract `other' from Current.
- external "CSE"
- end;
-
- infix "*" (other: DOUBLE): DOUBLE is
- -- Multiply `other' by Current.
- external "CSE"
- end;
-
- infix "/" (other: DOUBLE): DOUBLE is
- -- Divide Current by `other'.
- external "CSE"
- end;
-
- infix "^" (e: INTEGER): DOUBLE is
- -- Raise Current to `e'-th power.
- -- Note: use ANSI C pow.
- external "CSE"
- end;
-
- prefix "+" : DOUBLE is
- do
- Result := Current
- end;
-
- prefix "-": DOUBLE is
- external "CSE"
- end;
-
- abs: DOUBLE is
- do
- if Current < 0.0 then
- Result := -Current;
- else
- Result := Current;
- end;
- end;
-
- infix "<" (other: DOUBLE): BOOLEAN is
- -- Is Current less than `other'?
- external "CSE"
- end;
-
- compare(other: DOUBLE): INTEGER is
- -- Compare Current with `other'.
- -- '<' <==> Result < 0
- -- '>' <==> Result > 0
- -- Otherwise Result = 0
- do
- if Current < other then
- Result := -1
- else
- if Current > other then
- Result := 1
- end;
- end;
- end;
-
- valid_divisor(other: DOUBLE): BOOLEAN is
- do
- Result := (other /= 0.0)
- end;
-
- one: DOUBLE is 1.0;
-
- zero: DOUBLE is 0.0;
-
- double_floor: DOUBLE is
- -- Greatest integral value no greater than Current.
- external "CSE"
- end;
-
- floor: INTEGER is
- -- Greatest integral value no greater than Current.
- local
- d: DOUBLE;
- do
- d := double_floor;
- Result := d.truncated_to_integer;
- ensure
- result_no_greater: Result.to_double <= Current;
- close_enough: Current - Result < one;
- end;
-
- double_ceiling: DOUBLE is
- -- Smallest integral value no smaller than Current.
- do
- Result := ceil;
- end;
-
- ceiling: INTEGER is
- -- Smallest integral value no smaller than Current.
- local
- d: DOUBLE;
- do
- d := double_ceiling;
- Result := d.truncated_to_integer;
- ensure
- result_no_smaller: Current <= Result;
- close_enough: Current - Result < one;
- end;
-
- rounded: INTEGER is
- -- Rounded integral value.
- local
- d: DOUBLE;
- do
- if double_floor + 0.5 < Current then
- Result := double_ceiling.truncated_to_integer;
- else
- Result := double_floor.truncated_to_integer;
- end;
- end;
-
- truncated_to_integer: INTEGER is
- -- Integer part (same sign, largest absolute value
- -- no greater than Current).
- local
- d: DOUBLE;
- do
- d := double_floor;
- c_inline_c("R=((int)_d);");
- ensure
- Result.to_double <= Current
- end;
-
- to_real: REAL is
- -- Note: C conversion from "double" to "float".
- -- Thus, Result can be undefine (ANSI C).
- external "CSE"
- end;
-
- to_string: STRING is
- -- Convert the DOUBLE into a new allocated STRING.
- -- Note: see `append_in' to save memory.
- do
- !!Result.make(0);
- append_in(Result);
- end;
-
- append_in(str: STRING) is
- -- Append the equivalent of `to_string' at the end of
- -- `str'. Thus you can save memory because no other
- -- STRING is allocate for the job.
- require
- str /= Void;
- do
- tmp_string.ansi_c_sprintf(Current,"%%f");
- from
- until
- tmp_string.last /= '0'
- loop
- tmp_string.remove_last(1);
- end;
- str.append(tmp_string);
-
- end;
-
- to_string_format(d: INTEGER): STRING is
- -- Convert the DOUBLE into a new allocated STRING including
- -- only `d' digits in fractionnal part.
- -- Note: see `append_in_format' to save memory.
- do
- !!Result.make(0);
- append_in_format(Result,d);
- end;
-
- append_in_format(str: STRING; f: INTEGER) is
- -- Same as `append_in' but produce only `f' digit of
- -- the fractionnal part.
- require
- str /= Void;
- f >= 0;
- do
- fmt.clear;
- fmt.extend('%%');
- fmt.extend('.');
- f.append_in(fmt);
- fmt.extend('f');
- fmt.extend('%/0/');
- tmp_string.ansi_c_sprintf(Current,fmt);
- str.append(tmp_string);
- end;
-
- feature -- Maths functions :
-
- sqrt: DOUBLE is
- -- Square routine (ANSI C sqrt).
- require
- Current >= 0;
- external "CSE"
- end;
-
- sin: DOUBLE is
- -- Sinus (ANSI C sin).
- external "CSE"
- end;
-
- cos: DOUBLE is
- -- Cosinus (ANSI C cos).
- external "CSE"
- end;
-
- tan: DOUBLE is
- -- (ANSI C tan).
- external "CSE"
- end;
-
- asin: DOUBLE is
- -- (ANSI C asin).
- external "CSE"
- end;
-
- acos: DOUBLE is
- -- (ANSI C acos).
- external "CSE"
- end;
-
- atan: DOUBLE is
- -- (ANSI C atan).
- external "CSE"
- end;
-
- sinh: DOUBLE is
- -- (ANSI C sinh).
- external "CSE"
- end;
-
- cosh: DOUBLE is
- -- (ANSI C cosh).
- external "CSE"
- end;
-
- tanh: DOUBLE is
- -- (ANSI C tanh).
- external "CSE"
- end;
-
- exp: DOUBLE is
- -- (ANSI C exp).
- external "CSE"
- end;
-
- log: DOUBLE is
- -- (ANSI C log).
- external "CSE"
- end;
-
- log10: DOUBLE is
- -- (ANSI C log10).
- external "CSE"
- end;
-
- feature -- Object Printing :
-
- out_in_tagged_out_memory, fill_tagged_out_memory is
- do
- Current.append_in(tagged_out_memory);
- end;
-
- feature {NONE}
-
- ceil: DOUBLE is
- external "CSE"
- end;
-
- tmp_string: STRING is
- once
- !!Result.make(1024);
- end;
-
- fmt: STRING is
- once
- !!Result.make(4);
- end;
-
- end -- DOUBLE
-
-