Fraction constructor

Fraction(
  1. int numerator, [
  2. int denominator = 1
])

If the denominator is negative, the fraction is 'normalized' so that the minus sign only appears in front of the denominator. For example:

 Fraction(3, 4)  // is interpreted as 3/4
 Fraction(-3, 4) // is interpreted as -3/4
 Fraction(3, -4) // is interpreted as -3/4
 Fraction(3)     // is interpreted as 3/1

A FractionException object is thrown when the denominator is 0.

Implementation

factory Fraction(int numerator, [int denominator = 1]) {
  if (denominator == 0) {
    throw const FractionException('Denominator cannot be zero.');
  }

  // Fixing the sign of numerator and denominator
  if (denominator < 0) {
    return Fraction._(numerator * -1, denominator * -1);
  } else {
    return Fraction._(numerator, denominator);
  }
}