MixedFraction class base
Dart representation of a 'mixed fraction', which is made up by the whole
part and a proper fraction. A proper fraction is a fraction in which the
relation numerator <= denominator
is true.
To create a MixedFraction object, use one of its constructors:
MixedFraction(
whole: 5,
numerator: 2,
denominator: 3,
);
MixedFraction.fromDouble(1.5);
MixedFraction.fromString("1 1/2");
The MixedFraction type is immutable, so methods that require changing the object's internal state return a new instance. For example, the reduce method returns a new MixedFraction object that does not depend on the original one. Another example:
final f1 = MixedFraction(whole: 5, numerator: 7, denominator: 8); // 5 7/8
final f2 = MixedFraction(whole: 3, numerator: 1, denominator: 9); // 3 1/9
final sum = f1 + f2; // 8 71/72
final sub = f1 - f2; // -2 17/72
final mul = f1 * f2; // 18 20/72
final div = f1 / f2; // 1 199/224
Operators always return new objects. There are extension methods on num and String that allow you to build MixedFraction objects "on the fly". For example:
1.5.toMixedFraction();
"1 1/2".toMixedFraction();
If the string doesn't represent a valid fraction, a MixedFractionException object is thrown.
Constructors
- MixedFraction({required int whole, required int numerator, required int denominator})
-
If the numerator isn't greater than the denominator, values are
transformed so that a valid mixed fraction is created. For example:
factory
- MixedFraction.fromDouble(double value, {double precision = 1.0e-12})
-
Tries to give a fractional representation of a double according with the
given precision. This implementation takes inspiration from the continued
fraction algorithm.
factory
- MixedFraction.fromFraction(Fraction fraction)
-
Creates an instance of a mixed fraction.
factory
- MixedFraction.fromString(String value)
-
Creates a MixedFraction object from
value
. The input string must be in the form 'a b/c' with exactly one space between the whole part and the fraction.factory
Properties
- denominator → int
-
The divisor
b
of thea/b
division, which also is the denominator of the associated fraction.no setteroverride - fractionalPart → Fraction
-
Returns the fractional part of the mixed fraction as Fraction object.
no setter
- hashCode → int
-
The hash code for this object.
no setteroverride
- isNegative → bool
-
True or false whether this rational number is positive or negative.
no setteroverride
- isWhole → bool
-
True or false whether this rational number is whole or not.
no setteroverride
- numerator → int
-
The dividend
a
of thea/b
division, which also is the numerator of the associated fraction.no setteroverride - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- whole → int
-
Whole part of the mixed fraction.
final
Methods
-
compareTo(
Rational other) → int -
Compares this object to another object.
inherited
-
copyWith(
{int? whole, int? numerator, int? denominator}) → MixedFraction - Creates a deep copy of this object with the given fields replaced with the new values.
-
negate(
) → MixedFraction -
The sign is changed and the result is returned in new Rational object.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reduce(
) → MixedFraction -
Reduces this rational number to the lowest terms and returns the result in
a new Rational object.
override
-
toDouble(
) → double -
A floating point representation of this rational number.
override
-
toEgyptianFraction(
) → List< Fraction> -
Represents this rational number as an egyptian fraction.
override
-
toFraction(
) → Fraction - Converts this mixed fraction into a fraction.
-
toString(
) → String -
A string representation of this object.
override
-
toStringAsGlyph(
) → String - If possible, this method converts this MixedFraction instance into an unicode glyph string. For example:
Operators
-
operator *(
MixedFraction other) → MixedFraction - The product of two mixed fractions.
-
operator +(
MixedFraction other) → MixedFraction - The sum between two mixed fractions.
-
operator -(
MixedFraction other) → MixedFraction - The difference between two mixed fractions.
-
operator /(
MixedFraction other) → MixedFraction - The division of two mixed fractions.
-
operator <(
Rational other) → bool -
Checks whether this rational number is smaller than the other.
inherited
-
operator <=(
Rational other) → bool -
Checks whether this rational number is smaller or equal than the other.
inherited
-
operator ==(
Object other) → bool -
The equality operator.
override
-
operator >(
Rational other) → bool -
Checks whether this rational number is greater than the other.
inherited
-
operator >=(
Rational other) → bool -
Checks whether this rational number is greater or equal than the other.
inherited
-
operator [](
int index) → int - Access the whole part, the numerator or the denominator of the fraction via index. In particular: