Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Does this make any sense?

 
 
Lawrence Kirby
Guest
Posts: n/a
 
      01-10-2005
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
 
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