Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Does this make any sense? (http://www.velocityreviews.com/forums/t436449-does-this-make-any-sense.html)

tings 01-09-2005 07:12 PM

Does this make any sense?
 
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!



alex 01-09-2005 07:23 PM

Re: Does this make any sense?
 

"tings" <tings668@hotmail.com> wrote in message
news:wmfEd.90187$uM5.25380@bgtnsc05-news.ops.worldnet.att.net...
> 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



Joona I Palaste 01-09-2005 07:57 PM

Re: Does this make any sense?
 
tings <tings668@hotmail.com> 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.
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 (palaste@cc.helsinki.fi) ------------- 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

Derrick Coetzee 01-09-2005 08:26 PM

Re: Does this make any sense?
 
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.

infobahn 01-09-2005 09:36 PM

Re: Does this make any sense?
 
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.

Jack Klein 01-10-2005 04:38 AM

Re: Does this make any sense?
 
On 9 Jan 2005 19:57:06 GMT, Joona I Palaste <palaste@cc.helsinki.fi>
wrote in comp.lang.c:

> tings <tings668@hotmail.com> 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/~a...FAQ-acllc.html

raj 01-10-2005 05:53 AM

Re: Does this make any sense?
 
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


Joona I Palaste 01-10-2005 06:04 AM

Re: Does this make any sense?
 
raj <raj_guy_hyd@yahoo.com> 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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am lying."
- Anon

raj 01-10-2005 08:48 AM

Re: Does this make any sense?
 
>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.


Michael Mair 01-10-2005 09:16 AM

Re: Does this make any sense?
 


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.



All times are GMT. The time now is 09:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.