Topic: foat vs. double vs. long double ???
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sat, 2 Oct 1993 15:41:46 GMT Raw View
In article <CE8BtE.AAp@Colorado.EDU> jeremic@rankineColorado.EDU (Boris Jeremic) writes:
>Hello everybody,
>
>
>I am having a problem in finding out my machine epsilon with BC31 and with
>SUN C++ 2.1 .
>
>I know that plain C promotes float to double but I think that this
>is not the case in C++ ( I think there is something about that in ARM ).
The ARM passage you are probably thinking of is on page 35:
"Like ANSI C, C++ generates code for operations involving only
float (and not double or long double) operands without widening
to double."
I believe that this is an error in the ARM. It is (at the very least) an
over-simplification of the rules regarding floating-point expression
evaluation as given in section 3.1.2.5 of the "classic" ANSI C standard.
At the very end of that section, there is a clear statement as follows:
"The values of floating operands and of the results of floating
expressions may be represented in greater precision and range than
that required by the type; the types are not changed thereby."
Thus, given three variables `a', `b', and `c', all declared to be of type
"float", the the ANSI C standard definitely allows implementations to
evaluate the expression `a = b + c' by first loading the values of `b'
and `c' into registers, then widening them so that the values are repre-
sented in the same floating-point format as "double", then performing a
double-precision add, and then (finally) narrowing the result of the
addition back down to a single-precision "float" and storing that into
the variable `a'.
Believe me when I say that MANY existing ANSI C compilers take advantage
of this freedom (extended by the ANSI C standard) to perform floating-point
expression evaluations in whatever is the most convenient (or efficient)
format (as long as the format used is at least as wide as the format nor-
mally used for representing values of the apparent type of the expression
being evaluated).
I certainly hope that the C++ standard will extend this exact same permission
(explicitly) to C++ implementations. A failure to do so could have a rather
serious negative effect upon the usefulness of C++ for numerical applications
on a number of existing CPU types.
P.S. Note that (in ANSI C at least) the compiler's idea of the static data
type of an expression like `b + c' can be DIFFERENT from the actual machine
floating-point format used to *evaluate* the expression.
(Follow-ups to comp.std.c++ please.)
--
-- Ronald F. Guilmette ------------------------------------------------------
------ domain address: rfg@netcom.com ---------------------------------------
------ uucp address: ...!uunet!netcom.com!rfg -------------------------------