Skip to content

Commit

Permalink
refactor(mixed_num): ♻️ rename wholePart and make fractionPart pu…
Browse files Browse the repository at this point in the history
…blic

Signed-off-by: Albert Mañosa <26429103+albertms10@users.noreply.github.com>
  • Loading branch information
albertms10 committed May 13, 2024
1 parent e11f99f commit 3390fd9
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lib/src/utils/mixed_num.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import 'package:meta/meta.dart' show immutable;
/// A mixed number representation.
@immutable
final class MixedNum implements Comparable<MixedNum> {
/// The whole part.
final int integer;
/// The whole part integer.
final int wholePart;

/// The numerator of the fraction part.
final int numerator;

/// The denominator of the fraction part.
final int denominator;

/// Creates a new [MixedNum] from [integer], [numerator], and [denominator].
const MixedNum(this.integer, [this.numerator = 0, this.denominator = 1])
/// Creates a new [MixedNum] from [wholePart], [numerator], and [denominator].
const MixedNum(this.wholePart, [this.numerator = 0, this.denominator = 1])
: assert(
integer >= 0,
wholePart >= 0,
'The whole part must be greater or equal than zero.',
),
assert(
Expand Down Expand Up @@ -88,16 +88,17 @@ final class MixedNum implements Comparable<MixedNum> {
MixedNum get simple => MixedNum.fromDouble(toDouble());

/// This [MixedNum] as a [double].
double toDouble() => integer + _fractionPart;
double toDouble() => wholePart + fractionPart;

double get _fractionPart => numerator / denominator;
/// The fraction part of this [MixedNum].
double get fractionPart => numerator / denominator;

@override
String toString() => _fractionPart == 0
? '$integer'
: integer == 0
String toString() => fractionPart == 0
? '$wholePart'
: wholePart == 0
? '$numerator/$denominator'
: '$integer $numerator/$denominator';
: '$wholePart $numerator/$denominator';

@override
bool operator ==(Object other) =>
Expand Down

0 comments on commit 3390fd9

Please sign in to comment.