# Does this make any sense?

Discussion in 'C Programming' started by tings, Jan 9, 2005.

1. ### tingsGuest

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

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

tings, Jan 9, 2005

2. ### alexGuest

"tings" <> wrote in message
news:wmfEd.90187\$...
> int a, b, c, d;
> ...
> a = b*((double)c/d);
>
> Can someone clarify what's happening step by step in the statement?
>
>
>

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

alex, Jan 9, 2005

3. ### Joona I PalasteGuest

tings <> scribbled the following:
> int a, b, c, d;
> ...
> a = b*((double)c/d);

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

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.

--
/-- Joona Palaste () ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery

Joona I Palaste, Jan 9, 2005
4. ### Derrick CoetzeeGuest

tings wrote:
> 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.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.

Derrick Coetzee, Jan 9, 2005
5. ### infobahnGuest

Derrick Coetzee wrote:

<snip>

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

infobahn, Jan 9, 2005
6. ### Jack KleinGuest

On 9 Jan 2005 19:57:06 GMT, Joona I Palaste <>
wrote in comp.lang.c:

> tings <> scribbled the following:
> > 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.

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.

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jack Klein, Jan 10, 2005
7. ### rajGuest

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

raj, Jan 10, 2005
8. ### Joona I PalasteGuest

raj <> scribbled the following:
> 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.

--
/-- Joona Palaste () ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am lying."
- Anon

Joona I Palaste, Jan 10, 2005
9. ### rajGuest

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

raj, Jan 10, 2005
10. ### Michael MairGuest

raj wrote:
>>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.

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
--
E-Mail: Mine is a gmx dot de address.

Michael Mair, Jan 10, 2005
11. ### Lawrence KirbyGuest

On Sun, 09 Jan 2005 21:53:14 -0800, raj wrote:

> 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

Lawrence Kirby, Jan 10, 2005