public Rational Root(BigInteger r) { /* test for overflow */ if (r.CompareTo(MaxInt32) == 1) { throw new FormatException("Root " + r + " too large."); } if (r.CompareTo(MinInt32) == -1) { throw new FormatException("Root " + r + " too small."); } int rthroot = r.ToInt32(); /* cannot pull root of a negative value with even-valued root */ if (CompareTo(Zero) == -1 && (rthroot % 2) == 0) { throw new FormatException("Negative basis " + ToString() + " with odd root " + r); } /* extract a sign such that we calculate |n|^(1/r), still r carrying any sign */ bool flipsign = (CompareTo(Zero) == -1 && (rthroot % 2) != 0); /* delegate the main work to ifactor#root() */ var num = new IFactor(Numerator.Abs()); var deno = new IFactor(Denominator); Rational resul = num.Root(rthroot).Divide(deno.Root(rthroot)); return(flipsign ? resul.Negate() : resul); }
public byte ToByte() { if (!IsInteger) { throw new InvalidCastException(); } var i = Numerator.Abs().ToInt32(); if (i > Byte.MaxValue || i < Byte.MinValue) { throw new InvalidCastException(); } return((byte)i); }
public Rational Abs() { return(new Rational(Numerator.Abs(), Denominator.Abs())); }
public Rational Reciprocal() => new Rational() { Numerator = (Integer)Denominator * Numerator.Sign(), Denominator = Numerator.Abs() };