Skip to content

Commit

Permalink
More refactoring. Separating Root and RationalRoot operations. Fixes …
Browse files Browse the repository at this point in the history
…for root of negative number.
  • Loading branch information
tompazourek committed Nov 11, 2017
1 parent 20a25d4 commit 5d100d2
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 107 deletions.
File renamed without changes.
46 changes: 23 additions & 23 deletions src/Rationals/Comparisons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,57 @@ public partial struct Rational :
{
public int CompareTo(object obj)
{
if (obj is sbyte)
if (obj is sbyte @sbyte)
{
return CompareTo((sbyte)obj);
return CompareTo(@sbyte);
}

if (obj is byte)
if (obj is byte b)
{
return CompareTo((byte)obj);
return CompareTo(b);
}

if (obj is short)
if (obj is short s)
{
return CompareTo((short)obj);
return CompareTo(s);
}

if (obj is ushort)
if (obj is ushort @ushort)
{
return CompareTo((ushort)obj);
return CompareTo(@ushort);
}

if (obj is int)
if (obj is int i)
{
return CompareTo((int)obj);
return CompareTo(i);
}

if (obj is uint)
if (obj is uint u)
{
return CompareTo((uint)obj);
return CompareTo(u);
}

if (obj is long)
if (obj is long l)
{
return CompareTo((long)obj);
return CompareTo(l);
}

if (obj is ulong)
if (obj is ulong @ulong)
{
return CompareTo((ulong)obj);
return CompareTo(@ulong);
}

if (obj is Rational)
if (obj is Rational rational)
{
return CompareTo((Rational)obj);
return CompareTo(rational);
}

if (obj is BigInteger)
if (obj is BigInteger integer)
{
return CompareTo((BigInteger)obj);
return CompareTo(integer);
}

return ReferenceEquals(obj, this) ? 0 : -1;
return -1;
}

public int CompareTo(BigInteger other)
Expand Down Expand Up @@ -194,8 +194,8 @@ public bool Equals(ushort other)

public override bool Equals(object other)
{
if (other is Rational)
return Equals((Rational)other);
if (other is Rational rational)
return Equals(rational);

if (other is short ||
other is ushort ||
Expand Down
3 changes: 1 addition & 2 deletions src/Rationals/ExplicitFloatingPointConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public static explicit operator decimal(Rational rational)
var previousScale = 0M;
while (numerator > 0)
{
BigInteger rem;
var divided = BigInteger.DivRem(numerator, denominator, out rem);
var divided = BigInteger.DivRem(numerator, denominator, out var rem);

if (scale == 0)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Rationals/ExtendedProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ namespace Rationals
public partial struct Rational
{
/// <summary>
/// True if the number is equal to zero
/// True if the number is equal to zero.
/// </summary>
public bool IsZero => Numerator.IsZero;

/// <summary>
/// True if the number is equal to one
/// True if the number is equal to one.
/// </summary>
public bool IsOne => Numerator == Denominator;

/// <summary>
/// Gets a number that indicates the sign (negative, positive, or zero) of the rational number
/// Gets a number that indicates the sign (negative, positive, or zero) of the rational number.
/// </summary>
public int Sign => Numerator.Sign * Denominator.Sign;

Expand Down
3 changes: 1 addition & 2 deletions src/Rationals/Formatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public IEnumerable<char> Digits
var returnedAny = false;
while (numerator > 0)
{
BigInteger rem;
var divided = BigInteger.DivRem(numerator, denominator, out rem);
var divided = BigInteger.DivRem(numerator, denominator, out var rem);

var digits = divided.ToString(CultureInfo.InvariantCulture);

Expand Down
87 changes: 47 additions & 40 deletions src/Rationals/Operations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ namespace Rationals
public partial struct Rational
{
/// <summary>
/// Multiplicative inverse of the rational number
/// Multiplicative inverse of the rational number.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static Rational Invert(Rational p)
{
var numerator = p.Denominator;
Expand All @@ -19,10 +17,8 @@ public static Rational Invert(Rational p)
}

/// <summary>
/// Additive inverse of the rational number
/// Additive inverse of the rational number.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static Rational Negate(Rational p)
{
if (p.IsZero)
Expand All @@ -47,9 +43,6 @@ public static Rational Negate(Rational p)
/// <summary>
/// Returns the sum of the two numbers.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Rational Add(Rational left, Rational right)
{
if (right.IsZero)
Expand All @@ -67,9 +60,6 @@ public static Rational Add(Rational left, Rational right)
/// <summary>
/// Subtracts one number from another and returns result.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Rational Subtract(Rational left, Rational right)
{
if (right.IsZero)
Expand All @@ -87,9 +77,6 @@ public static Rational Subtract(Rational left, Rational right)
/// <summary>
/// Returns product of the two numbers.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Rational Multiply(Rational left, Rational right)
{
if (left.IsZero || right.IsZero)
Expand All @@ -104,9 +91,6 @@ public static Rational Multiply(Rational left, Rational right)
/// <summary>
/// Divides one number by another and returns result.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static Rational Divide(Rational left, Rational right)
{
if (right.IsZero)
Expand All @@ -122,11 +106,8 @@ public static Rational Divide(Rational left, Rational right)
}

/// <summary>
/// Exponentiation of the rational number to the given integer exponent
/// Exponentiation of the rational number to the given integer exponent.
/// </summary>
/// <param name="number"></param>
/// <param name="exponent"></param>
/// <returns></returns>
public static Rational Pow(Rational number, int exponent)
{
if (exponent == 1)
Expand Down Expand Up @@ -163,10 +144,8 @@ public static Rational Pow(Rational number, int exponent)
}

/// <summary>
/// Gets the absolute value of the rational number
/// Gets the absolute value of the rational number.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static Rational Abs(Rational p)
{
if (p.IsZero)
Expand All @@ -179,10 +158,8 @@ public static Rational Abs(Rational p)
}

/// <summary>
/// Returns the base 10 logarithm of a rational number
/// Returns the base 10 logarithm of a rational number.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static double Log10(Rational p)
{
if (p.IsZero)
Expand All @@ -193,10 +170,8 @@ public static double Log10(Rational p)
}

/// <summary>
/// Returns the natural (base e) logarithm of a rational number
/// Returns the natural (base e) logarithm of a rational number.
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static double Log(Rational p)
{
if (p.IsZero)
Expand All @@ -207,11 +182,8 @@ public static double Log(Rational p)
}

/// <summary>
/// Returns the logarithm of a rational number
/// Returns the logarithm of a rational number.
/// </summary>
/// <param name="p"></param>
/// <param name="baseValue"></param>
/// <returns></returns>
public static double Log(Rational p, double baseValue)
{
if (p.IsZero)
Expand All @@ -222,12 +194,42 @@ public static double Log(Rational p, double baseValue)
}

/// <summary>
/// Root of the rational number to the given integer radix
/// Root of the rational number as double.
/// </summary>
/// <param name="number"></param>
/// <param name="radix"></param>
/// <returns></returns>
public static Rational Root(Rational number, int radix)
public static double Root(Rational number, double radix)
{
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (radix == 1)
return (double)number;

// ReSharper disable once CompareOfFloatsByEqualityOperator
if (radix == 0)
throw new InvalidOperationException("Cannot use zero radix.");

if (number.IsOne)
return 1;

if (number.IsZero)
{
if (radix < 0)
throw new DivideByZeroException("Cannot root zero to negative exponent.");

return 0;
}

if (number < 0)
{
throw new InvalidOperationException("Cannot compute root of negative number.");
}

var result = Math.Pow((double)number, 1 / radix);
return result;
}

/// <summary>
/// Root of the rational number to the given integer radix (experimental).
/// </summary>
public static Rational RationalRoot(Rational number, int radix)
{
if (radix == 1)
return number;
Expand All @@ -246,6 +248,11 @@ public static Rational Root(Rational number, int radix)
return number;
}

if (number < 0)
{
throw new InvalidOperationException("Cannot compute root of negative number.");
}

if (radix > 0)
{
var numerator = BigIntegerRootUtils.BigIntegerRoot(number.Numerator, radix);
Expand Down
24 changes: 9 additions & 15 deletions src/Rationals/Parsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ public static Rational Parse(string value, NumberStyles style, IFormatProvider p

private static Rational Parse(string value, NumberStyles style, NumberFormatInfo info)
{
Rational result;

if (!TryParse(value, style, info, out result))
if (!TryParse(value, style, info, out var result))
{
throw new FormatException("Cannot parse string as Rational, the input is in incorrect format.");
}
Expand Down Expand Up @@ -86,8 +84,7 @@ private static bool TryParse(string value, NumberStyles style, NumberFormatInfo
return false;
}

BigInteger justWhole;
if (BigInteger.TryParse(value, style, info, out justWhole))
if (BigInteger.TryParse(value, style, info, out var justWhole))
{
result = new Rational(justWhole);
return true;
Expand All @@ -98,28 +95,27 @@ private static bool TryParse(string value, NumberStyles style, NumberFormatInfo

public static Rational ParseDecimal(string value, decimal tolerance = 0)
{
return ParseDecimal(value, NumberStyles.Float, NumberFormatInfo.CurrentInfo, tolerance);
return ParseDecimal(value, NumberStyles.Float, NumberFormatInfo.CurrentInfo);
}

public static Rational ParseDecimal(string value, NumberStyles style, decimal tolerance = 0)
{
return ParseDecimal(value, style, NumberFormatInfo.CurrentInfo, tolerance);
return ParseDecimal(value, style, NumberFormatInfo.CurrentInfo);
}

public static Rational ParseDecimal(string value, IFormatProvider provider, decimal tolerance = 0)
{
return ParseDecimal(value, NumberStyles.Float, NumberFormatInfo.GetInstance(provider), tolerance);
return ParseDecimal(value, NumberStyles.Float, NumberFormatInfo.GetInstance(provider));
}

public static Rational ParseDecimal(string value, NumberStyles style, IFormatProvider provider, decimal tolerance = 0)
{
return ParseDecimal(value, style, NumberFormatInfo.GetInstance(provider), tolerance);
return ParseDecimal(value, style, NumberFormatInfo.GetInstance(provider));
}

private static Rational ParseDecimal(string value, NumberStyles style, NumberFormatInfo info, decimal tolerance = 0)
private static Rational ParseDecimal(string value, NumberStyles style, NumberFormatInfo info)
{
Rational result;
if (!TryParseDecimal(value, style, info, out result))
if (!TryParseDecimal(value, style, info, out var result))
{
throw new FormatException("Cannot parse string as Rational, the input is in incorrect format.");
}
Expand All @@ -139,9 +135,7 @@ public static bool TryParseDecimal(string value, NumberStyles style, IFormatProv

private static bool TryParseDecimal(string value, NumberStyles style, NumberFormatInfo info, out Rational result, decimal tolerance = 0)
{
decimal d;

if (!decimal.TryParse(value, style, info, out d))
if (!decimal.TryParse(value, style, info, out var d))
{
result = default(Rational);
return false;
Expand Down
3 changes: 1 addition & 2 deletions src/Rationals/WholeAndFraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public BigInteger WholePart
return wholePart;
}

BigInteger remainder;
wholePart = BigInteger.DivRem(num, den, out remainder);
wholePart = BigInteger.DivRem(num, den, out var remainder);

if (remainder != 0)
wholePart--;
Expand Down
Loading

0 comments on commit 5d100d2

Please sign in to comment.