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

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


You're welcome :-)
CTFE is flexible indeed. The only major thing it can't do is classes
and external functions (those without their bodies visible to the
current compilation).
Everything else is CTFE-able.

On Tue, Oct 25, 2011 at 3:56 PM, Chris Dew <cmsdew at gmail.com> wrote:
> I didn't know that, CTFE is really flexible.
>
> I've filed the bug http://d.puremagic.com/issues/show_bug.cgi?id=6850
> even though it won't hinder me.
>
> Thanks,
>
> Chris.
>
> On 25 October 2011 12:25, Gor Gyolchanyan <gor.f.gyolchanyan at gmail.com> wrote:
>> 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