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

E

Eurig Jones

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!
 
R

Ron Natalie

Eurig Jones said:
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...
 
S

Severin Ecker

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
 
R

Ralf

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
 
J

John Brown

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top