Fraction constructor

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

Creates a fraction from a numerator and an optional denominator.

Implementation

factory Fraction(int numerator, [int denominator = 1]) {
  if (denominator == 0) {
    return numerator == 0
        ? Fraction.nan
        : numerator < 0
            ? Fraction.negativeInfinity
            : Fraction.infinity;
  }
  var divisor = numerator.gcd(denominator).abs();
  if (denominator < 0) {
    divisor *= -1;
  }
  return divisor == 1
      ? Fraction._(numerator, denominator)
      : Fraction._(numerator ~/ divisor, denominator ~/ divisor);
}