Example #1
0
        /// <summary>
        /// Creates a normalized (reduced/simplified) or unnormalized fraction using <paramref name="numerator"/> and <paramref name="denominator"/>.
        /// </summary>
        /// <param name="numerator">Numerator</param>
        /// <param name="denominator">Denominator</param>
        /// <param name="normalize">If <c>true</c> the fraction will be created as reduced/simplified fraction.
        /// This is recommended, especially if your applications requires that the results of the equality methods <see cref="object.Equals(object)"/>
        /// and <see cref="IComparable.CompareTo"/> are always the same. (1/2 != 2/4)</param>
        public Fraction(BigInteger numerator, BigInteger denominator, bool normalize)
        {
            if (normalize)
            {
                this = GetReducedFraction(numerator, denominator);
                return;
            }

            _state = (numerator.IsZero && denominator.IsZero)
                ? FractionState.IsNormalized
                : FractionState.Unknown;

            _numerator   = numerator;
            _denominator = denominator;
        }
Example #2
0
 /// <summary>
 /// Create a fraction with <paramref name="numerator"/>, <paramref name="denominator"/> and the fraction' <paramref name="state"/>.
 /// Warning: if you use unreduced values combined with a state of <see cref="FractionState.IsNormalized"/>
 /// you will get wrong results when working with the fraction value.
 /// </summary>
 /// <param name="numerator"></param>
 /// <param name="denominator"></param>
 /// <param name="state"></param>
 private Fraction(BigInteger numerator, BigInteger denominator, FractionState state)
 {
     _numerator   = numerator;
     _denominator = denominator;
     _state       = state;
 }
Example #3
0
 /// <summary>
 /// Creates a normalized fraction using a big integer.
 /// </summary>
 /// <param name="numerator">big integer value that will be used for the numerator. The denominator will be 1.</param>
 public Fraction(BigInteger numerator)
 {
     _numerator   = numerator;
     _denominator = numerator.IsZero ? BigInteger.Zero : BigInteger.One;
     _state       = FractionState.IsNormalized;
 }
Example #4
0
 /// <summary>
 /// Creates a normalized fraction using a unsigned 64bit integer.
 /// </summary>
 /// <param name="numerator">integer value that will be used for the numerator. The denominator will be 1.</param>
 public Fraction(UInt64 numerator)
 {
     _numerator   = new BigInteger(numerator);
     _denominator = numerator != 0 ? BigInteger.One : BigInteger.Zero;
     _state       = FractionState.IsNormalized;
 }