473,378 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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!
Nov 14 '05 #1
10 1162

"tings" <ti******@hotmail.com> wrote in message
news:wm*******************@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
Nov 14 '05 #2
tings <ti******@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 (pa*****@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
Nov 14 '05 #3
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.
Nov 14 '05 #4
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.
Nov 14 '05 #5
On 9 Jan 2005 19:57:06 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote in comp.lang.c:
tings <ti******@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
Nov 14 '05 #6
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

Nov 14 '05 #7
raj <ra*********@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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am lying."
- Anon
Nov 14 '05 #8
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.

Nov 14 '05 #9


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.

Nov 14 '05 #10
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
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the...
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
0
by: mike | last post by:
regards: Does the following programming architecture make sense? http://www.wretch.cc/album/show.php?i=otp&b=1&f=1111993473&p=2 ...
10
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address,...
1
by: mikeotp | last post by:
Does the statement make sense? Meaning of “program” is to setup *.h(header檔) and to edit a main file to be a program exit. The main file includes all of the“*.h” files I edited before....
3
by: **Developer** | last post by:
I have a usercontrol that contains two pictureboxes One is on top of the other. The bottom one is the parent of the top one. The top one is transparent. I invalidate the top one often
3
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to...
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
2
by: puzzlecracker | last post by:
I have never seen this in practice and interested in its pros, or ever existential (another words, standardized) possibility? Thanks, Sasha
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.