⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fiurational.cs

📁 对gif
💻 CS
📖 第 1 页 / 共 3 页
字号:
			decimal currentDifference = -1m;
			int digits = GetDigits(value);

			if (digits <= 9)
			{
				uint mul = 1;
				for (int i = 1; i <= digits; i++)
				{
					mul *= 10;
				}
				if (mul <= maxDen)
				{
					num = (uint)(value * mul);
					den = mul;
					return;
				}
			}

			for (uint u = 1; u <= maxDen; u++)
			{
				uint numerator = (uint)Math.Floor(value * (decimal)u + 0.5m);
				currentDifference = Math.Abs(value - (decimal)numerator / (decimal)u);
				if (currentDifference < bestDifference)
				{
					num = numerator;
					den = u;
					bestDifference = currentDifference;
				}
			}
		}

		/// <summary>
		/// Converts the numeric value of the <see cref="FIURational"/> 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="FIURational"/> structure
		/// and is equivalent to this <see cref="FIURational"/> structure.
		/// </summary>
		/// <param name="obj">The object to test.</param>
		/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="FIURational"/> structure
		/// equivalent to this <see cref="FIURational"/> structure; otherwise, <b>false</b>.</returns>
		public override bool Equals(object obj)
		{
			return ((obj is FIURational) && (this == ((FIURational)obj)));
		}

		/// <summary>
		/// Returns a hash code for this <see cref="FIURational"/> structure.
		/// </summary>
		/// <returns>An integer value that specifies the hash code for this <see cref="FIURational"/>.</returns>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		#region Operators

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator +(FIURational value)
		{
			return value;
		}

		/// <summary>
		/// Returns the reciprocal value of this instance.
		/// </summary>
		public static FIURational operator ~(FIURational value)
		{
			uint temp = value.denominator;
			value.denominator = value.numerator;
			value.numerator = temp;
			value.Normalize();
			return value;
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator ++(FIURational value)
		{
			checked
			{
				value.numerator += value.denominator;
			}
			return value;
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator --(FIURational value)
		{
			checked
			{
				value.numerator -= value.denominator;
			}
			return value;
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator +(FIURational left, FIURational right)
		{
			ulong numerator = 0;
			ulong denominator = Scm(left.denominator, right.denominator);
			numerator = (left.numerator * (denominator / left.denominator)) +
						(right.numerator * (denominator / right.denominator));
			Normalize(ref numerator, ref denominator);
			checked
			{
				return new FIURational((uint)numerator, (uint)denominator);
			}
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator -(FIURational left, FIURational right)
		{
			checked
			{
				if (left.denominator != right.denominator)
				{
					uint denom = left.denominator;
					left.numerator *= right.denominator;
					left.denominator *= right.denominator;
					right.numerator *= denom;
					right.denominator *= denom;
				}
				left.numerator -= right.numerator;
				left.Normalize();
				return left;
			}
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator *(FIURational left, FIURational r2)
		{
			ulong numerator = left.numerator * r2.numerator;
			ulong denominator = left.denominator * r2.denominator;
			Normalize(ref numerator, ref denominator);
			checked
			{
				return new FIURational((uint)numerator, (uint)denominator);
			}
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator /(FIURational left, FIURational right)
		{
			uint temp = right.denominator;
			right.denominator = right.numerator;
			right.numerator = temp;
			return left * right;
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static FIURational operator %(FIURational left, FIURational right)
		{
			right.Normalize();
			if (Math.Abs(right.numerator) < right.denominator)
				return new FIURational(0, 0);
			int div = (int)(left / right);
			return left - (right * div);
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static bool operator ==(FIURational left, FIURational right)
		{
			left.Normalize();
			right.Normalize();
			return (left.numerator == right.numerator) && (left.denominator == right.denominator);
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static bool operator !=(FIURational left, FIURational right)
		{
			left.Normalize();
			right.Normalize();
			return (left.numerator != right.numerator) || (left.denominator != right.denominator);
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static bool operator >(FIURational left, FIURational right)
		{
			ulong denominator = Scm(left.denominator, right.denominator);
			return (left.numerator * (denominator / left.denominator)) >
				(right.numerator * (denominator / right.denominator));
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static bool operator <(FIURational left, FIURational right)
		{
			ulong denominator = Scm(left.denominator, right.denominator);
			return (left.numerator * (denominator / left.denominator)) <
				(right.numerator * (denominator / right.denominator));
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static bool operator >=(FIURational left, FIURational right)
		{
			ulong denominator = Scm(left.denominator, right.denominator);
			return (left.numerator * (denominator / left.denominator)) >=
				(right.numerator * (denominator / right.denominator));
		}

		/// <summary>
		/// Standard implementation of the operator.
		/// </summary>
		public static bool operator <=(FIURational left, FIURational right)
		{
			ulong denominator = Scm(left.denominator, right.denominator);
			return (left.numerator * (denominator / left.denominator)) <=
				(right.numerator * (denominator / right.denominator));
		}

		#endregion

		#region Conversions

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Boolean"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Boolean"/> initialized to <paramref name="value"/>.</returns>
		public static explicit operator bool(FIURational value)
		{
			return (value.numerator != 0);
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Byte"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Byte"/> initialized to <paramref name="value"/>.</returns>
		public static explicit operator byte(FIURational value)
		{
			return (byte)(double)value;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Char"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Char"/> initialized to <paramref name="value"/>.</returns>
		public static explicit operator char(FIURational value)
		{
			return (char)(double)value;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Decimal"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Decimal"/> initialized to <paramref name="value"/>.</returns>
		public static implicit operator decimal(FIURational value)
		{
			return value.denominator == 0 ? 0m : (decimal)value.numerator / (decimal)value.denominator;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Double"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Double"/> initialized to <paramref name="value"/>.</returns>
		public static implicit operator double(FIURational value)
		{
			return value.denominator == 0 ? 0d : (double)value.numerator / (double)value.denominator;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="Int16"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Int16"/> initialized to <paramref name="value"/>.</returns>
		public static explicit operator short(FIURational value)
		{
			return (short)(double)value;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="Int32"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Int32"/> initialized to <paramref name="value"/>.</returns>
		public static explicit operator int(FIURational value)
		{
			return (int)(double)value;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to an <see cref="Int64"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Int64"/> initialized to <paramref name="value"/>.</returns>
		public static explicit operator long(FIURational value)
		{
			return (byte)(double)value;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="Single"/> structure.
		/// </summary>
		/// <param name="value">A <see cref="FIURational"/> structure.</param>
		/// <returns>A new instance of <see cref="Single"/> initialized to <paramref name="value"/>.</returns>
		public static implicit operator float(FIURational value)
		{
			return value.denominator == 0 ? 0f : (float)value.numerator / (float)value.denominator;
		}

		/// <summary>
		/// Converts the value of a <see cref="FIURational"/> structure to a <see cref="SByte"/> structure.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -