Skip to content

Commit

Permalink
readme fix and add strict mode toNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
serdimoa committed Oct 30, 2021
1 parent 1c2cbc2 commit dda44fb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
97 changes: 84 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,100 @@ For general information about developing packages, see the Dart guide for
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->
<a href="https://github.com/serdimoa/big.dart/actions"><img src="https://github.com/serdimoa/big.dart/workflows/big.dart/badge.svg" alt="build"></a>
![build](https://github.com/serdimoa/big.dart/workflows/big.dart/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/serdimoa/big.dart/badge.svg)](https://coveralls.io/github/serdimoa/big.dart)
[![style: dart lint recommended](https://img.shields.io/badge/style-lints_recommended-40c4ff.svg)](https://pub.dev/packages/lints)
[![License: MIT](https://img.shields.io/badge/license-MIT-purple.svg)](https://opensource.org/licenses/MIT)


A small library for arbitrary-precision decimal arithmetic inspired by [big.js](https://github.com/MikeMcl/big.js/)

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.
- Simple API
- Easier-to-use
- Replicates the `toStringAsExponential`, `toStringAsFixed` and `toStringAsPrecision` methods of Dart Numbers
- Stores values in an accessible decimal floating point format
- Comprehensive [documentation](http://mikemcl.github.io/big.js/) and test set
- Uses only Dart, so works in well where Dart work


## Use

## Getting started
*In the code examples below, semicolons and `toString` calls are not shown.*

TODO: List prerequisites and provide or point to information on how to
start using the package.
The library exports a single constructor function, `Big`.

A Big number is created from a primitive number, string, or other Big number.

```dart
var x = Big(123.4567)
var y = Big('123456.7e-3')
var z = Big(x)
x.eq(y) && x.eq(z) && y.eq(z) // true
```

## Usage
In Big strict mode, creating a Big number from a primitive number is disallowed.

```dart
Big.strict = true
x = Big(1) // TypeError: [big.dart] Invalid number
y = Big('1.0000000000000001')
y.toNumber() // Error: [big.dart] Imprecise conversion
```

A Big number is immutable in the sense that it is not changed by its methods.

```dart
0.3 - 0.1 // 0.19999999999999998
x = Big(0.3)
x.minus(0.1) // "0.2"
x // "0.3"
```

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
The methods that return a Big number can be chained.

```dart
const like = 'sample';
x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
x.sqrt().div(y).pow(3).gt(y.mod(z)) // true
```

## Additional information
Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods.

```dart
x = Big(255.5)
x.toExponential(5) // "2.55500e+2"
x.toFixed(5) // "255.50000"
x.toPrecision(5) // "255.50"
```

The arithmetic methods always return the exact result except `div`, `sqrt` and `pow`
(with negative exponent), as these methods involve division.

The maximum number of decimal places and the rounding mode used to round the results of these methods is determined by the value of the `dp` and `rm` properties of the `Big` number constructor.

```dart
Big.dp = 10
Big.rm = Big.roundHalfUp
x = Big(2);
y = Big(3);
z = x.div(y) // "0.6666666667"
z.sqrt() // "0.8164965809"
z.pow(-3) // "3.3749999995"
z.times(z) // "0.44444444448888888889"
z.times(z).round(10) // "0.4444444445"
```

The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.

```javascript
x = Big(-123.456);
x.c // [1,2,3,4,5,6] coefficient (i.e. significand)
x.e // 2 exponent
x.s // -1 sign
```


(dart code fully converted to Dart with test and etc.)


TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
12 changes: 7 additions & 5 deletions lib/src/big.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Big with EquatableMixin {
/// ************************************************************************************************/
// Error messages.
static const name = '[big.js] ';
static const name = '[big.dart] ';
static const invalid = name + 'Invalid ';
static const invalidDp = invalid + 'decimal places';
static const invalidRm = invalid + 'rounding mode';
Expand Down Expand Up @@ -923,10 +923,12 @@ class Big with EquatableMixin {
String valueOf() {
var x = this;

/// TODO:
// if (Big.strict == true) {
// throw Error(NAME + 'valueOf disallowed');
// }
if (Big.strict == true) {
throw BigError(
description: name + 'valueOf disallowed',
code: BigErrorCode.type,
);
}
return stringify(x, x.e <= ne || x.e >= pe, true);
}

Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ environment:

dependencies:
equatable: ^2.0.3
collection: ^1.15.0

dev_dependencies:
lints: ^1.0.0
Expand Down

0 comments on commit dda44fb

Please sign in to comment.