📄 firational.cs
字号:
numerator = continuedFraction[i] * numerator + denominator;
denominator = temp;
}
}
/// <summary>
/// Tries 'brute force' to approximate <paramref name="value"/> with a fraction.
/// </summary>
private static void ApproximateFraction(decimal value, int maxDen, out int num, out int den)
{
num = 0;
den = 0;
decimal bestDifference = 1m;
decimal currentDifference = -1m;
int digits = GetDigits(value);
if (digits <= 9)
{
int mul = 1;
for (int i = 1; i <= digits; i++)
{
mul *= 10;
}
if (mul <= maxDen)
{
num = (int)(value * mul);
den = mul;
return;
}
}
for (int i = 1; i <= maxDen; i++)
{
int numerator = (int)Math.Floor(value * (decimal)i + 0.5m);
currentDifference = Math.Abs(value - (decimal)numerator / (decimal)i);
if (currentDifference < bestDifference)
{
num = numerator;
den = i;
bestDifference = currentDifference;
}
}
}
/// <summary>
/// Converts the numeric value of the <see cref="FIRational"/> object
/// to its equivalent string representation.
/// </summary>
/// <returns>The string representation of the value of this instance.</returns>
public override string ToString()
{
return ((IConvertible)this).ToDouble(null).ToString();
}
/// <summary>
/// Tests whether the specified object is a <see cref="FIRational"/> structure
/// and is equivalent to this <see cref="FIRational"/> structure.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="FIRational"/> structure
/// equivalent to this <see cref="FIRational"/> structure; otherwise, <b>false</b>.</returns>
public override bool Equals(object obj)
{
return ((obj is FIRational) && (this == ((FIRational)obj)));
}
/// <summary>
/// Returns a hash code for this <see cref="FIRational"/> structure.
/// </summary>
/// <returns>An integer value that specifies the hash code for this <see cref="FIRational"/>.</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
#region Operators
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator +(FIRational r1)
{
return r1;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator -(FIRational r1)
{
r1.numerator *= -1;
return r1;
}
/// <summary>
/// Returns the reciprocal value of this instance.
/// </summary>
public static FIRational operator ~(FIRational r1)
{
int temp = r1.denominator;
r1.denominator = r1.numerator;
r1.numerator = temp;
r1.Normalize();
return r1;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator ++(FIRational r1)
{
checked
{
r1.numerator += r1.denominator;
}
return r1;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator --(FIRational r1)
{
checked
{
r1.numerator -= r1.denominator;
}
return r1;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator +(FIRational r1, FIRational r2)
{
long numerator = 0;
long denominator = Scm(r1.denominator, r2.denominator);
numerator = (r1.numerator * (denominator / r1.denominator)) + (r2.numerator * (denominator / r2.denominator));
Normalize(ref numerator, ref denominator);
checked
{
return new FIRational((int)numerator, (int)denominator);
}
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator -(FIRational r1, FIRational r2)
{
return r1 + (-r2);
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator *(FIRational r1, FIRational r2)
{
long numerator = r1.numerator * r2.numerator;
long denominator = r1.denominator * r2.denominator;
Normalize(ref numerator, ref denominator);
checked
{
return new FIRational((int)numerator, (int)denominator);
}
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator /(FIRational r1, FIRational r2)
{
int temp = r2.denominator;
r2.denominator = r2.numerator;
r2.numerator = temp;
return r1 * r2;
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static FIRational operator %(FIRational r1, FIRational r2)
{
r2.Normalize();
if (Math.Abs(r2.numerator) < r2.denominator)
return new FIRational(0, 0);
int div = (int)(r1 / r2);
return r1 - (r2 * div);
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator ==(FIRational r1, FIRational r2)
{
r1.Normalize();
r2.Normalize();
return (r1.numerator == r2.numerator) && (r1.denominator == r2.denominator);
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator !=(FIRational r1, FIRational r2)
{
return !(r1 == r2);
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator >(FIRational r1, FIRational r2)
{
long denominator = Scm(r1.denominator, r2.denominator);
return (r1.numerator * (denominator / r1.denominator)) > (r2.numerator * (denominator / r2.denominator));
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator <(FIRational r1, FIRational r2)
{
long denominator = Scm(r1.denominator, r2.denominator);
return (r1.numerator * (denominator / r1.denominator)) < (r2.numerator * (denominator / r2.denominator));
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator >=(FIRational r1, FIRational r2)
{
long denominator = Scm(r1.denominator, r2.denominator);
return (r1.numerator * (denominator / r1.denominator)) >= (r2.numerator * (denominator / r2.denominator));
}
/// <summary>
/// Standard implementation of the operator.
/// </summary>
public static bool operator <=(FIRational r1, FIRational r2)
{
long denominator = Scm(r1.denominator, r2.denominator);
return (r1.numerator * (denominator / r1.denominator)) <= (r2.numerator * (denominator / r2.denominator));
}
#endregion
#region Conversions
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to a <see cref="Boolean"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Boolean"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator bool(FIRational value)
{
return (value.numerator != 0);
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to a <see cref="Byte"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Byte"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator byte(FIRational value)
{
return (byte)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to a <see cref="Char"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Char"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator char(FIRational value)
{
return (char)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to a <see cref="Decimal"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Decimal"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator decimal(FIRational value)
{
return value.denominator == 0 ? 0m : (decimal)value.numerator / (decimal)value.denominator;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to a <see cref="Double"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Double"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator double(FIRational value)
{
return value.denominator == 0 ? 0d : (double)value.numerator / (double)value.denominator;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to an <see cref="Int16"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Int16"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator short(FIRational value)
{
return (short)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to an <see cref="Int32"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Int32"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator int(FIRational value)
{
return (int)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to an <see cref="Int64"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Int64"/> initialized to <paramref name="value"/>.</returns>
public static explicit operator long(FIRational value)
{
return (byte)(double)value;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to a <see cref="Single"/> structure.
/// </summary>
/// <param name="value">A <see cref="FIRational"/> structure.</param>
/// <returns>A new instance of <see cref="Single"/> initialized to <paramref name="value"/>.</returns>
public static implicit operator float(FIRational value)
{
return value.denominator == 0 ? 0f : (float)value.numerator / (float)value.denominator;
}
/// <summary>
/// Converts the value of a <see cref="FIRational"/> structure to a <see cref="SByte"/> structure.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -