home *** CD-ROM | disk | FTP | other *** search
- -- 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
- 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 "^" (exp : INTEGER): DOUBLE is
- -- Raise Current to `exp'-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;
-
- floor: INTEGER is
- -- Greatest integral value no greater than Current.
- external "CSE"
- ensure
- result_no_greater: Result <= Current;
- close_enough: Current - Result < one;
- end;
-
- ceiling: INTEGER is
- -- Smallest integral value no smaller than Current.
- do
- Result := floor + 1;
- ensure
- result_no_smaller: Result >= Current;
- -- *** BUG in ELKS :
- -- close_enough: Result - Current < one;
- end;
-
- truncated_to_integer: INTEGER is
- -- Integer part (same sign, largest absolute value
- -- no greater than Current).
- local
-
- do
- Result := floor;
- if 0.5 + Result < Current then
- Result := ceiling;
- end;
- 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;
- local
- d: DOUBLE;
- i: INTEGER;
- do
- if Current < 0 then
- str.extend('-');
- d := -Current;
- else
- d := Current;
- end;
- i := d.floor;
- i.append_in(str);
- str.extend('.');
- d := d - i;
- from
- invariant
- d < 1;
- until
- d = 0.0
- loop
- d := d * 10.0;
- i := d.floor;
- d := d - i;
- str.extend(i.digit);
- end;
- 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(Result);
- 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;
- local
- r: DOUBLE;
- i, f_count: INTEGER;
- do
- if Current < 0 then
- str.extend('-');
- r := -Current;
- else
- r := Current;
- end;
- i := r.floor;
- i.append_in(str);
- str.extend('.');
- r := r - i;
- from
- f_count := f;
- invariant
- r < 1;
- until
- f_count = 0
- loop
- r := r * 10.0;
- i := r.floor;
- r := r - i;
- str.extend(i.digit);
- f_count := f_count - 1;
- end;
- end;
-
- sqrt: DOUBLE is
- -- Compute the square routine.
- -- Note: use ANSI C sqrt.
- require
- Current >= 0;
- external "CSE"
- end;
-
- end -- DOUBLE
-
-