Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# +-*/ evaluation

Posted on 2003-11-12
Medium Priority
416 Views
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
Question by:G00fy

LVL 5

Expert Comment

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

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

efn earned 200 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

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
###### Suggested Courses
Course of the Month6 days, 5 hours left to enroll