MixedFraction.fromDouble constructor
Tries to give a fractional representation of a double according with the given precision. This implementation takes inspiration from the continued fraction algorithm.
MixedFraction.fromDouble(5.46) // represented as 5 + 23/50
Note that irrational numbers can not be represented as fractions. If you try to use this method on π (3.1415...) for example, you won't get a valid result:
MixedFraction.fromDouble(math.pi)
The above code doesn't throw. It returns a MixedFraction object because
the algorithm only considers the first 10 decimal digits (since
precision
is set to 1.0e-10).
MixedFraction.fromDouble(math.pi, precision: 1.0e-20)
This example will return another different value because it considers the first 20 digits. It's still not a fractional representation of π because irrational numbers cannot be expressed as fractions.
You should only use this method with rational numbers.
Implementation
factory MixedFraction.fromDouble(double value, {double precision = 1.0e-12}) {
// In this way we can reuse the continued fraction algorithm.
final fraction = Fraction.fromDouble(value, precision: precision);
return fraction.toMixedFraction();
}