how will two objects of type float perform when adding one to another

V

van6

code frag:
int main()
{

float fval1 = 1.0f;
float fval2 = 2.0f;
double dsum =0.0;

// here is the point ;
// Are fval1 and fval2 both promoted to type of double prior to adding
or perform the adding
// operation directly (the result of adding is still of type float ? )
dsum = fval1 + fval2;


}



This question may a little stupid . But different books give different
answers.
 
R

Ron Natalie

van6 said:
This question may a little stupid . But different books give different
answers.

No promotion occurs before the addition. The result of the addition is
still float. The conversion from float to double occurs when stored.

The reason why you may see difference of opinions is that C USED to
work differently. Any floating point operation was "logically"
expanded to double in the 1990 version of the C standard. C++ and
the 1999 C rewrote the promotion rules that don't widen floats to
doubles unless the other operand is a double.
 
V

van6

Ron said:
No promotion occurs before the addition. The result of the addition is
still float. The conversion from float to double occurs when stored.

The reason why you may see difference of opinions is that C USED to
work differently. Any floating point operation was "logically"
expanded to double in the 1990 version of the C standard. C++ and
the 1999 C rewrote the promotion rules that don't widen floats to
doubles unless the other operand is a double.

Thank you very much . Actually,I am puzzled by this question for
a long time. Now, you gave the exact answer.
 
J

Jack Klein

No promotion occurs before the addition. The result of the addition is
still float. The conversion from float to double occurs when stored.

The reason why you may see difference of opinions is that C USED to
work differently.

So far, so good.
Any floating point operation was "logically"
expanded to double in the 1990 version of the C standard.

Nooooooooooo! ANSI C 89 and ISO C 90 both eliminated the automatic
conversion of floats to doubles in operations and as arguments in
calls to appropriately prototyped non-variadic functions.
C++ and
the 1999 C rewrote the promotion rules that don't widen floats to
doubles unless the other operand is a double.

Nope, you're thinking of pre standard K&R 1. No ANSI or ISO standard
for either C or C++ contained the widening, with the exception of
floats passed to the ... of a variadic function. And that widening
still occurs in both languages today.

In C, the widening still occurs when passing a float to a function
without a prototype in scope (which even C99 allows, although a
declaration specifying the return type is required). But that doesn't
apply to C++ at all, since all legal C++ function declarations are
what C calls prototypes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top