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 REAL
- --
- -- Note : corresponding C type is "float"
- --
- inherit
- REAL_REF
- redefine
- infix "+", infix "-", infix "*", infix "/", infix "^",
- prefix "+", prefix "-", valid_divisor, infix "<",
- infix "<=", infix ">", infix ">=", compare, one,
- zero, print_on
- end;
-
- feature {ANY}
-
- infix "+" (other: REAL): REAL is
- -- Add `other' to Current.
- external "CSE"
- end;
-
- infix "-" (other: REAL): REAL is
- -- Subtract `other' from Current.
- external "CSE"
- end;
-
- infix "*" (other: REAL): REAL is
- -- Multiply `other' by Current.
- external "CSE"
- end;
-
- infix "/" (other: REAL): REAL is
- -- Divide Current by `other'.
- external "CSE"
- end;
-
- infix "^" (exp : INTEGER): REAL is
- -- Raise Current to `exp'-th power.
- do
- check
- Current = 0.0 implies exp > 0;
- end;
- Result := (to_double ^ exp).to_real;
- end;
-
- prefix "+" : REAL is
- do
- Result := Current
- end;
-
- prefix "-": REAL is
- external "CSE"
- end;
-
- abs: REAL is
- do
- if Current < 0.0 then
- Result := -Current;
- else
- Result := Current;
- end;
- end;
-
- infix "<" (other: REAL): BOOLEAN is
- -- Is Current less than `other'?
- external "CSE"
- end;
-
- infix "<=" (other: REAL): BOOLEAN is
- -- Is Current smaller or equal to `other'?
- external "CSE"
- end;
-
- infix ">" (other: REAL): BOOLEAN is
- -- Is Current greater than `other'?
- external "CSE"
- end;
-
- infix ">=" (other: REAL): BOOLEAN is
- -- Is Current greater or equal to `other'?
- external "CSE"
- end;
-
- compare(other: REAL): 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: REAL): BOOLEAN is
- do
- Result := (other /= 0.0)
- end;
-
- one: REAL is 1.0;
-
- zero: REAL is 0.0;
-
- floor: INTEGER is
- -- Greatest integral value no greater than Current.
- do
- Result := to_double.floor;
- ensure
- result_no_greater: Current >= Result;
- close_enough: Current - Result < one;
- end;
-
- ceiling: INTEGER is
- -- Smallest integral value no smaller than Current.
- do
- Result := floor + 1;
- ensure
- result_no_smaller: Current <= Result;
- close_enough: Result.to_real - Current < one;
- end;
-
- truncated_to_integer: INTEGER is
- -- Integer part (same sign, largest absolute value
- -- no greater than Current).
- do
- Result := floor;
- if 0.5 + Result < Current then
- Result := ceiling;
- end;
- end;
-
- to_string: STRING is
- -- Convert the REAL into a new allocated STRING.
- -- Note: see `append_in' to save memory.
- do
- Result := to_double.to_string;
- 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
- to_double.append_in(str);
- end;
-
- to_string_format(d: INTEGER): STRING is
- -- Convert the REAL into a new allocated STRING including
- -- only `d' digits in fractionnal part.
- -- Note: see `append_in_format' to save memory.
- do
- Result := to_double.to_string_format(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
- to_double.append_in_format(str,f);
- end;
-
- to_double: DOUBLE is
- -- Note: C conversion from "float" to "double".
- do
- Result := Current;
- end;
-
- sqrt: DOUBLE is
- -- Compute the square routine.
- require
- Current >= 0;
- do
- Result := to_double.sqrt;
- end;
-
- print_on(file: STD_FILE_WRITE) is
- do
- file.put_real(Current);
- end;
-
- end -- class REAL
-
-