Skip to content

Commit

Permalink
Fix/revert zero() (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciripel authored Feb 17, 2023
1 parent d2bbc1d commit cdfde59
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
## 0.2.1
- Revert `zero()` cause `const` is very valuable.
## 0.2.0
- Changed `zero()` to `zero`
- Fixed bug in `isZero`
- Refactor `clear()`
- Changed `zero()` to `zero`.
- Fixed bug in `isZero`.
- Refactor `clear()`.
## 0.1.5
- Fixed bug in compareTo.
- Fixed bug in `compareTo`.
## 0.1.4
- Added `<`,`<=`,`>`,`>=` operators.
- Added `abs` method.
Expand Down
6 changes: 5 additions & 1 deletion example/sio_big_decimal_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ void main() {
print(e);
}
print('==================================================');
print(BigDecimal.zero()); // 0
print(BigDecimal.zero(precision: 8)); // 0.00000000
print(BigDecimal.zero(precision: -1)); // 0
print('==================================================');
final x = BigDecimal.parse('1.222', precision: 3);
final y = BigDecimal.parse('0.2225', precision: 4);
final z = BigDecimal.parse('1.888', precision: 4);
final zero = BigDecimal.zero;
final zero = BigDecimal.zero();
final addition = x + z; // 3.11 (precision 2)
final subtract = x - y; // 0.9995 (precision 4)
final multiplication = x * z; // 2.307136 (precision: 6)
Expand Down
15 changes: 14 additions & 1 deletion lib/src/sio_big_decimal_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,20 @@ class BigDecimal extends Equatable implements Comparable<BigDecimal> {
final int precision;

/// The [BigDecimal] corresponding to `0`.
static BigDecimal get zero => BigDecimal.fromBigInt(BigInt.zero);
///
/// [defaultPrecision] is 0.
///
/// Negative [precision] will default to `0`.
///
/// Example:
/// ```dart
/// print(BigDecimal.zero()); // 0
/// print(BigDecimal.zero(precision: 8)); // 0.00000000
/// print(BigDecimal.zero(precision: -1)); // 0
/// ```
const BigDecimal.zero({
int precision = defaultPrecision,
}) : this._('', const [0], null, precision);

/// The [BigDecimal] corresponding to `1`.
static BigDecimal get one => BigDecimal.fromBigInt(BigInt.one);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: sio_big_decimal
description: Numeric library to manipulate very big numbers keeping very high precision.
version: 0.2.0
version: 0.2.1
repository: https://github.com/SimplioOfficial/sio_big_decimal

environment:
Expand Down
21 changes: 11 additions & 10 deletions test/sio_big_decimal_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,42 @@ void main() {
});
});

test('zero', () {
expect(BigDecimal.zero().toString(), '0');
expect(BigDecimal.zero(precision: 8).toString(), '0.00000000');
expect(BigDecimal.zero(precision: -1).toString(), '0');
});

group('BigDecimal operators tests -', () {
test('smaller than (<)', () {
expect(BigDecimal.one < BigDecimal.two, true);
expect(BigDecimal.parse('30.00') < BigDecimal.parse('30'), false);
expect(BigDecimal.parse('10') < BigDecimal.parse('-10'), false);
expect(BigDecimal.zero < BigDecimal.parse('-0'), false);
expect(BigDecimal.zero() < BigDecimal.parse('-0'), false);
});
test('smaller than or equal (<=)', () {
expect(BigDecimal.one <= BigDecimal.two, true);
expect(BigDecimal.parse('30.00') <= BigDecimal.parse('30'), true);
expect(BigDecimal.parse('10') <= BigDecimal.parse('-10'), false);
expect(BigDecimal.zero <= BigDecimal.parse('-0'), true);
expect(BigDecimal.zero() <= BigDecimal.parse('-0'), true);
});
test('greater than (>)', () {
expect(BigDecimal.one > BigDecimal.two, false);
expect(BigDecimal.parse('30.00') > BigDecimal.parse('30'), false);
expect(BigDecimal.parse('10') > BigDecimal.parse('-10'), true);
expect(BigDecimal.zero > BigDecimal.parse('-0'), false);
expect(BigDecimal.zero() > BigDecimal.parse('-0'), false);
});
test('smaller than or equal (>=)', () {
expect(BigDecimal.one >= BigDecimal.two, false);
expect(BigDecimal.parse('30.00') >= BigDecimal.parse('30'), true);
expect(BigDecimal.parse('10') >= BigDecimal.parse('-10'), true);
expect(BigDecimal.zero >= BigDecimal.parse('-0'), true);
expect(BigDecimal.zero() >= BigDecimal.parse('-0'), true);
});

test('negation -()', () {
expect((-BigDecimal.one), BigDecimal.parse('-1'));
expect(-BigDecimal.parse('-1'), BigDecimal.one);
expect(-BigDecimal.zero, BigDecimal.zero);
expect(-BigDecimal.zero(), BigDecimal.zero());
});

test('addition (+)', () {
Expand Down Expand Up @@ -274,11 +280,6 @@ void main() {
});

group('BigDecimal getters tests -', () {
test('zero', () {
expect(BigDecimal.zero.toBigInt(), BigInt.zero);
expect(BigDecimal.zero.toString(), '0');
});

test('one', () {
expect(BigDecimal.one.toBigInt(), BigInt.one);
expect(BigDecimal.one.toString(), '1');
Expand Down

0 comments on commit cdfde59

Please sign in to comment.