473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
3 1600
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fred | last post by:
Are primitives objects? int i = 3; System.out.println(i.getClass()); doesn't compile. Get an error message "int can't be dereferenced" But yet the docs for class Object say: Class Object...
16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
4
by: emagutu | last post by:
Hi, I have been geiven the following Task. Class, inheritance, polymorphism, file handling, arrays or linked list. A C++ program is required to handle three types of objects: - Trucks - Cars...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
8
by: Steve Wasser | last post by:
I'm pulling SQL data into a dataset to be used to perform some math against. I asked this question earlier, but the answer someone gave me left me with further questions. The SQL data is stored as...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
33
by: Frederick Gotham | last post by:
(My dialect of English seems to puzzle people at times, so I'll first clarify a few terms which I use in the following post:) (1) By "domestic", I mean "ordinary, run-of-the-mill, not...
3
by: Antony Sequeira | last post by:
Hi I was trying to improve my understanding of generics. Reading the tutorial http://java.sun.com/docs/books/tutorial/extra/generics/methods.html I had a doubt and wanted to check it out. I...
9
by: Soneji | last post by:
I've been learning C++ for about 6 weeks now, and I've been able to figure out pretty much everything I have had a problem with. This time, I'm at a loss. I'm not learning from the greatest book...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.