Solved

+-*/ evaluation

Posted on 2003-11-12
3
373 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now