How can I write compile-time (pure) BigInt computations?

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Tue Oct 25 05:37:59 PDT 2011


That's because N is not a compile-time value. Initializing global
variable is allowed only with compile-time values, but the variable
themselves are run-time values.
This could probably be fixed, since the initial values of global
variables are always known.

Anyway, if the value of N (and probable X too) is not subject to
modification, you should make then either immutable or, better yet,
enum.
This way they WILL be compile-time values and you won't get that error.

On Tue, Oct 25, 2011 at 4:31 PM, Chris Dew <cmsdew at gmail.com> wrote:
> Stuck again, looks like BigInt doesn't work with CTFE for other reasons...
>
> Is there a way to create BigInts at compile time (I've also tried from
> a string)?  Or is it back to C code generation in Python :-(
>
> Thanks anyway,
>
> Chris.
>
>
> ./gcd.d(11): Error: static variable N cannot be read at compile time
> ./gcd.d(11): Error: cannot evaluate gcd((BigInt __ctmp1335;
>  , __ctmp1335).this(48),N) at compile time
> Failed: dmd  -v -o- './gcd.d' -I'.' >./gcd.d.deps
>
>
>
> import std.stdio;
> import std.bigint;
>
> T gcd(T)(T a, T b) {
>  if (b == 0) return a;
>  return gcd(b, a % b);
> }
>
>
> BigInt N = cast(BigInt)1000000000L;
> BigInt X = gcd(cast(BigInt)48, N);
>
> int main() {
>  writefln("%s", X);
>  return 0;
> }
>


More information about the Digitalmars-d mailing list