R3.Math.Fraction.Reduce C# (CSharp) Method

Reduce() public method

public Reduce ( ) : void
return void
        public void Reduce()
        {
            // Two wrongs do make a right.
            if( A < 0 && B < 0 )
                Reverse();
            else if( B < 0 )
            {
                // Normalize so that A is always < 0 for negatives.
                Reverse();
            }

            if( 0 == B )
            {
                //Debug.Assert( false );
                A = 0;
                B = 1;
                return;
            }

            if( 0 == A )
            {
                B = 1;
                return;
            }

            int gcd = GCD( System.Math.Abs( A ), System.Math.Abs( B ) );
            A = A / gcd;
            B = B / gcd;
        }

Usage Example

Example #1
0
 public static Fraction operator /( Fraction f1, Fraction f2 )
 {
     f1.Reduce();
     f2.Reduce();
     Fraction result = new Fraction( f1.A * f2.B, f1.B * f2.A );
     result.Reduce();
     return result;
 }
All Usage Examples Of R3.Math.Fraction::Reduce