473,804 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1619
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.ne t>
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.l earn.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
2593
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 is the root of the class hierarchy. Every class has Object as
16
2993
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, string, use namespace std) template <typename T> struct something { T x; operator T();
4
1634
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 with engine capacity greater than 1200cc - Cars with engine capacity smaller than or equal to 1200cc The annual road tax for a truck is $3000. The annual road tax for a car with engine capacity greater than 1200cc is $300 and for a car with
14
14320
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
5876
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 decimal, and the local variables I create to store them in is float. Fine. When I iterate through a datarow, I am assigning each cell to a variable. At least, I want to. I always get the message cannot implicitly convert type 'object' to type...
5
2693
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 that's come before. To take an extreme example, suppose you have been writing Visual Basic applications for years now. If you're like many developers, you will have built up a substantial inventory of code in that time. And if you've been following...
33
2337
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 extraordinary or strange". (2) By "willy-nilly", I mean something along the lines of "haphazardly", but without any sense of recklessness (i.e. gleefully doing something without expecting any sort of negative effect, even though there may in fact be a...
3
2886
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 modified the example code slightly by adding a line of code that says a = c.iterator().next();
9
2100
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 anyway, but here's the deal: I was learning about template classes, and how to transfer data between two objects of the same class with pointers involved. I was shown how to make the code "safe" by adding a copy constructor, an overloaded...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.