Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Does this make any sense?

Reply
Thread Tools

Does this make any sense?

 
 
tings
Guest
Posts: n/a
 
      01-09-2005
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!


 
Reply With Quote
 
 
 
 
alex
Guest
Posts: n/a
 
      01-09-2005

"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?
>
> 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


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      01-09-2005
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.
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
 
Reply With Quote
 
Derrick Coetzee
Guest
Posts: n/a
 
      01-09-2005
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.
 
Reply With Quote
 
infobahn
Guest
Posts: n/a
 
      01-09-2005
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.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-10-2005
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/~a...FAQ-acllc.html
 
Reply With Quote
 
raj
Guest
Posts: n/a
 
      01-10-2005
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

 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      01-10-2005
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
 
Reply With Quote
 
raj
Guest
Posts: n/a
 
      01-10-2005
>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.

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-10-2005


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a FREE Call Any Where Any Time Neelam007 Java 1 04-05-2007 06:52 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
How does one make use of sub files to make DVD? Bun Mui DVD Video 0 05-21-2006 03:24 PM
How does one make use of sub files to make DVD? Bun Mui Computer Support 0 05-21-2006 03:09 PM
Can any body provide me teh information how to make teh resume database online or any help related to it mit ASP .Net 0 07-19-2005 08:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57