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

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Tue Oct 25 04:25:11 PDT 2011


CTFE does not require purity. Your error is not CTFE-specific.
It's a bug in BigInt's implementation, because the opBinary(string op
: "%") is not marked pure as it should be.

On Tue, Oct 25, 2011 at 3:05 PM, Chris Dew <cmsdew at gmail.com> wrote:
> 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