Mail Archives: djgpp/1999/02/07/23:55:42
At 01:07 AM 2/8/99 -0300, you wrote:
> But it doesn't work. It does the rigth number of calls to rcs_factorial
>but fails at doing the first multiplication. Thanks. Luis.
"We've lost RCS thrusters! We're going down!"
The problem line:
> return passed * rcs_factorial(--passed);
To do the multiply, the result of rcs_factorial(--passed) must be computed;
to do that, --passed must execute, changing the passed in the multiply as
well.
To make this code work, write the following, far more sensible line:
return passed * rcs_factorial(passed-1);
There's no real need to mutate passed anyways. The compiler will generate
correct code in this instance, and will optimize as well as it can anyways.
Basic rule of C/C++: Avoid using ++ or -- in complex mathematical
expressions, and use +=, *=, etc. only as a one-off assignment:
foo += expression;
Otherwise the compiler will surprise you.
--
.*. "Clouds are not spheres, mountains are not cones, coastlines are not
-() < circles, and bark is not smooth, nor does lightning travel in a
`*' straight line." -------------------------------------------------
-- B. Mandelbrot |http://surf.to/pgd.net
_____________________ ____|________ Paul Derbyshire pderbysh AT usa DOT net
Programmer & Humanist|ICQ: 10423848|
- Raw text -