From 14e49a0237b3fd14fefa4fd417213efdddc451be Mon Sep 17 00:00:00 2001 From: guilherme Date: Wed, 21 Jun 2023 20:41:54 +0930 Subject: [PATCH 01/14] chore!: trim public api - hide irrelevant symbols --- lib/big_decimal.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/big_decimal.dart b/lib/big_decimal.dart index e02b799..4a68f46 100644 --- a/lib/big_decimal.dart +++ b/lib/big_decimal.dart @@ -1 +1 @@ -export 'src/big_decimal.dart'; +export 'src/big_decimal.dart' hide plusCode, dotCode, nineCode, zeroCode, minusCode, capitalECode; From 08e3199a6a9e11d727ac66c37ba53beb55baf2bd Mon Sep 17 00:00:00 2001 From: guilherme Date: Wed, 21 Jun 2023 21:40:33 +0930 Subject: [PATCH 02/14] chore!: removed sumScale from public api --- lib/big_decimal.dart | 2 +- lib/src/big_decimal.dart | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/big_decimal.dart b/lib/big_decimal.dart index 4a68f46..1fd114e 100644 --- a/lib/big_decimal.dart +++ b/lib/big_decimal.dart @@ -1 +1 @@ -export 'src/big_decimal.dart' hide plusCode, dotCode, nineCode, zeroCode, minusCode, capitalECode; +export 'src/big_decimal.dart' hide plusCode, dotCode, nineCode, zeroCode, minusCode, capitalECode, sumScale; diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index 07c3df5..89e6bb8 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -334,11 +334,6 @@ class BigDecimal implements Comparable { } } - static int sumScale(int scaleA, int scaleB) { - // TODO: We need to check for overflows here - return scaleA + scaleB; - } - @override int compareTo(BigDecimal other) { if (scale == other.scale) { @@ -423,3 +418,8 @@ class BigDecimal implements Comparable { return b.toString(); } } + +int sumScale(int scaleA, int scaleB) { + // TODO: We need to check for overflows here + return scaleA + scaleB; +} From a9ccddc144bf665d931ad9e088f6b2966c70898b Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 10:29:00 +1030 Subject: [PATCH 03/14] fix: invalid == operator signature --- lib/src/big_decimal.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index 89e6bb8..411ac89 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -117,10 +117,10 @@ class BigDecimal implements Comparable { final int scale; @override - bool operator ==(dynamic other) => + bool operator ==(Object other) => other is BigDecimal && compareTo(other) == 0; - bool exactlyEquals(dynamic other) => + bool exactlyEquals(Object? other) => other is BigDecimal && intVal == other.intVal && scale == other.scale; BigDecimal operator +(BigDecimal other) => From 0e25ff402831b87a24aa0b5dfc614c24aae2e400 Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:04:06 +1030 Subject: [PATCH 04/14] chore: enable missing lints --- analysis_options.yaml | 4 ++++ pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index f95c275..f2777f8 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -4,3 +4,7 @@ include: package:lints/recommended.yaml analyzer: exclude: - example/**/*.dart + +linter: + rules: + - public_member_api_docs \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 9a29ff5..de73cb3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,5 +8,5 @@ environment: sdk: ">=2.12.0 <3.0.0" dev_dependencies: - lints: ^2.0.1 + lints: ^5.1.1 test: ^1.17.2 From e679e4926e89b1d9d51c55643ce890f46a747091 Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:04:49 +1030 Subject: [PATCH 05/14] chore: created a minimal example --- example/main.dart | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 example/main.dart diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 0000000..bbe8618 --- /dev/null +++ b/example/main.dart @@ -0,0 +1,5 @@ +import 'package:big_decimal/big_decimal.dart'; + +final decimal1 = BigDecimal.parse('1'); +final decimal2 = BigDecimal.parse('2'); +final decimal3 = decimal1 + decimal2; // 3 From 6b1fc3e50dc3f3a0c8462775be7c5a2bba17e8b6 Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:15:32 +1030 Subject: [PATCH 06/14] chore: add documentation for rounding mode --- lib/src/big_decimal.dart | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index 411ac89..7a475c6 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -1,13 +1,30 @@ +// enum is using screaming snake case due to a direct migration from java // ignore_for_file: constant_identifier_names +/// rounding mode used when doing operations on a [BigDecimal] enum RoundingMode { + /// away from zero UP, + + /// towards zero DOWN, + + /// towards +infinity CEILING, + + /// towards -infinity FLOOR, + + /// away from zero if remainder comparison to half divisor is even HALF_UP, + + /// towards zero if remainder comparison to half divisor is even HALF_DOWN, + + /// towards zero if remainder comparison to half divisor is even and [BigDecimal] is odd HALF_EVEN, + + /// does not round at all. Throws if not exact and needs rounding UNNECESSARY, } From 7221591ec5397ba7a7caa2ebabb5275395f71a36 Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:19:54 +1030 Subject: [PATCH 07/14] fix: made private members that should not be accessible externally --- lib/big_decimal.dart | 2 +- lib/src/big_decimal.dart | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/big_decimal.dart b/lib/big_decimal.dart index 1fd114e..e02b799 100644 --- a/lib/big_decimal.dart +++ b/lib/big_decimal.dart @@ -1 +1 @@ -export 'src/big_decimal.dart' hide plusCode, dotCode, nineCode, zeroCode, minusCode, capitalECode, sumScale; +export 'src/big_decimal.dart'; diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index 7a475c6..6821ea7 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -28,13 +28,13 @@ enum RoundingMode { UNNECESSARY, } -const plusCode = 43; -const minusCode = 45; -const dotCode = 46; -const smallECode = 101; -const capitalECode = 69; -const zeroCode = 48; -const nineCode = 57; +const _plusCode = 43; +const _minusCode = 45; +const _dotCode = 46; +const _smallECode = 101; +const _capitalECode = 69; +const _zeroCode = 48; +const _nineCode = 57; class BigDecimal implements Comparable { BigDecimal._({ @@ -57,7 +57,7 @@ class BigDecimal implements Comparable { var index = start; for (; index < value.length; index++) { final code = value.codeUnitAt(index); - if (code < zeroCode || code > nineCode) { + if (code < _zeroCode || code > _nineCode) { break; } } @@ -78,11 +78,11 @@ class BigDecimal implements Comparable { var nextIndex = 0; switch (value.codeUnitAt(index)) { - case minusCode: + case _minusCode: sign = '-'; index++; break; - case plusCode: + case _plusCode: index++; break; default: @@ -98,7 +98,7 @@ class BigDecimal implements Comparable { } var decimalPart = ''; - if (value.codeUnitAt(index) == dotCode) { + if (value.codeUnitAt(index) == _dotCode) { index++; nextIndex = nextNonDigit(value, index); decimalPart = value.substring(index, nextIndex); @@ -113,8 +113,8 @@ class BigDecimal implements Comparable { } switch (value.codeUnitAt(index)) { - case smallECode: - case capitalECode: + case _smallECode: + case _capitalECode: index++; final exponent = int.parse(value.substring(index)); return BigDecimal._( @@ -196,11 +196,11 @@ class BigDecimal implements Comparable { return BigDecimal._(intVal: BigInt.zero, scale: newScale); } else { if (newScale > scale) { - final drop = sumScale(newScale, -scale); + final drop = _sumScale(newScale, -scale); final intResult = intVal * BigInt.from(10).pow(drop); return BigDecimal._(intVal: intResult, scale: newScale); } else { - final drop = sumScale(scale, -newScale); + final drop = _sumScale(scale, -newScale); return _divideAndRound(intVal, BigInt.from(10).pow(drop), newScale, roundingMode, newScale); } @@ -240,14 +240,14 @@ class BigDecimal implements Comparable { if (dividend == BigInt.zero) { return BigDecimal._(intVal: BigInt.zero, scale: scale); } - if (sumScale(scale, divisorScale) > dividendScale) { + if (_sumScale(scale, divisorScale) > dividendScale) { final newScale = scale + divisorScale; final raise = newScale - dividendScale; final scaledDividend = dividend * BigInt.from(10).pow(raise); return _divideAndRound( scaledDividend, divisor, scale, roundingMode, scale); } else { - final newScale = sumScale(dividendScale, -scale); + final newScale = _sumScale(dividendScale, -scale); final raise = newScale - divisorScale; final scaledDivisor = divisor * BigInt.from(10).pow(raise); return _divideAndRound( @@ -301,7 +301,7 @@ class BigDecimal implements Comparable { break; } intValMut = intValMut ~/ ten; - scaleMut = sumScale(scaleMut, -1); + scaleMut = _sumScale(scaleMut, -1); } return BigDecimal._(intVal: intValMut, scale: scaleMut); @@ -436,7 +436,7 @@ class BigDecimal implements Comparable { } } -int sumScale(int scaleA, int scaleB) { +int _sumScale(int scaleA, int scaleB) { // TODO: We need to check for overflows here return scaleA + scaleB; } From 6f05e5891b2cd8bc4620e657096a78b3d02f2d5e Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:21:18 +1030 Subject: [PATCH 08/14] fix: made static value final --- lib/src/big_decimal.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index 6821ea7..7a99d4c 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -49,9 +49,9 @@ class BigDecimal implements Comparable { ); } - static BigDecimal zero = BigDecimal.fromBigInt(BigInt.zero); - static BigDecimal one = BigDecimal.fromBigInt(BigInt.one); - static BigDecimal two = BigDecimal.fromBigInt(BigInt.two); + static final zero = BigDecimal.fromBigInt(BigInt.zero); + static final one = BigDecimal.fromBigInt(BigInt.one); + static final two = BigDecimal.fromBigInt(BigInt.two); static int nextNonDigit(String value, [int start = 0]) { var index = start; From b87884ff370cad5d28a964fa7bd1d65745cb6a1e Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:46:53 +1030 Subject: [PATCH 09/14] fix: added missing documentation --- lib/src/big_decimal.dart | 52 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index 7a99d4c..eb725dd 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -24,7 +24,9 @@ enum RoundingMode { /// towards zero if remainder comparison to half divisor is even and [BigDecimal] is odd HALF_EVEN, - /// does not round at all. Throws if not exact and needs rounding + /// does not round at all. + /// + /// Throws [Exception] if not exact and needs rounding UNNECESSARY, } @@ -36,12 +38,14 @@ const _capitalECode = 69; const _zeroCode = 48; const _nineCode = 57; +/// representation of an arbitrarily large decimal number class BigDecimal implements Comparable { BigDecimal._({ required this.intVal, required this.scale, }); + /// factory constructor of a [BigDecimal] from a [BigInt] factory BigDecimal.fromBigInt(BigInt value) { return BigDecimal._( intVal: value, @@ -49,8 +53,13 @@ class BigDecimal implements Comparable { ); } + /// a [BigDecimal] with a numerical value of 0 static final zero = BigDecimal.fromBigInt(BigInt.zero); + + /// a [BigDecimal] with a numerical value of 1 static final one = BigDecimal.fromBigInt(BigInt.one); + + /// a [BigDecimal] with a numerical value of 2 static final two = BigDecimal.fromBigInt(BigInt.two); static int nextNonDigit(String value, [int start = 0]) { @@ -64,6 +73,7 @@ class BigDecimal implements Comparable { return index; } + /// try to create a [BigDecimal] from a [String]. Returns null if invalid static BigDecimal? tryParse(String value) { try { return BigDecimal.parse(value); @@ -72,6 +82,9 @@ class BigDecimal implements Comparable { } } + /// try to create a [BigDecimal] from a [String]. + /// + /// Throws [Exception] if invalid factory BigDecimal.parse(String value) { var sign = ''; var index = 0; @@ -129,38 +142,57 @@ class BigDecimal implements Comparable { ); } + /// the arbitrarily large numeric value without scale final BigInt intVal; + + /// precision of the decimal digits of this late final int precision = _calculatePrecision(); + + /// scale of this [BigDecimal] final int scale; @override bool operator ==(Object other) => other is BigDecimal && compareTo(other) == 0; + /// compares this with [other] for both value and scale bool exactlyEquals(Object? other) => other is BigDecimal && intVal == other.intVal && scale == other.scale; + /// adds this to [other] BigDecimal operator +(BigDecimal other) => _add(intVal, other.intVal, scale, other.scale); + /// multiply this with [other] BigDecimal operator *(BigDecimal other) => BigDecimal._(intVal: intVal * other.intVal, scale: scale + other.scale); + /// subtracts this to [other] BigDecimal operator -(BigDecimal other) => _add(intVal, -other.intVal, scale, other.scale); + /// Whether this is less than [other]. bool operator <(BigDecimal other) => compareTo(other) < 0; + /// Whether this is less than or equal to [other]. bool operator <=(BigDecimal other) => compareTo(other) <= 0; + /// Whether this is greater than [other]. bool operator >(BigDecimal other) => compareTo(other) > 0; + /// Whether this is greater than or equal to [other]. bool operator >=(BigDecimal other) => compareTo(other) >= 0; + /// Negates this big decimal BigDecimal operator -() => BigDecimal._(intVal: -intVal, scale: scale); + /// Returns the absolute value of this BigDecimal abs() => BigDecimal._(intVal: intVal.abs(), scale: scale); + /// divides this number by [divisor]. Defaults to not rounding the number. + /// + /// Throws [Exception] if rounding is [RoundingMode.UNNECESSARY] but rounding + /// is actually necessary. BigDecimal divide( BigDecimal divisor, { RoundingMode roundingMode = RoundingMode.UNNECESSARY, @@ -169,6 +201,7 @@ class BigDecimal implements Comparable { _divide(intVal, this.scale, divisor.intVal, divisor.scale, scale ?? this.scale, roundingMode); + /// this to the power of [n] BigDecimal pow(int n) { if (n >= 0 && n <= 999999999) { // TODO: Check scale of this multiplication @@ -179,13 +212,29 @@ class BigDecimal implements Comparable { 'Invalid operation: Exponent should be between 0 and 999999999'); } + /// returns this as a [double] double toDouble() => intVal.toDouble() / BigInt.from(10).pow(scale).toDouble(); + + /// returns this as a [BigInt] with the desired [roundingMode] + /// + /// Throws [Exception] if rounding is [RoundingMode.UNNECESSARY] but rounding + /// is actually necessary. BigInt toBigInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) => withScale(0, roundingMode: roundingMode).intVal; + + /// returns this as a [int] with the desired [roundingMode] + /// + /// Throws [Exception] if rounding is [RoundingMode.UNNECESSARY] but rounding + /// is actually necessary. int toInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) => toBigInt(roundingMode: roundingMode).toInt(); + /// returns a new [BigDecimal] with the desired [newScale]. May round by + /// [roundingMode]. + /// + /// Throws [Exception] if rounding is [RoundingMode.UNNECESSARY] but rounding + /// is actually necessary. BigDecimal withScale( int newScale, { RoundingMode roundingMode = RoundingMode.UNNECESSARY, @@ -406,6 +455,7 @@ class BigDecimal implements Comparable { return b.toString(); } + /// returns its [String] represantation without using exponential notation String toPlainString() { if (scale == 0) { return intVal.toString(); From 12921d2a1878cf85701204a49b169bb00d6d5c52 Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:47:22 +1030 Subject: [PATCH 10/14] fix: made private remaining methods that should not be exposed --- lib/src/big_decimal.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index eb725dd..cf5752c 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -62,7 +62,7 @@ class BigDecimal implements Comparable { /// a [BigDecimal] with a numerical value of 2 static final two = BigDecimal.fromBigInt(BigInt.two); - static int nextNonDigit(String value, [int start = 0]) { + static int _nextNonDigit(String value, [int start = 0]) { var index = start; for (; index < value.length; index++) { final code = value.codeUnitAt(index); @@ -102,7 +102,7 @@ class BigDecimal implements Comparable { break; } - nextIndex = nextNonDigit(value, index); + nextIndex = _nextNonDigit(value, index); final integerPart = '$sign${value.substring(index, nextIndex)}'; index = nextIndex; @@ -113,7 +113,7 @@ class BigDecimal implements Comparable { var decimalPart = ''; if (value.codeUnitAt(index) == _dotCode) { index++; - nextIndex = nextNonDigit(value, index); + nextIndex = _nextNonDigit(value, index); decimalPart = value.substring(index, nextIndex); index = nextIndex; @@ -324,14 +324,14 @@ class BigDecimal implements Comparable { return BigDecimal._(intVal: quotient, scale: scale); } else { if (preferredScale != scale) { - return createAndStripZerosForScale(quotient, scale, preferredScale); + return _createAndStripZerosForScale(quotient, scale, preferredScale); } else { return BigDecimal._(intVal: quotient, scale: scale); } } } - static BigDecimal createAndStripZerosForScale( + static BigDecimal _createAndStripZerosForScale( BigInt intVal, int scale, int preferredScale, From 8401f0c69bea0d029e01f5d02ff6a5986ab8eff4 Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:51:04 +1030 Subject: [PATCH 11/14] chore: bump minor version --- CHANGELOG.md | 5 +++++ pubspec.yaml | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 686c56f..9583013 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 0.6.0 +- Fix static analysis issues +- Add support for Dart 3 +- Remove internal members that were exposed to the API + ## 0.5.0 - Add toPlainString() diff --git a/pubspec.yaml b/pubspec.yaml index de73cb3..333254c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,11 @@ name: big_decimal -version: 0.5.0 +version: 0.6.0 description: > A bugless implementation of BigDecimal in Dart based on Java's BigDecimal repository: https://github.com/bugless/big-decimal environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.12.0 <4.0.0" dev_dependencies: lints: ^5.1.1 From 0c394346a6314a3f7da331dd39f52c1a88aacf6a Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:52:06 +1030 Subject: [PATCH 12/14] revert line breaks not matching maintainers preferences --- lib/src/big_decimal.dart | 48 ++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index cf5752c..cde517d 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -152,24 +152,19 @@ class BigDecimal implements Comparable { final int scale; @override - bool operator ==(Object other) => - other is BigDecimal && compareTo(other) == 0; + bool operator ==(Object other) => other is BigDecimal && compareTo(other) == 0; /// compares this with [other] for both value and scale - bool exactlyEquals(Object? other) => - other is BigDecimal && intVal == other.intVal && scale == other.scale; + bool exactlyEquals(Object? other) => other is BigDecimal && intVal == other.intVal && scale == other.scale; /// adds this to [other] - BigDecimal operator +(BigDecimal other) => - _add(intVal, other.intVal, scale, other.scale); + BigDecimal operator +(BigDecimal other) => _add(intVal, other.intVal, scale, other.scale); /// multiply this with [other] - BigDecimal operator *(BigDecimal other) => - BigDecimal._(intVal: intVal * other.intVal, scale: scale + other.scale); + BigDecimal operator *(BigDecimal other) => BigDecimal._(intVal: intVal * other.intVal, scale: scale + other.scale); /// subtracts this to [other] - BigDecimal operator -(BigDecimal other) => - _add(intVal, -other.intVal, scale, other.scale); + BigDecimal operator -(BigDecimal other) => _add(intVal, -other.intVal, scale, other.scale); /// Whether this is less than [other]. bool operator <(BigDecimal other) => compareTo(other) < 0; @@ -198,8 +193,7 @@ class BigDecimal implements Comparable { RoundingMode roundingMode = RoundingMode.UNNECESSARY, int? scale, }) => - _divide(intVal, this.scale, divisor.intVal, divisor.scale, - scale ?? this.scale, roundingMode); + _divide(intVal, this.scale, divisor.intVal, divisor.scale, scale ?? this.scale, roundingMode); /// this to the power of [n] BigDecimal pow(int n) { @@ -208,13 +202,11 @@ class BigDecimal implements Comparable { final newScale = scale * n; return BigDecimal._(intVal: intVal.pow(n), scale: newScale); } - throw Exception( - 'Invalid operation: Exponent should be between 0 and 999999999'); + throw Exception('Invalid operation: Exponent should be between 0 and 999999999'); } /// returns this as a [double] - double toDouble() => - intVal.toDouble() / BigInt.from(10).pow(scale).toDouble(); + double toDouble() => intVal.toDouble() / BigInt.from(10).pow(scale).toDouble(); /// returns this as a [BigInt] with the desired [roundingMode] /// @@ -227,8 +219,7 @@ class BigDecimal implements Comparable { /// /// Throws [Exception] if rounding is [RoundingMode.UNNECESSARY] but rounding /// is actually necessary. - int toInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) => - toBigInt(roundingMode: roundingMode).toInt(); + int toInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) => toBigInt(roundingMode: roundingMode).toInt(); /// returns a new [BigDecimal] with the desired [newScale]. May round by /// [roundingMode]. @@ -250,8 +241,7 @@ class BigDecimal implements Comparable { return BigDecimal._(intVal: intResult, scale: newScale); } else { final drop = _sumScale(scale, -newScale); - return _divideAndRound(intVal, BigInt.from(10).pow(drop), newScale, - roundingMode, newScale); + return _divideAndRound(intVal, BigInt.from(10).pow(drop), newScale, roundingMode, newScale); } } } @@ -264,8 +254,7 @@ class BigDecimal implements Comparable { return intVal.abs().compareTo(BigInt.from(10).pow(r)) < 0 ? r : r + 1; } - static BigDecimal _add( - BigInt intValA, BigInt intValB, int scaleA, int scaleB) { + static BigDecimal _add(BigInt intValA, BigInt intValB, int scaleA, int scaleB) { final scaleDiff = scaleA - scaleB; if (scaleDiff == 0) { return BigDecimal._(intVal: intValA + intValB, scale: scaleA); @@ -293,14 +282,12 @@ class BigDecimal implements Comparable { final newScale = scale + divisorScale; final raise = newScale - dividendScale; final scaledDividend = dividend * BigInt.from(10).pow(raise); - return _divideAndRound( - scaledDividend, divisor, scale, roundingMode, scale); + return _divideAndRound(scaledDividend, divisor, scale, roundingMode, scale); } else { final newScale = _sumScale(dividendScale, -scale); final raise = newScale - divisorScale; final scaledDivisor = divisor * BigInt.from(10).pow(raise); - return _divideAndRound( - dividend, scaledDivisor, scale, roundingMode, scale); + return _divideAndRound(dividend, scaledDivisor, scale, roundingMode, scale); } } @@ -315,10 +302,8 @@ class BigDecimal implements Comparable { final remainder = dividend.remainder(divisor).abs(); final quotientPositive = dividend.sign == divisor.sign; if (remainder != BigInt.zero) { - if (_needIncrement( - divisor, roundingMode, quotientPositive, quotient, remainder)) { - final intResult = - quotient + (quotientPositive ? BigInt.one : -BigInt.one); + if (_needIncrement(divisor, roundingMode, quotientPositive, quotient, remainder)) { + final intResult = quotient + (quotientPositive ? BigInt.one : -BigInt.one); return BigDecimal._(intVal: intResult, scale: scale); } return BigDecimal._(intVal: quotient, scale: scale); @@ -363,8 +348,7 @@ class BigDecimal implements Comparable { BigInt quotient, BigInt remainder, ) { - final remainderComparisonToHalfDivisor = - (remainder * BigInt.from(2)).compareTo(divisor); + final remainderComparisonToHalfDivisor = (remainder * BigInt.from(2)).compareTo(divisor); switch (roundingMode) { case RoundingMode.UNNECESSARY: throw Exception('Rounding necessary'); From 49136f32e0d33a5b367433ac43ca01e12ec271c3 Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:55:38 +1030 Subject: [PATCH 13/14] Revert "revert line breaks not matching maintainers preferences" This reverts commit 0c394346a6314a3f7da331dd39f52c1a88aacf6a. --- lib/src/big_decimal.dart | 48 ++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/src/big_decimal.dart b/lib/src/big_decimal.dart index cde517d..cf5752c 100644 --- a/lib/src/big_decimal.dart +++ b/lib/src/big_decimal.dart @@ -152,19 +152,24 @@ class BigDecimal implements Comparable { final int scale; @override - bool operator ==(Object other) => other is BigDecimal && compareTo(other) == 0; + bool operator ==(Object other) => + other is BigDecimal && compareTo(other) == 0; /// compares this with [other] for both value and scale - bool exactlyEquals(Object? other) => other is BigDecimal && intVal == other.intVal && scale == other.scale; + bool exactlyEquals(Object? other) => + other is BigDecimal && intVal == other.intVal && scale == other.scale; /// adds this to [other] - BigDecimal operator +(BigDecimal other) => _add(intVal, other.intVal, scale, other.scale); + BigDecimal operator +(BigDecimal other) => + _add(intVal, other.intVal, scale, other.scale); /// multiply this with [other] - BigDecimal operator *(BigDecimal other) => BigDecimal._(intVal: intVal * other.intVal, scale: scale + other.scale); + BigDecimal operator *(BigDecimal other) => + BigDecimal._(intVal: intVal * other.intVal, scale: scale + other.scale); /// subtracts this to [other] - BigDecimal operator -(BigDecimal other) => _add(intVal, -other.intVal, scale, other.scale); + BigDecimal operator -(BigDecimal other) => + _add(intVal, -other.intVal, scale, other.scale); /// Whether this is less than [other]. bool operator <(BigDecimal other) => compareTo(other) < 0; @@ -193,7 +198,8 @@ class BigDecimal implements Comparable { RoundingMode roundingMode = RoundingMode.UNNECESSARY, int? scale, }) => - _divide(intVal, this.scale, divisor.intVal, divisor.scale, scale ?? this.scale, roundingMode); + _divide(intVal, this.scale, divisor.intVal, divisor.scale, + scale ?? this.scale, roundingMode); /// this to the power of [n] BigDecimal pow(int n) { @@ -202,11 +208,13 @@ class BigDecimal implements Comparable { final newScale = scale * n; return BigDecimal._(intVal: intVal.pow(n), scale: newScale); } - throw Exception('Invalid operation: Exponent should be between 0 and 999999999'); + throw Exception( + 'Invalid operation: Exponent should be between 0 and 999999999'); } /// returns this as a [double] - double toDouble() => intVal.toDouble() / BigInt.from(10).pow(scale).toDouble(); + double toDouble() => + intVal.toDouble() / BigInt.from(10).pow(scale).toDouble(); /// returns this as a [BigInt] with the desired [roundingMode] /// @@ -219,7 +227,8 @@ class BigDecimal implements Comparable { /// /// Throws [Exception] if rounding is [RoundingMode.UNNECESSARY] but rounding /// is actually necessary. - int toInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) => toBigInt(roundingMode: roundingMode).toInt(); + int toInt({RoundingMode roundingMode = RoundingMode.UNNECESSARY}) => + toBigInt(roundingMode: roundingMode).toInt(); /// returns a new [BigDecimal] with the desired [newScale]. May round by /// [roundingMode]. @@ -241,7 +250,8 @@ class BigDecimal implements Comparable { return BigDecimal._(intVal: intResult, scale: newScale); } else { final drop = _sumScale(scale, -newScale); - return _divideAndRound(intVal, BigInt.from(10).pow(drop), newScale, roundingMode, newScale); + return _divideAndRound(intVal, BigInt.from(10).pow(drop), newScale, + roundingMode, newScale); } } } @@ -254,7 +264,8 @@ class BigDecimal implements Comparable { return intVal.abs().compareTo(BigInt.from(10).pow(r)) < 0 ? r : r + 1; } - static BigDecimal _add(BigInt intValA, BigInt intValB, int scaleA, int scaleB) { + static BigDecimal _add( + BigInt intValA, BigInt intValB, int scaleA, int scaleB) { final scaleDiff = scaleA - scaleB; if (scaleDiff == 0) { return BigDecimal._(intVal: intValA + intValB, scale: scaleA); @@ -282,12 +293,14 @@ class BigDecimal implements Comparable { final newScale = scale + divisorScale; final raise = newScale - dividendScale; final scaledDividend = dividend * BigInt.from(10).pow(raise); - return _divideAndRound(scaledDividend, divisor, scale, roundingMode, scale); + return _divideAndRound( + scaledDividend, divisor, scale, roundingMode, scale); } else { final newScale = _sumScale(dividendScale, -scale); final raise = newScale - divisorScale; final scaledDivisor = divisor * BigInt.from(10).pow(raise); - return _divideAndRound(dividend, scaledDivisor, scale, roundingMode, scale); + return _divideAndRound( + dividend, scaledDivisor, scale, roundingMode, scale); } } @@ -302,8 +315,10 @@ class BigDecimal implements Comparable { final remainder = dividend.remainder(divisor).abs(); final quotientPositive = dividend.sign == divisor.sign; if (remainder != BigInt.zero) { - if (_needIncrement(divisor, roundingMode, quotientPositive, quotient, remainder)) { - final intResult = quotient + (quotientPositive ? BigInt.one : -BigInt.one); + if (_needIncrement( + divisor, roundingMode, quotientPositive, quotient, remainder)) { + final intResult = + quotient + (quotientPositive ? BigInt.one : -BigInt.one); return BigDecimal._(intVal: intResult, scale: scale); } return BigDecimal._(intVal: quotient, scale: scale); @@ -348,7 +363,8 @@ class BigDecimal implements Comparable { BigInt quotient, BigInt remainder, ) { - final remainderComparisonToHalfDivisor = (remainder * BigInt.from(2)).compareTo(divisor); + final remainderComparisonToHalfDivisor = + (remainder * BigInt.from(2)).compareTo(divisor); switch (roundingMode) { case RoundingMode.UNNECESSARY: throw Exception('Rounding necessary'); From 88a56744ac80db77d99dec5836b365314e9c648f Mon Sep 17 00:00:00 2001 From: guilherme Date: Sat, 4 Jan 2025 21:57:35 +1030 Subject: [PATCH 14/14] fix: rename unnecessary snake case params names in tests --- test/big_decimal_test.dart | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/big_decimal_test.dart b/test/big_decimal_test.dart index 3090c1e..46722ab 100644 --- a/test/big_decimal_test.dart +++ b/test/big_decimal_test.dart @@ -287,9 +287,9 @@ void main() { String down, String ceiling, String floor, - String half_up, - String half_down, - String half_even, + String halfUp, + String halfDown, + String halfEven, Object unnecessary, ) { BigDecimal round(RoundingMode mode) => @@ -300,11 +300,11 @@ void main() { reason: 'CEILING'); expect(round(RoundingMode.FLOOR), exactly(floor.dec), reason: 'FLOOR'); - expect(round(RoundingMode.HALF_UP), exactly(half_up.dec), + expect(round(RoundingMode.HALF_UP), exactly(halfUp.dec), reason: 'HALF_UP'); - expect(round(RoundingMode.HALF_DOWN), exactly(half_down.dec), + expect(round(RoundingMode.HALF_DOWN), exactly(halfDown.dec), reason: 'HALF_DOWN'); - expect(round(RoundingMode.HALF_EVEN), exactly(half_even.dec), + expect(round(RoundingMode.HALF_EVEN), exactly(halfEven.dec), reason: 'HALF_EVEN'); if (unnecessary is String) { expect(round(RoundingMode.UNNECESSARY), exactly(unnecessary.dec),