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

Chris Dew cmsdew at gmail.com
Tue Oct 25 04:05:46 PDT 2011


I would like to do some expensive BigInt computation at compile-time.

The basic arithmetic operations on BigInt seem to be impure, which
seems to make this impossible.

How can I work round this?

Thanks,

Chris.


gcd.d:

import std.stdio;
import std.bigint;

pure BigInt gcd(BigInt a, BigInt b) {  if (b == 0) return a;  return
gcd(b, a % b);}
int main() {  BigInt n = "10000000000";  writefln("%s", gcd(cast(BigInt)48, n));
  return 0;}
./gcd.d(6): Error: pure function 'gcd' cannot call impure function 'opBinary'
Failed: dmd  -v -o- './gcd.d' -I'.' >./gcd.d.deps


More information about the Digitalmars-d mailing list