Fraction constructor

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

Creates a new representation of a fraction. If the denominator is negative, the fraction is 'normalized' so that the minus sign only appears in front of the denominator.

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

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);
  }
}