Re: round not in math.h?

Wolfgang Jansen <[email protected]> Mon, 19 Sep 2005 18:48:02 +0200
Newsgroups gmane.comp.lang.eiffel.smalleiffel
Organization University of Potsdam, Institute of Informatics
Message-ID <[email protected]>
Heilig, Brian - ACD wrote:

>No, this is similar to floor but dangerous. It only truncates the
>fractional part and there is a risk that the float will not fit into the
>integer:
>
>"An rvalue of a floating point type can be converted to an rvalue of an
>integer type. The conversion truncates; that is, the fractional part is
>discarded. The behavior is undefined if the truncated value cannot be
>represented in the destination type."
>
>Brian
>
>  
>
Now, I see the problem. But what is the rounded value of a floating
point number if it does not fit into an integer of the same memory size?
For example, what is `round(12.3456e+78)' when working with
64-bit IEEE754 numbers? If the number (here 12.3456e+78) does not fit
into a 64-bit integer (say EIF_INTEGER_64) then functions `round' etc.
are meaningless; the best they can do is to return the argument unchanged.

I have to improve the program splitter as follows:

  EIF_REAL_64 x;
  EIF_INTEGER_64 n;

  int sign = (x < 0 ? -1 : 1);
  x = fabs(x)+0.5;
  if (x<ULLONG_MAX) {
    n = x;
    x = n;
  }
  if (sign<0)  x = -x;


Well, this is much more sophisitacted than I thought and
a library function `round' would make sense.
On the other hand, I conjecture that you (or the someone
maintaining the SmartEiffel libraries) must provide the function
yourselves. Not only that you do not find it in standard libraries
of the gcc (although it is in the man pages), other C compilers
do not provide the function at all!

WJ