[phobos] BigInt from Tango

Don Clugston dclugston at googlemail.com
Fri Mar 19 03:57:17 PDT 2010


On 19 March 2010 07:20, Andrei Alexandrescu <andrei at erdani.com> wrote:
> On 03/18/2010 10:46 PM, Don Clugston wrote:
>>
>> I've converted my BigInt module from Tango to Phobos2. It's a great
>> test of the new operator overloading!
>> After 2.042 comes out, I'd like to add it to Phobos.
>
> Awesome! How do the operators hold?

They're looking pretty good. The code size for the operator
overloading has so far shrunk to less than half what it used to be.
More importantly, it's shown up some bugs in the existing code. Here's
a sample:

    // BigInt op BigInt
    BigInt opBinary(string op, T)(T y)  if ( is(T: BigInt) )
    {
        BigInt x = this;
        return x.opOpAssign!( op ~ "=" )(y);
    }

    // Commutative operators
    BigInt opBinaryRight(string op, T)(T y)  if ( (op == "+" || op ==
"*" ) && ! is(T: BigInt) )
    {
        return opBinary!(op)(y);
    }


>
>> A couple of issues:
>> (1) We need somewhere in Phobos for implementation code (of which
>> there is a considerable amount for BigInt).
>>
>> I propose std.internal.math.XXX; for all math related modules. This
>> would keep everything tidy and hidden.
>> None of this will be user-visible.
>
> Sounds good.
>
>> (2)  Syntax for conversion to string is undecided. Strawman:
>>
>>  void toString(void delegate(const(char)[]) sink, string format);
>>
>> where format is currently limited to:
>>
>> d    for decimal
>> x    for lower case hex
>> X    for upper case hex
>
> s or null for "whatever" for conformity.
>
>> but will be extended in the future. If format is "", defaults to decimal.
>> Sink takes a const(char)[] so that BigInt can keep reusing the same
>> buffer.
>>
>> Eventually, whatever is chosen will require some changes to
>> std.format, but I'd just like to get a reasonable first cut for the
>> initial release.
>
> That's fine; we need to look again at std.format anyway.
>
> Speaking of which - do we have good code for parsing and writing floating
> point numbers? I need it for unformat().

Not yet. To do floating point accurately, you need some BigInt functions...
Someone (Fawzi I think) had made some good progress on it.


More information about the phobos mailing list