By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Sep 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
van6 wrote:
>

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.
Sep 14 '06 #2

P: n/a

Ron Natalie wrote:
van6 wrote:


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.
Thank you very much . Actually,I am puzzled by this question for
a long time. Now, you gave the exact answer.

Sep 15 '06 #3

P: n/a
On Thu, 14 Sep 2006 12:13:43 -0400, Ron Natalie <ro*@spamcop.net>
wrote in comp.lang.c++:
van6 wrote:


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.
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.