Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

+-*/ evaluation

Posted on 2003-11-12
3
391 Views
Last Modified: 2010-04-01
If I have:

// code
int i = 1;
float f = 1.2;
double d = 2.5;
long l = 3L;
\\ code

How will the following functions be evaluated?

// code
i * f;
d * f;
l * i;
........
\\ code

Will the variables I'm using be converted to the lowest bit-value and then calculated? Or will they be converted to the highest bit-value and the result casted to the lowest bitvalue? Or... ??? (with bitvalue I mean the amount of bits of that variable)
0
Comment
Question by:G00fy
3 Comments
 
LVL 5

Expert Comment

by:drnick
ID: 9737483
that depends mainly on the compiler you use and the left-side-operats, which you omitted.
if you have f = i *f for example, the i could be converted to a float,
otherwise, having i = f * i, the f would become an int.
also, most compilers will throw warnings telling you what they do at this position or how they expect you to explicitely typecast.
0
 

Author Comment

by:G00fy
ID: 9737486
I'm using Visual C++... On linux I'm using GCC3...

it's better to static_cast<>() it then? That way I'm always sure it's what I want on any compiler?
0
 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 9738394
First, the compiler does the "usual unary conversions" on each operand.  That won't do anything with the types you listed, but if you had a char or short, it would be promoted to int.

Then it does the "usual binary conversions" to convert both operands to the same type.  If either one is a floating point type, both are converted to floating point.  Aside from that, if the two types have different widths, they are converted to the wider type.

So for i * f, i is converted to float; for d * f, f is converted to double; and for l * i, i is converted to long.

The result of the expression may be converted again if it is assigned to something else, but first the operands are converted as described above.  Also, the order of the operands does not matter.  Any compiler that conforms to the language standard should do it this way.

You only need to use static_cast if you need something other than the usual conversions.

--efn
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ to C# code conversion issue 4 111
How to convert MFC APP to Win32 APP. 19 93
C++ question 3 70
C++ help/ Toy problem 19 28
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question