Does this make any sense?

T

tings

int a, b, c, d;
....
a = b*((double)c/d);

Can someone clarify what's happening step by step in the statement?

Thanks for your help!
 
A

alex

tings said:
int a, b, c, d;
...
a = b*((double)c/d);

Can someone clarify what's happening step by step in the statement?

Thanks for your help!

it means variable "a" will store the result of "b" mutliplied by whatever
the result of "c"
divided by "d" is.

b,c,d will contain unknown values unless they're initialised somewhere, so
with the piece of code
you've shown, the result of "a" is unknown
 
J

Joona I Palaste

tings said:
int a, b, c, d;
...
a = b*((double)c/d);
Can someone clarify what's happening step by step in the statement?
Thanks for your help!

Let's take it step by step, shall we?
It assigns something to a.
This something is a product of two multiplicands. The first is b.
The second is the quotient of two dividends.
The first dividend is (double)c, which is c cast to a double.
The second dividend is d.

In other words, the second multiplicand is c divided by d, only that c
is cast to a double to prevent the "cutting-off" that occurs when an
int is dived by an int.

In yet other words, it assigns to a a new value, this value being b
multiplied by the quotient of c and d. The (double) bit is only there
to ensure a proper mathematical division instead of the "cutting-off"
integer division C would otherwise perform.
 
D

Derrick Coetzee

tings said:
int a, b, c, d;
...
a = b*((double)c/d);

Can someone clarify what's happening step by step in the statement?

The same thing that happens in this sequence of statements:

double c_dbl = (double)c;
double d_dbl = (double)d;
double quotient = c_dbl/d_dbl;
double b_dbl = (double)b;
double product = b_dbl*quotient;
a = (int)product;

Although there's only one cast in the original statement, all of the
above casts happen implicitly. The jist of it mathematically is to put
truncate(bc/d) into a. Provided the double type on your platform has
more bits of precision than int (this is true on modern Intel
platforms), it should do this successfully for all input values where d
!= 0. The statement "a = b*c/d" would do the same thing more efficiently
in many cases, but risks overflow for large values of b and c.
 
I

infobahn

Derrick Coetzee wrote:

Although there's only one cast in the original statement, all of the
above casts happen implicitly.

You run the risk of confusing newbies.

A cast is an *explicit* conversion. Casts cannot happen implicitly.
 
J

Jack Klein

Let's take it step by step, shall we?
It assigns something to a.
This something is a product of two multiplicands. The first is b.
The second is the quotient of two dividends.

Just terminology, but there are not two dividends in a division.
There is one dividend and one divisor. This is different from the
situation for multiplication, because order does not matter for
multiplication but it does for division.

a * b == b * a

....but:

a/b != b/a
The first dividend is (double)c, which is c cast to a double.
The second dividend is d.

So actually, the dividend is (double)c, and the divisor is d.
 
R

raj

a = b*( (double)c/d);
this is a common way to get out of round off error which occurs when an
int is divided by an int . for eg if c=10 and d =3
c/d is 3 not 3.3333 that is the reason the variable c is typecasted to
double , now the entire expression returns a double .. which when
mutliplied with an int will again give u a double ... then finally
again it is type casted to int.
what i think is it is of waste to convert variable c into double if the
finally u are again typecasting the result into int...
i think this will same as the your equation u wrote if a,b,c,d are all
ints
a= (b*c)/d
eg if b=3 , c=10 and d=3
then a=10
where as your equation will give a=9 ..
go thru the books on numerical methods in computer science by any
author for further classification
 
J

Joona I Palaste

raj said:
a = b*( (double)c/d);
this is a common way to get out of round off error which occurs when an
int is divided by an int . for eg if c=10 and d =3
c/d is 3 not 3.3333 that is the reason the variable c is typecasted to
double , now the entire expression returns a double .. which when
mutliplied with an int will again give u a double ... then finally
again it is type casted to int.
what i think is it is of waste to convert variable c into double if the
finally u are again typecasting the result into int...
i think this will same as the your equation u wrote if a,b,c,d are all
ints
a= (b*c)/d
eg if b=3 , c=10 and d=3
then a=10
where as your equation will give a=9 ..
go thru the books on numerical methods in computer science by any
author for further classification

Plz don't sp33k like a h4x0r d00d in comp.lang.c.
It is not the same at all. Consider for example the case where b==2,
c==1 and d==2.
If you use b*((double)c/d), then (double)c/d equals 0.5, and b*0.5
gives you 1.
If you use b*(c/d), then c/d equals 0, and b*0 gives you 0.
Now I don't know about you, but I consider 1 and 0 different numbers.
 
R

raj

If you use b*(c/d), then c/d equals 0, and b*0 gives you 0.
Now I don't know about you, but I consider 1 and 0 different numbers.
sir please watch that i metioned (b*c)/d not b*(c/d) ...as division and
mutliplication are commutative operators . in normal arthmetic (b*c)/d
is same as b*(c/d) . which is not same in computers that's y i changed
it.
 
M

Michael Mair

raj said:
sir please watch that i metioned (b*c)/d not b*(c/d) ...as division and
mutliplication are commutative operators . in normal arthmetic (b*c)/d
is same as b*(c/d) . which is not same in computers that's y i changed
it.

Right. However, b*c may overflow even though b*((double)c/d) does not.

Please do not snip attributions; even if Joona I Palaste was wrong,
it is still _his_ mistake.


-Michael
 
L

Lawrence Kirby

a = b*( (double)c/d);
this is a common way to get out of round off error which occurs when an
int is divided by an int . for eg if c=10 and d =3
c/d is 3 not 3.3333 that is the reason the variable c is typecasted to
double , now the entire expression returns a double .. which when
mutliplied with an int will again give u a double ... then finally
again it is type casted to int.

Don't confuse casts with conversions. Casts are explicit operators in the
source code e.g. (double) above is a cast operator. Casts can cause
conversions (and typically do). However not all conversions involve casts
e.g. the conversion of the resul form double to int before it is written
to a is an example.
what i think is it is of waste to convert variable c into double if the
finally u are again typecasting the result into int... i think this will
same as the your equation u wrote if a,b,c,d are all ints a= (b*c)/d eg
if b=3 , c=10 and d=3
then a=10

That's possibly true except:

1. if b*c is greater than can be represented by an int you get undefined
behaviour. Casting to long or in C99 long long may provide adequate
range to avoid this.

2. Given your example values a = b*((double)c/d) gives a= 3*((double)10/3
or a = 3 * 3.33... Now 3.33... isn't representable exactly in
decimal or binary. So 3 * 3.33... may evaluate to 10, something
slightly larger than 10, or something slightly smaller than 10. If it
is slightly smaller then converting it to int will produce 9 as a
result, which is probably not what was wanted. Even if it happens to
work in this case there will be others where it fails.
a = (double)b*c / d would be better in this respect.

Moral: don't use floating point for integer operations unless you really
know what you are doing. Then you have to ask yourself how you could tell
if you know what you are doing. :)

Lawrence
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top