Christopher Coale <chris95...@...> wrote: > peternilsson42 wrote: > > Christopher Coale <chris95219@> wrote: > > > Paul Herring wrote: > > > > billcu34<billcu1@> <mailto:billcu1%40suddenlink.net>> wrote: > > > > > > double Sma(double *ds, int size) > > > > > > { > > > > > > double res = 0; > > > > > > for (int i = 0; i < size; i++) > > > > > > res += ds[i]; > > > > > > return ds / (double)size; > > > > > > } ... > > > > It should in fact read > > > > return res/size; ... > > > Yeah, my mistake. I didn't bother testing - I just typed it > > > in the e-mail quickly. > > > > > > Casting to a double is to avoid compiler warnings > > > > A bad practice. You silenced the warning but completely > > ignored why it was issued. So instead of fixing the > > problem, you white-washed it. > > > > > (which will just make that a cast anyways). > > > > Not sure what you're saying here. Without the cast, there > > is an implicit conversion of size from int to double. A cast > > is an explicit conversion, but a conversion is not a cast. ... > Er... my whole point was that 1) I didn't try compiling,
Perhaps you should. Bill gets confused enough with accurate examples, let alone inaccurate ones... ;) > I just typed the code in the e-mail really quick. I've taken to copying terminal sessions to reduce posting silly mistakes. IMO code speaks louder than words. So it's more important to get the code right than it is to clean up spelling mistakes. > 2) It really doesn't matter that I casted to a double, > why would it? Because it unnecessarily adds to the maintenance cost. > The difference between "implicit casting" and conversion > is a very, very fine line. Casts are syntactical; conversions are semantic. There's no such thing as implicit casting. > So, now, I don't get what you are saying. Are you saying > that I shouldn't cast the int to a double? Generally no, if there's an implicit conversion. When I see casts, I get suspicious. [Especially when I use them. ;-] > If so, why not? Because it's redundant and subtracts rather than adds. > ... > I am casting the int to a double so that the division > is done correctly with an expected result Removing the cast in this case won't change the expected result. What do you expect from a division if not a quotient? Integral promotion, usual arithmetic conversions and default argument promotions are as fundamental to the language as functions, declarations and statements. Their implicit nature make them harder to grasp in casual inspections. Casts have a nasty habit of introducting more errors than they fix. For me, they're 10 times more deceptive. > (as well as indirectly get rid of a compiler warning Perhaps your warnings are set too high. You can't and shouldn't code to remove all warnings. Different implementations can and will issue warnings for whatever reason they like. That you've started to preempt compiler warnings is a (bad) sign of 'conditioning.' I'm not averse to reducing warnings myself, but I'd rather have correct code with a lot of information warnings than incorrect code with none. [Incidentally, my pet peeve is warnings about comparing signed and unsigned types. It usually occurs when I do a pointer subtraction, which is why I would have preferred pointer subtraction to result in an unsigned size_t, rather than a signed ptrdiff_t.] > - which just so happens to show up for the same reason > I am casting to a double). > > I'll go crawl back under my rock now... Please don't. These sorts of discussions have broader application than 'how do I do ODBC' discussions. Of course, to most they don't seem quite so 'sexy'. ;) -- Peter
