Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

+-*/ evaluation

Posted on 2003-11-12
3
Medium Priority
?
413 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
[X]
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
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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

730 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