Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added new 'MixedFraction.fromDouble' constructor #19

Merged
merged 5 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.1.2]
- Added the `MixedFraction.fromDouble` method
- Dependencies update

## [4.1.1]
- Added more linter rules
- Dependencies updates
Expand Down
5 changes: 4 additions & 1 deletion example/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 1.0.2
## 1.0.4
- Updated dependencies

## 1.0.3
- Updated `analysis_options.yaml` file
- Updated dependencies

Expand Down
6 changes: 3 additions & 3 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fraction_example
description: A simple Dart CLI application for the 'fraction' package
version: 1.0.3
version: 1.0.4
repository: https://github.com/albertodev01/fraction/tree/master/example
homepage: https://fluttercompletereference.com/
publish_to: none
Expand All @@ -14,7 +14,7 @@ dependencies:

dev_dependencies:
# https://pub.dev/packages/dart_code_metrics
dart_code_metrics: ^4.15.2
dart_code_metrics: ^4.16.0

# https://pub.dev/packages/test
test: ^1.21.1
test: ^1.21.4
34 changes: 0 additions & 34 deletions example/test/console_test.dart

This file was deleted.

36 changes: 36 additions & 0 deletions lib/src/types/mixed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ class MixedFraction extends Rational {
);
}

/// Tries to give a fractional representation of a double according with the
/// given precision. This implementation takes inspiration from the
/// (continued fraction)[https://en.wikipedia.org/wiki/Continued_fraction]
/// algorithm.
///
/// ```dart
/// MixedFraction.fromDouble(5.46) // represented as 5 + 23/50
/// ```
///
/// Note that irrational numbers can **not** be represented as fractions, so
/// if you try to use this method on π (3.1415...) you won't get a valid
/// result.
///
/// ```dart
/// MixedFraction.fromDouble(math.pi)
/// ```
///
/// The above returns a mixed fraction because the algorithm considers only
/// the first 10 decimal digits (since `precision` is set to 1.0e-10).
///
/// ```dart
/// 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 pi because
/// irrational numbers cannot be expressed as fractions.
///
/// This method is good with rational numbers.
factory MixedFraction.fromDouble(double value, {double precision = 1.0e-12}) {
// Use 'Fraction' to reuse the continued fraction algorithm.
final fraction = Fraction.fromDouble(value, precision: precision);

return fraction.toMixedFraction();
}

@override
int get numerator => _numerator;

Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: fraction
description: A package that helps you working with mathematical fractions.
version: 4.1.1
version: 4.1.2
repository: https://github.com/albertodev01/fraction
homepage: https://fluttercompletereference.com/

Expand All @@ -9,7 +9,7 @@ environment:

dev_dependencies:
# https://pub.dev/packages/dart_code_metrics
dart_code_metrics: ^4.15.2
dart_code_metrics: ^4.16.0

# https://pub.dev/packages/test
test: ^1.21.1
test: ^1.21.4
34 changes: 34 additions & 0 deletions test/types/mixed_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ void main() {
expect(fraction3.numerator, equals(2));
expect(fraction3.denominator, equals(7));
expect(fraction3.toString(), equals('-6 2/7'));

expect(
MixedFraction(whole: -3, numerator: 2, denominator: 5).toFraction(),
equals(Fraction(-17, 5)),
);
expect(
MixedFraction(whole: -4, numerator: 5, denominator: 6).toFraction(),
equals(Fraction(-29, 6)),
);
});

test(
Expand Down Expand Up @@ -189,6 +198,31 @@ void main() {
);
},
);

test(
'Making sure that mixed fractions are properly constructed from '
'decimal values',
() {
expect(
MixedFraction.fromDouble(5.46),
equals(
MixedFraction(whole: 5, numerator: 23, denominator: 50),
),
);
expect(
MixedFraction.fromDouble(1.06),
equals(
MixedFraction(whole: 1, numerator: 3, denominator: 50),
),
);
expect(
MixedFraction.fromDouble(0),
equals(
MixedFraction(whole: 0, numerator: 0, denominator: 1),
),
);
},
);
});

group('Testing objects equality', () {
Expand Down