Preliminary submission - std.rational and std.typelist

Arlen arlen.ng at gmx.com
Sun Oct 7 21:44:46 PDT 2012


On Sat, Oct 6, 2012 at 1:32 PM, Dmitry Olshansky <dmitry.olsh at gmail.com> wrote:
>
> Cool, does it work with BigInt?
>

No it doesn't work with BigInt, but I did look into it today briefly.
There are issues with BigInt that I'm not sure what to do about:

1.  To convert a BigInt to floating-point one needs to convert it to
the built-in integer types first.  If you go beyond the limits of the
built-in types (long), then what's the point?  You might as well be
using int or long.

2.  The functions in std.math don't support BigInt, and most likely
never will.  Even if they did, you would still need multi-precision
floating point to store the irrational numbers.  If you decided to use
the built-in types, then again what's the point?  You might as well go
with int or long.

> Also I think there is better version of toString that has signature:
> void toString(scope void delegate(const(char)[]) sink)
>
> this toString just use functions like formattedWrite to write chars to sink.
> It thus totally avoids overhead of allocating strings.
>
> Your current code may have some bad impact on performance:
> return "(" ~ to!string(res.numerator) ~ "/" ~ to!string(res.denominator) ~
> ")";
>
> Allocates 4 times. ~ operator is convenient shortcut to get job done but
> it's unsuitable for building strings and formatted output.
>
>

Thanks, I'll fix it.


More information about the Digitalmars-d mailing list