By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,300 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

Problem! (Easy for most of you i'm sure)

P: n/a
I'm a C++ newbie, just been starting out today... I really can't get my head
around this...
*****************

int main()
{
int one = 1;
int number = 43;

double result = one / number;
double result2 = static_cast<double>(one / number);

cout << result << endl << result2;

return 0;
}

*****************

OUTPUT:

0
0

*****************
I dont understand!!! :-( please help

thanks!
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Eurig Jones" <eu***@one47.com> wrote in message news:bo*******************@news.demon.co.uk...
int one = 1;
int number = 43;
These are two integers.

double result = one / number;
Ignore the stuff to the left of the = sign for now. The expression
one/number
has value zero, becaue integer divide throws away the fractional values.
1/43 is 0, 42/43 is 0, 43/43 is 1.

So in effect you have:
double result = 0;

The int zero is converted to double and initializers result. You have a double value of
zero.
double result2 = static_cast<double>(one / number);


The same thing happens here. The sub expression one/number is still 0.
double result2 = static_cast<double>(0);
The static cast converts it to double, but it's still zero.

If you want to do a floating point divide, you must convert at least one of the
operands to a floating point type (if you convert only one, the compiler will
automatically convert the other to match).

double result = static_cast<double>(one) / number;

for example. This convets the 1 to a double. The compiler now does the divide,
converting number to double to match (there are a list of "usual artithmatic conversions"
that gets applied to operators when the types of the operands don't match, generally
the least precise of the two is converted to the more precise).

The result of this expression is the 1.0/43 or about .023...
Jul 19 '05 #2

P: n/a
hi!
double result = one / number;
double result2 = static_cast<double>(one / number);


if i'm not completely wrong, the divisions are calculated using integer
division. both operands are integer so integer div. is used (and in the
second case, the result!! is casted to double....)
you must convert (at least) one operand to the precision you want the
calculation to take place in, the the second operand is implicitly converted
to the greater type.
(though i think it's better and better style to cast both to double)

double result = static_cast<double>(one) / static_cast<double>(number);

should do the trick.

regards,
sev
Jul 19 '05 #3

P: n/a
> int main()
{
int one = 1;
int number = 43;

double result = one / number;
double result2 = static_cast<double>(one / number);

cout << result << endl << result2;

return 0;
}

*****************

OUTPUT:

0
0

*****************


The output is correct! The two variables "one" and "number" are integer, so
the result of "one/number" ist
integer too (0). And this you are converting to double which ist remaining
0.

Try the following code:

int main()
{
int one = 1;
int number = 43;

double result = (double)one / (double)number;

cout << result << endl << result2;

return 0;
}

If one of the operands is of the type "double", the operation is a floating
point operation.

Ralf

http://www.cplusplus-kurse.de


Jul 19 '05 #4

P: n/a
> I'm a C++ newbie, just been starting out today... I really can't get my
head
around this... I dont understand!!! :-( please help

thanks!


Also look up the "usual rules of arithmetic conversion" (in addition to what
others have now said).
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.