[LCP]Rounding questions

Robert Gehring zoroaster at snafu.de
Sat Sep 7 17:31:43 UTC 2002


wrote Greg Black:
> Chuck Martin wrote:
> | On Fri, Sep 06, 2002 at 01:29:54AM +1000, Greg Black wrote:
> | > Chuck Martin wrote:
> | > | The %f takes a double.  If I change "round((double) 390.0)" to
> | > | "(double) 390.0", the output is "390.000000" and a newline.
> | >
> | > If you left out all these pointless casts, it would be easier to
> | > believe that you knew what you were doing.
> |
> | I agree they are pointless, and my original tests did not include
> | them.  I started out with "round(390)", and only added the casts
> | to make sure everything was being interpreted as doubles.
>
> But what you really show with the casts is that you don't know
> what's happening and are trying to work around it.  This is just
> never a good idea.  Even if you make such a change to test an
> idea, you should immediately establish what is missing and go
> and find the real problem.
>
[...]
> |
> | On the contrary, rather than obfuscate the code, it "needlessly"
> | clarifies it.  Superfluous castings are merely ignored by the
> | compiler, but make it clear exactly what arguments are being
> | passed and what is expected to be returned.
>
> No, superfluous casts merely show that the programmer is unsure
> of what s/he is doing.  Casts are almost never required and are
> always a flag that says "I don't what's happening here".
>

IMHO: Casting makes a lot of sense at least when you try to make your code 
reliable trough checking it with lint. lint likes casting very much, as you 
can easily try out. And there is one more point: The day may come when 
someone other has to look at your code ... Casting is one way of documenting 
your code for other people. It explicitly says what you meant to do.

Robert





More information about the linuxCprogramming mailing list