f was intended to give the fibonacci number, but was not written
correctly by me. So, f( 10 ) was supposed to be 55, but indeed is 89.
FYI:
// fib.c
#include <stdio.h>
#include <chaos/preprocessor.h>
#define FIB(n) \
CHAOS_PP_ARBITRARY_DEMOTE( \
CHAOS_PP_VARIADIC_ELEM( \
2, \
CHAOS_PP_EXPR(CHAOS_PP_WHILE_X( \
20, \
FIB_P, FIB_O, \
CHAOS_PP_ARBITRARY_DEC( \
CHAOS_PP_IIF(CHAOS_PP_IS_VARIADIC(n))( \
n, \
CHAOS_PP_ARBITRARY_PROMOTE(n) \
) \
), \
(0), (1) \
)) \
) \
) \
/**/
#define FIB_P(s, n, a, b) \
CHAOS_PP_ARBITRARY_BOOL(n) \
/**/
#define FIB_O(s, n, a, b) \
CHAOS_PP_ARBITRARY_DEC(n), b, CHAOS_PP_ARBITRARY_ADD(a, b) \
/**/
int main(void) {
printf(CHAOS_PP_STRINGIZE(FIB(500)) "\n");
return 0;
}
The program above computes the 500th Fibonacci number with the
preprocessor. It takes about 20 seconds to compile with GCC on the Linux
VM that I'm running on a mid-range machine.
I did the above on Linux, but will work under Cygwin with MinGW (either
the Cygwin-dependent version or the standalone).
$ gcc -std=c99 -I $CHAOS_ROOT -o fib fib.c
$ ./fib
139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125
The input to the macro FIB is an integer literal (without suffixes) in
the range 1-512. Values larger than that have to be input in Chaos'
arbitrary precision format such as (5)(1)(3) for 513. The output is a
single pp-number token. For FIB(500), as in the above, that pp-number is
far greater than is representable in 64-bit value so it has to be
stringized before the underlying compiler gets a hold of it.
Note, BTW, that I'm not suggesting that computing Fibonacci numbers with
the preprocessor is advisable. I'm merely mentioning that everything
mentioned in this thread is already possible with a standard C or C++
preprocessor without iteratively invoking the preprocessor in a LaTeX-
like style.
-----
Chaos has to be pulled from the repository and then put wherever you
want....
$ cd <whereever>
$ cvs -z3 -d
server:
[email protected]:/cvsroot/chaos-pp co -P chaos-pp
$ export CHAOS_ROOT=<whereever>
Chaos is a preprocessor metaprogramming library originally derived from
the Boost Preprocessor Library. While Boost is a collection of C++
libraries (rather than C), the Boost Preprocessor Library is C/C++.
Chaos is a vastly more powerful derivative that doesn't mess around with
workarounds for broken preprocessors. Thus, it takes a very good
preprocessor to handle it (such as GCC's, EDG's, and a few others).
Regards,
Paul Mensonides