cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/07/23:55:42

Message-Id: <3.0.6.32.19990207235345.008e59e0@pop.netaddress.com>
X-Sender: pderbysh AT pop DOT netaddress DOT com
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Sun, 07 Feb 1999 23:53:45 -0500
To: djgpp AT delorie DOT com
From: Paul Derbyshire <pderbysh AT usa DOT net>
Subject: Re: recursive factorial
In-Reply-To: <002301be5318$9d1b86e0$461b2bc8@luis>
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019