Preliminary submission - std.rational and std.typelist

bearophile bearophileHUGS at lycos.com
Mon Feb 11 11:05:07 PST 2013


Arlen:

> https://github.com/Arlen/phobos/blob/std_rational/std/rational.d
> docs: http://arlen.github.com/phobos/std_rational.html

I'd like a good Rational in Phobos.

 From your code:

private template isSignedIntegral(T)
{
     enum isSignedIntegral = isIntegral!T && isSigned!T;
}
...
struct Rational(T) if (isSignedIntegral!T)
{
     static if (is(T == immutable) || is(T == const))
     {
         private enum bool mutableT = false;
     }
     else
     {
         private enum bool mutableT = true;
     }

     static if (mutableT)
     {
         private T numerator;
         private T denominator = 1;
         private bool dirty;
     }
     else
     {
         private T numerator, denominator;
         @disable this();
     }
...


A Rational should accept T as BigInt too.

And isn't it better for the Rational constructor to simplify the 
arguments (calling an optimized GCD)? See this implementation 
(that doesn't use an optimized GCD):

http://rosettacode.org/wiki/Arithmetic/Rational#D

Bye,
bearophile


More information about the Digitalmars-d mailing list