Solved

+-*/ evaluation

Posted on 2003-11-12
3
387 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 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